|
1 | | -# How-to-export-all-rows-while-having-pagination-in-a-Flutter-DataTable-SfDataGrid |
2 | | -How to export all rows while having pagination in a Flutter DataTable (SfDataGrid). |
| 1 | +# How to export all rows while having pagination in a Flutter DataTable (SfDataGrid)?. |
| 2 | + |
| 3 | +In this article, we will show you how to export all rows while having pagination in a [Flutter DataTable](). |
| 4 | + |
| 5 | +Initialize the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html) widget with all the necessary properties. When exporting to Excel, both the [exportDataGridToExcel](https://pub.dev/documentation/syncfusion_flutter_datagrid_export/latest/syncfusion_flutter_datagrid_export/DataGridExcelExportExtensions/exportToExcelWorkbook.html) and [exportToExcelWorksheet](https://pub.dev/documentation/syncfusion_flutter_datagrid_export/latest/syncfusion_flutter_datagrid_export/DataGridExcelExportExtensions/exportToExcelWorksheet.html) methods, and when exporting to PDF, both the [exportDataGridToPdfDocument](https://pub.dev/documentation/syncfusion_flutter_datagrid_export/latest/syncfusion_flutter_datagrid_export/DataGridPdfExportExtensions/exportToPdfDocument.html) and [exportDataGridToPdfGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid_export/latest/syncfusion_flutter_datagrid_export/DataGridPdfExportExtensions/exportToPdfGrid.html) methods, include a named property called [rows](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource/rows.html).This property enables you to export specific rows. You can pass all DataGridRows to the rows property to export the entire set of rows. |
| 6 | + |
| 7 | +```dart |
| 8 | + Future<void> exportDataGridToExcel() async { |
| 9 | + // Generate data grid rows for entire data source |
| 10 | + List<DataGridRow> datagridRows = _employees |
| 11 | + .map<DataGridRow>((employees) => DataGridRow(cells: [ |
| 12 | + DataGridCell<int>(columnName: 'id', value: employees.id), |
| 13 | + DataGridCell<String>(columnName: 'name', value: employees.name), |
| 14 | + DataGridCell<String>( |
| 15 | + columnName: 'designation', value: employees.designation), |
| 16 | + DataGridCell<int>(columnName: 'salary', value: employees.salary), |
| 17 | + ])) |
| 18 | + .toList(); |
| 19 | + final Workbook workbook = |
| 20 | + _key.currentState!.exportToExcelWorkbook(rows: datagridRows); |
| 21 | + final List<int> bytes = workbook.saveAsStream(); |
| 22 | +
|
| 23 | + await helper.saveAndLaunchFile(bytes, 'DataGrid.xlsx'); |
| 24 | + workbook.dispose(); |
| 25 | + } |
| 26 | +
|
| 27 | + @override |
| 28 | + Widget build(BuildContext context) { |
| 29 | + return Scaffold( |
| 30 | + appBar: AppBar(title: const Text('Syncfusion Flutter DataGrid')), |
| 31 | + body: LayoutBuilder( |
| 32 | + builder: (BuildContext context, BoxConstraints constraints) { |
| 33 | + return Column(children: [ |
| 34 | + Padding( |
| 35 | + padding: const EdgeInsets.all(8.0), |
| 36 | + child: SizedBox( |
| 37 | + height: 40, |
| 38 | + child: ElevatedButton( |
| 39 | + onPressed: exportDataGridToExcel, |
| 40 | + child: const Text('Export To Excel'), |
| 41 | + ), |
| 42 | + ), |
| 43 | + ), |
| 44 | + SizedBox( |
| 45 | + height: constraints.maxHeight - 120, |
| 46 | + child: buildDataGrid(constraints)), |
| 47 | + SizedBox( |
| 48 | + height: 60, |
| 49 | + child: SfDataPager( |
| 50 | + pageCount: pageCount, delegate: _employeeDataSource)) |
| 51 | + ]); |
| 52 | + }), |
| 53 | + ); |
| 54 | + } |
| 55 | +``` |
| 56 | + |
| 57 | +You can download the example from [GitHub](https://github.com/SyncfusionExamples/How-to-export-all-rows-while-having-pagination-in-a-Flutter-DataTable-SfDataGrid). |
0 commit comments