How to create Excel programmatically in Flutter web platform?
Syncfusion Flutter XlsIO is a non-UI Excel library written natively in Dart for creating the Excel documents from scratch. Using this package, you can create Excel document in the Flutter Mobile and Web platforms.
Steps to create an Excel document programmatically in Web platform
Step 1: Create a new Flutter application project.
- Open Visual Studio Code (After installing the Dart and Flutter extensions as stated in this setup editor page)
- Click View -> Command Paletteā¦
- Type Flutter and choose Flutter: New Project.
- Enter the project name and press the Enter button.
- 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 a 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: Text( 'Generate Excel', style: TextStyle(color: Colors.white), ), color: Colors.blue, onPressed: _createExcel, ) ], ), ), ); }
Step 4: Add the following code to the _createExcel function to create an Excel document programmatically.
Future<void> _createExcel() async { //Create a Excel document. //Creating a workbook. final Workbook workbook = Workbook(); //Accessing via index. final Worksheet sheet = workbook.worksheets[0]; // Set the text value. sheet.getRangeByName('A1').setText('Hello!!'); //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.
- Import the following packages in your main.dart file.
import 'dart:convert'; import 'dart:html';
- Include the following code snippet in the _createExcel 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", "output.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 Chrome browser. This creates a simple Excel document.
After the application launches, you will get the application preview and Excel document as followsbelow.
Screenshot of the application
Output Excel document created using XlsIO
A complete working sample can be downloaded from CreateExcelFile.Zipcreate-excel-in-flutter.zip.
Take a moment to peruse the documentation, where you can find other options like cell formatting, Excel formulas, number format, hyperlinks, row and column manipulations. Also, the features like images, charts, protect Excel documents, and more with code examples.
Conclusion
I hope you enjoyed learning about how to create Excel programmatically in Flutter web platform.
You can refer to our Flutter library feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flutter library example to understand how to create and manipulate data.
For current customers, you can check out our Document Processing Libraries from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our 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 forums, Direct-trac, or feedback portal. We are always happy to assist you!