How to import data to Excel using List T in Flutter using XlsIO?
Syncfusion Flutter XlsIO library is used to create Excel documents. Using this library, you can also import data into an Excel worksheet using a list in Flutter.
Steps to import data into an Excel worksheet using List<T> in Flutter.
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 Application 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 command or Get packages option from the Visual Studio Code.
dependencies: syncfusion_flutter_xlsio: ^19.1.59-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>[ TextButton( child: const Text('Generate Excel'), style: TextButton.styleFrom( primary: Colors.white, backgroundColor: Colors.lightBlue, onSurface: Colors.grey, ), onPressed: importData, ) ], ), ), ); }
Step 4: Use the following code to load an image in the Flutter project.
- Add the following code in your pubspec.yaml file.
flutter: # To add assets to your application, add an assets section, like this: assets: - images/
- Import the following package in your main.dart file.
import 'package:flutter/services.dart';
- Add the following code in lib/main.dart file to read an image.
Future<List<int>> _readImageData(String name) async { final ByteData data = await rootBundle.load('images/$name'); return data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); }
Step 5: Use the following code to export the list of data rows
- Add the following code in lib/main.dart file to export data rows list.
Future<List<ExcelDataRow>> _buildCustomersDataRowsIH() async { List<ExcelDataRow> excelDataRows = <ExcelDataRow>[]; final Future<List<_Customers>> reports = _getCustomersImageHyperlink(); List<_Customers> reports_1 = await Future.value(reports); excelDataRows = reports_1.map<ExcelDataRow>((_Customers dataRow) { return ExcelDataRow(cells: <ExcelDataCell>[ ExcelDataCell(columnName: 'Sales Person', value: dataRow.salesPerson), ExcelDataCell( columnName: 'Sales Jan to June', value: dataRow.salesJanJune), ExcelDataCell( columnName: 'Sales July to Dec', value: dataRow.salesJulyDec), ExcelDataCell(columnName: 'Change', value: dataRow.change), ExcelDataCell(columnName: 'Hyperlink', value: dataRow.hyperlink), ExcelDataCell(columnName: 'Images Hyperlinks', value: dataRow.image) ]); }).toList(); return excelDataRows; } Future<List<_Customers>> _getCustomersImageHyperlink() async { final List<_Customers> reports = <_Customers>[]; final Hyperlink link = Hyperlink.add('https://www.syncfusion.com', 'Hyperlink', 'Syncfusion', HyperlinkType.url); Picture pic = Picture(await _readImageData('Woman1.jpg')); pic.width = 200; pic.height = 200; pic.hyperlink = link; _Customers customer = _Customers('Meghna', 45000, 58000, 29); customer.hyperlink = link; customer.image = pic; reports.add(customer); pic = Picture(await _readImageData('Man4.png')); pic.width = 200; pic.height = 200; pic.hyperlink = link; customer = _Customers('Patricia McKenna', 34000, 65000, 91); customer.hyperlink = link; customer.image = pic; reports.add(customer); pic = Picture(await _readImageData('Woman12.jpg')); pic.width = 200; pic.height = 200; pic.hyperlink = link; customer = _Customers('Antonio Moreno Taquería', 75000, 64000, -15); customer.hyperlink = link; customer.image = pic; reports.add(customer); pic = Picture(await _readImageData('Man16.jpg')); pic.width = 200; pic.height = 200; pic.hyperlink = link; customer = _Customers('Thomas Hardy', 56500, 33600, -40); customer.hyperlink = link; customer.image = pic; reports.add(customer); pic = Picture(await _readImageData('Man6.png')); pic.width = 200; pic.height = 200; pic.hyperlink = link; customer = _Customers('Christina Berglund', 46500, 52000, 12); customer.hyperlink = link; customer.image = pic; reports.add(customer); return reports; }
- Create a _Customers class in lib/main.dart file.
class _Customers { _Customers( this.salesPerson, this.salesJanJune, this.salesJulyDec, this.change); String salesPerson; int salesJanJune; int salesJulyDec; int change; Hyperlink hyperlink; Picture image; } |
Step 6: Add the following code to the importData() function to import data into an Excel document programmatically.
Future<void> importData() async { // Create a Excel document. // Creating a workbook. final Workbook workbook = Workbook(); // Accessing via index final Worksheet sheet = workbook.worksheets[0]; // List of data to import data. final Future<List<ExcelDataRow>> dataRows = _buildCustomersDataRowsIH(); List<ExcelDataRow> dataRows_1 = await Future.value(dataRows); // Import the list to Sheet. sheet.importData(dataRows_1, 1, 1); // Auto-Fit columns. sheet.getRangeByName('A1:E1').autoFitColumns(); //Save and launch the excel. final List<int> bytes = workbook.saveAsStream(); // Dispose the document. workbook.dispose(); // Get the storage folder location using path_provider package. final Directory directory = await getExternalStorageDirectory(); final String path = directory.path; final File file = File('$path/ImportData.xlsx'); await file.writeAsBytes(bytes, flush: true); // Launch the file (used open_file package) await OpenFile.open('$path/ImportData.xlsx'); }
Step 5: Use the following code to save and launch the generated Excel file.
- 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
- 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';
- Include the following code snippet in the importData() 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 the directory path final String path = directory.path; // Create an empty file to write the Excel data final File file = File('$path/ImportData.xlsx'); // Write Excel data await file.writeAsBytes(bytes, flush: true); // Launch the file (used open_file package) await OpenFile.open('$path/ImportData.xlsx');
Step 6: Run the sample using the flutter run command. This will import data in the Excel document.
After the application launches, you will get the Excel document below.
Output Excel document created using XlsIO
You can download the complete work sample from import-data.zip.
Take a moment to peruse the documentation. Also, the features like formulas, images, charts, protect Excel documents, and more with code examples.
Conclusion
I hope you enjoyed learning about how to import data to Excel using List T in Flutter using XlsIO.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!