Articles in this section
Category / Section

How to Protect Excel worksheets programmatically in Flutter web platform?

2 mins read

Syncfusion Flutter XlsIO library is used to create and modify Excel documents. Using this library, you can also protect Excel Worksheet elements with password using Flutter. Worksheet Protection is used to protect the structure of elements in the worksheet from viewing, moving, editing, or deleting important data.

Steps to protect Excel worksheet programmatically in Flutter Web platform.


Step 1: Create a new Flutter application project.

  1. Open Visual Studio Code (After installing the Dart and Flutter extensions as stated in this setup editor page)
  2. Click View -> Command Palette…

Command Palette

  1. Type Flutter and choose Flutter: New Application Project.

New project

  1. Enter the project name and press the Enter button.
  2. Now choose the location of the project.

 

Step 2: Add the following code in your pubspec.yaml file to install the syncfusion flutter xlsio package in your application. It will be automatically downloaded from the pub once you trigger the flutter pub get comment or Get packages option from the Visual Studio Code.

dependencies:
   syncfusion_flutter_xlsio: ^18.4.31-beta

 

Import the following package in your main.dart file.

import 'package:syncfusion_flutter_xlsio/xlsio.dart' ;

 

Step 3: Add the following code in the lib/main.dart file to create a simple button.

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            FlatButton(
              child: const Text(
                'Generate Excel',
                style: TextStyle(color: Colors.white),
              ),
              color: Colors.blue,
              onPressed: _protectExcelWorksheet,
            )
          ],
        ),
      ),
    );
  }

 

Step 4: Add the following code to _protectExcelWorksheet() the  function to protect Excel worksheet programmatically.

Future<void> _protectExcelWorksheet() async {
    //Create a Excel document.
    //Creating a workbook.
    final Workbook workbook = Workbook();
 
    //Accessing 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 launch the excel.
    final List<int> bytes = workbook.saveAsStream();
 
    //Dispose the document.
    workbook.dispose();
}

 

Step 5: Use the following code to download the generated Excel file in web platform.

  1. Import the following packages in your main.dart file.
    import 'dart:convert';
    import 'dart:html';
    

 

  1. Include the following code snippet in the _protectExcelWorksheet() method to download the Excel document in the web application (any browser).
    //Download the output file
    AnchorElement(
        href:
            "data:application/octet-stream;charset=utf-16le;base64,${base64.encode(bytes)}")
      ..setAttribute("download", "ProtectExcelWorksheet.xlsx")
      ..click();
    

 

Step 6: To run the sample in web platform, follow the steps given in Building a web application with Flutter. Using the flutter run -d chrome command, launch the application in Chrome browser. This creates an Excel document with worksheet protected.

After the application launches, you will get the application preview and Excel document below.

Output 1

Screenshot of the application

Output 2

Output Excel document created using XlsIO

You can download the complete work sample from protect-excel-worksheet.zip.

Take a moment to peruse the documentation of security,  where you can find other protection options like protect workbook and protect cell in Excel. Also, finds the features like images, charts and more with code examples.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied