Getting Started with Flutter XlsIO
6 Jun 20236 minutes to read
This section explains the steps required to create an Excel document by few lines of code. This section covers only the minimal features needed to learn to get started with the Excel.
Steps to create Excel document in Flutter application
Create a simple project using the instructions given in the `Getting Started with your first Flutter app’ documentation.
Add dependency
Add the Syncfusion Flutter XlsIO dependency to your pub spec file.
dependencies:
syncfusion_flutter_xlsio: ^xx.x.xx
NOTE
Here xx.x.xx denotes the current version of ‘Syncfusion Flutter XlsIO’ package.
Get packages
Run the following command to get the required packages.
$ flutter pub get
Import package
Import the following package in your Dart code.
import 'package:syncfusion_flutter_xlsio/xlsio.dart';
Add a new button widget as a child of your container widget.
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: _createExcel,
child: Text('Create Excel')
)
)
);
}
Include the following code snippet in the button click event to create an Excel file.
Future<void> _createExcel() async {
// Create a new Excel Document.
final Workbook workbook = Workbook();
// Accessing worksheet via index.
final Worksheet sheet = workbook.worksheets[0];
// Set the text value.
sheet.getRangeByName('A1').setText('Hello World!');
// Save and dispose the document.
final List<int> bytes = workbook.saveSync();
workbook.dispose();
// Save the Excel file in the local machine.
File('Output.xlsx').writeAsBytes(bytes);
}
Create an Excel document in mobile
You can create an Excel document in mobile by using the following steps:
Add dependency
Add the following packages to your pub spec file.
path_provider: ^1.6.5
open_file: ^3.0.1
Import package
import 'dart:io';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
Include the following code snippet to create an Excel document in mobile.
Future<void> _createExcel() async {
// Create a new Excel Document.
final Workbook workbook = Workbook();
// Accessing worksheet via index.
final Worksheet sheet = workbook.worksheets[0];
// Set the text value.
sheet.getRangeByName('A1').setText('Hello World!');
// Save and dispose the document.
final List<int> bytes = workbook.saveSync();
workbook.dispose();
// Get external storage directory
final directory = await getExternalStorageDirectory();
// Get directory path
final path = directory.path;
// Create an empty file to write Excel data
File file = File('$path/Output.xlsx');
// Write Excel data
await file.writeAsBytes(bytes, flush: true);
// Open the Excel document in mobile
OpenFile.open('$path/Output.xlsx');
}
Create an Excel document in web
You can create an Excel document in web by using the following steps.
Import package
import 'dart:convert';
import 'dart:html';
Include the following code snippet to create an Excel document in web.
Future<void> _createExcel() async {
// Create a new Excel Document.
final Workbook workbook = Workbook();
// Accessing worksheet via index.
final Worksheet sheet = workbook.worksheets[0];
// Set the text value.
sheet.getRangeByName('A1').setText('Hello World!');
// Save and dispose the document.
final List<int> bytes = workbook.saveSync();
workbook.dispose();
//Download the output file in web.
AnchorElement(
href:
"data:application/octet-stream;charset=utf-16le;base64,${base64.encode(bytes)}")
..setAttribute("download", "output.xlsx")
..click();
Create an Excel document in Desktop
Follow the steps provide in link to set desktop support in Flutter.
You can create an Excel document in desktop by using the following steps:
Add dependency
Add the following packages to your pub spec file.
path_provider: ^2.0.1
Import package
import 'package:path_provider/path_provider.dart';
Include the following code snippet to create an Excel document in desktop.
Future<void> _createExcel() async {
//Create an 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.saveSync();
//Dispose the document.
workbook.dispose();
//Get the storage folder location using path_provider package.
final Directory directory =
await path_provider.getApplicationSupportDirectory();
final String path = directory.path;
final File file =
File(Platform.isWindows ? '$path\\output.xlsx' : '$path/output.xlsx');
await file.writeAsBytes(bytes, flush: true);
if (Platform.isWindows) {
await Process.run('start', <String>['$path\\output.xlsx'],
runInShell: true);
} else if (Platform.isMacOS) {
await Process.run('open', <String>['$path/output.xlsx'],
runInShell: true);
} else if (Platform.isLinux) {
await Process.run('xdg-open', <String>['$path/output.xlsx'],
runInShell: true);
}
}