Excel Security using Syncfusion XlsIO for Flutter
13 Jul 20264 minutes to read
You can protect other users from viewing, moving, editing, or deleting important data in a worksheet or workbook by protecting a worksheet or workbook, with or without a password.
NOTE
Before you begin, make sure you have completed the Getting Started with Flutter XlsIO steps to add the package and import it.
NOTE
In production code, use
await workbook.save()(asynchronous) and wrap workbook usage in atry/finallyblock to callworkbook.dispose(). The samples below usesaveSync()and direct disposal for brevity.
Protect workbook elements
To keep others from making structural changes to your document — such as moving, deleting, and adding sheets — you can protect the workbook in Flutter XlsIO. The workbook.protect method accepts two flags:
-
isProtectWindow– Prevents users from changing the position or size of the workbook window. -
isProtectContent– Prevents users from adding, removing, renaming, hiding, or moving worksheets.
The following code example illustrates how to protect a workbook with a password.
import 'dart:io';
import 'package:syncfusion_flutter_xlsio/xlsio.dart';
// Create a new Excel Document.
final Workbook workbook = Workbook();
// Accessing sheet via index.
final Worksheet sheet = workbook.worksheets[0];
// Assigning text to cells.
final Range range = sheet.getRangeByName('A1');
range.setText('WorkBook Protected');
final bool isProtectWindow = true;
final bool isProtectContent = true;
// Protect Workbook
workbook.protect(isProtectWindow, isProtectContent, 'password');
// Save and dispose workbook.
final List<int> bytes = workbook.saveSync();
await File('WorkbookProtect.xlsx').writeAsBytes(bytes);
workbook.dispose();To remove workbook protection, call workbook.unprotect('password').
Protect Worksheet
Flutter XlsIO provides support for protecting elements in a worksheet by using the protect method of Worksheet. You can fine-tune which operations are allowed for the user by setting properties on the ExcelSheetProtectionOption class, such as all, insertRows, deleteRows, insertColumns, deleteColumns, formatCells, formatColumns, formatRows, selectLockedCells, selectUnlockedCells, sort, and useAutoFilter. Setting options.all = true disables all of them.
The following code example illustrates how to protect a worksheet with a password.
import 'dart:io';
import 'package:syncfusion_flutter_xlsio/xlsio.dart';
// Create a new Excel Document.
final Workbook workbook = Workbook();
// Accessing sheet via index.
final Worksheet sheet = workbook.worksheets[0];
// Assigning text to cells.
final Range range = sheet.getRangeByName('A1');
range.setText('Worksheet Protected');
// ExcelSheetProtectionOption.
final ExcelSheetProtectionOption options = ExcelSheetProtectionOption();
options.all = true;
// Protecting the Worksheet by using a Password.
sheet.protect('Password', options);
// Save and dispose workbook.
final List<int> bytes = workbook.saveSync();
await File('WorksheetProtect.xlsx').writeAsBytes(bytes);
workbook.dispose();NOTE
By using the
ExcelSheetProtectionOptionclass, you can set protection for individual worksheet elements/operations. To remove worksheet protection, callsheet.unprotect('Password').
Protect Cell
Flutter XlsIO supports locking and unlocking cells by using the cell’s Locked property of CellStyle. This can be manipulated to make certain cells editable in a protected worksheet. The cell’s lock state is only enforced when the worksheet itself is protected, so the cell style and sheet.protect(...) must both be configured before saving.
The following code example illustrates how to lock a worksheet and then unlock a specific cell so that it remains editable.
import 'dart:io';
import 'package:syncfusion_flutter_xlsio/xlsio.dart';
// Create a new Excel Document.
final Workbook workbook = Workbook();
// Accessing sheet via index.
final Worksheet sheet = workbook.worksheets[0];
// Assigning text to cells.
final Range range = sheet.getRangeByName('A1');
range.setText('Worksheet Protected');
// Unlock the cell so it remains editable after the worksheet is protected.
range.cellStyle.locked = false;
// Protecting the Worksheet by using a Password.
sheet.protect('Password');
// Save and dispose workbook.
final List<int> bytes = workbook.saveSync();
await File('ProtectCell.xlsx').writeAsBytes(bytes);
workbook.dispose();NOTE
By default, cells are locked. Locking or unlocking a cell in an unprotected worksheet has no visible effect until the worksheet is protected.