How to autofit column widths and row Heights of Excel in Flutter
Syncfusion Flutter XlsIO library is used to create and modify Excel documents. Using this library, you can also auto-size the column width and row height of a cells to its content in Excel worksheet using Flutter.
Steps to autofit column widths and row Heights of Excel 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 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: _autoFitRowColumn, ) ], ), ), ); }
Step 4: Add the following code to the _autoFitRowColumn() function to autofit rows and columns of an Excel document programmatically.
Future<void> _autoFitRowColumn() async { //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('WrapTextWrapText'); range.cellStyle.wrapText = true; final Range range1 = sheet.getRangeByName('B1'); range1.setText('This is long text'); // Assigning text to cells final Range range2 = sheet.getRangeByName('C4:D4'); range2.setText('This is Long Text'); final Range range3 = sheet.getRangeByName('C5:D5'); range3.setText('This is Long Text using AutoFit Columns and Rows'); range3.cellStyle.wrapText = true; // AutoFit applied to a single row sheet.autoFitRow(1); // AutoFit applied to a single Column. sheet.autoFitColumn(2); // Auto-Fit column the range range2.autoFitColumns(); // Auto-Fit row the range range3.autoFitRows(); //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.
- 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 _autoFitRowColumn() 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/AutoFitRowColumn.xlsx'); //Write Excel data await file.writeAsBytes(bytes, flush: true); //Launch the file (used open_file package) await OpenFile.open('$path/AutoFitRowColumn.xlsx');
Step 6: Run the sample using the flutter run command. This will autofit the rows and columns of 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 autofit-row-and-column.zip.
Take a moment to peruse the documentation of autofit rows and columns. Also, the features like formulas, images, charts, protect Excel documents, and more with code examples.