exportToExcelWorksheet method

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

Exports the SfDataGrid to the given Worksheet.

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

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 Excel sheet where DataGrid content should be started using startRowIndex and startColumnIndex.

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

Future<void> exportDataGridToExcel() async {
  final Workbook workbook = Workbook();
  final Worksheet worksheet = workbook.worksheets[0];
  _key.currentState!.exportToExcelWorksheet(worksheet);
  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

void exportToExcelWorksheet(Worksheet worksheet,
    {List<DataGridRow>? rows,
    bool exportStackedHeaders = true,
    bool exportTableSummaries = true,
    int defaultColumnWidth = 90,
    int defaultRowHeight = 49,
    bool exportColumnWidth = true,
    bool exportRowHeight = 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,
  );

  converter.exportToExcelWorksheet(widget, rows, worksheet);
}