exportToExcelWorkbook method

Workbook exportToExcelWorkbook({
  1. List<DataGridRow>? rows,
  2. bool exportStackedHeaders = true,
  3. bool exportTableSummaries = true,
  4. int defaultColumnWidth = 90,
  5. int defaultRowHeight = 49,
  6. bool exportRowHeight = true,
  7. bool exportColumnWidth = true,
  8. int startColumnIndex = 1,
  9. int startRowIndex = 1,
  10. List<String> excludeColumns = const <String>[],
  11. DataGridToExcelConverter? converter,
  12. DataGridCellExcelExportCallback? cellExport,
})

Exports the SfDataGrid to Excel Workbook.

If the rows is set, the given list of DataGridRow collections is exported. Typically, you can set this property to export the selected rows from SfDataGrid.

Use cellExport argument as the callback, and it will be called for each cell. You can customize the cell in the Excel sheet.

Set the customized Excel converter using the converter argument. To customize the default Excel converter, override the DataGridToExcelConverter class and override the necessary methods.

Use defaultColumnWidth and defaultRowHeight arguments to set the default column width and row height in Excel while exporting.

Define the start of the row and column index in the Excel sheet where DataGrid content should be started using startRowIndex and startColumnIndex.

The following example shows how to export the SfDataGrid to the Excel workbook.

final GlobalKey<SfDataGridState> _key = GlobalKey<SfDataGridState>();

Future<void> exportDataGridToExcel() async {
  final Workbook workbook = _key.currentState!.exportToExcelWorkbook();
  final List<int> bytes = workbook.saveAsStream();
  File('DataGrid.xlsx').writeAsBytes(bytes);
  workbook.dispose();
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text(
        'Syncfusion Flutter DataGrid Export',
        overflow: TextOverflow.ellipsis,
      ),
    ),
    body: Column(
      children: <Widget>[
        Container(
          margin: const EdgeInsets.all(12.0),
          child: const SizedBox(
            height: 40.0,
            width: 150.0,
            child: MaterialButton(
                color: Colors.blue,
                child: Center(
                    child: Text(
                  'Export to Excel',
                  style: TextStyle(color: Colors.white),
                )),
                onPressed: exportDataGridToExcel),
          ),
        ),
        Expanded(
          child: SfDataGrid(
            key: _key,
            source: employeeDataSource,
            columns: <GridColumn>[
              GridColumn(
                  columnName: 'ID',
                  label: Container(
                      padding: const EdgeInsets.all(16.0),
                      alignment: Alignment.center,
                      child: const Text(
                        'ID',
                      ))),
              GridColumn(
                  columnName: 'Name',
                  label: Container(
                      padding: const EdgeInsets.all(8.0),
                      alignment: Alignment.center,
                      child: const Text('Name'))),
              GridColumn(
                  columnName: 'Designation',
                  label: Container(
                      padding: const EdgeInsets.all(8.0),
                      alignment: Alignment.center,
                      child: const Text(
                        'Designation',
                        overflow: TextOverflow.ellipsis,
                      ))),
              GridColumn(
                  columnName: 'Salary',
                  label: Container(
                      padding: const EdgeInsets.all(8.0),
                      alignment: Alignment.center,
                      child: const Text('Salary'))),
            ],
          ),
        ),
      ],
    ),
  );
}

Implementation

Workbook exportToExcelWorkbook(
    {List<DataGridRow>? rows,
    bool exportStackedHeaders = true,
    bool exportTableSummaries = true,
    int defaultColumnWidth = 90,
    int defaultRowHeight = 49,
    bool exportRowHeight = true,
    bool exportColumnWidth = true,
    int startColumnIndex = 1,
    int startRowIndex = 1,
    List<String> excludeColumns = const <String>[],
    DataGridToExcelConverter? converter,
    DataGridCellExcelExportCallback? cellExport}) {
  converter ??= DataGridToExcelConverter();

  _initializeProperties(
    converter,
    cellExport: cellExport,
    rowHeight: defaultRowHeight,
    columnWidth: defaultColumnWidth,
    excludeColumns: excludeColumns,
    excelStartRowIndex: startRowIndex,
    excelStartColumnIndex: startColumnIndex,
    canExportRowHeight: exportRowHeight,
    canExportColumnWidth: exportColumnWidth,
    canExportTableSummaries: exportTableSummaries,
    canExportStackedHeaders: exportStackedHeaders,
  );

  return converter.exportToExcelWorkbook(widget, rows);
}