Articles in this section
Category / Section

How to Protect Excel workbook programmatically in Flutter?

2 mins read

Flutter XIsIO library is used to create and modify Excel documents. Using this library, you can also protect Excel Workbook elements with or without a password using Flutter. Workbook protection is used to protect the structure of workbook elements from viewing, moving, editing, or deleting important data.

Steps to protect the workbook of the Excel document programmatically.


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 Project.

Flutter - 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: _protectWorkbook,
            )
          ],
        ),
      ),
    );
  }

 

Step 4: Add the following code to the _protectWorkbook() function to protect workbook of an Excel document programmatically.

Future<void> _protectWorkbook() 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('WorkBook Protected');
 
    final bool isProtectWindow = true;
    final bool isProtectContent = true;
 
    // Protect Workbook
    workbook.protect(isProtectWindow, isProtectContent, 'password');
 
    // Save and launch the excel.
    final List<int> bytes = workbook.saveAsStream();
 
    // Dispose the document.
    workbook.dispose();
}

 

Step 5: Use the following code to save and launch the generated Excel file.

  1. Add the following dependencies in your pubspec.yaml file.
    path_provider: ^1.6.7
    open_file: ^3.0.1   #Open source library to launch the Excel file in mobile devices
    

 

  1. Import the following packages in your main.dart file.
    import 'dart:io'; 
    import 'package:path_provider/path_provider.dart';
    import 'package:open_file/open_file.dart';
    

 

  1. Include the following code snippet in the _protectWorkbook() method to open the Excel document in the mobile‘s default application (any Excel application).
    // Get the storage folder location using path_provider package.
    final Directory directory = await getExternalStorageDirectory();
    // Get directory path
    final String path = directory.path;
    // Create an empty file to write Excel data
    final File file = File('$path/ProtectWorkbook.xlsx');
    // Write Excel data
    await file.writeAsBytes(bytes, flush: true);
    // Launch the file (used open_file package)
    await OpenFile.open('$path/ProtectWorkbook.xlsx');
    

 

Step 6: Run the sample using the flutter run command. This will protect the workbook of the Excel document.

After the application launches, you will get the Excel document as follows.

Graphical user interface, application, table, Excel

Description automatically generated

Output Excel document created using XlsIO

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

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


Conclusion

I hope you enjoyed learning about how to protect Excel workbook in Syncfusion Flutter XIsIO Library.

You can refer to our Flutter XIsIO feature tour page to know about its other groundbreaking feature representations documentation and how to quickly get started for configuration specifications.  You can also explore our Flutter XIsIO example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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