Articles in this section
Category / Section

How to export all rows while having pagination in a Flutter DataTable?

3 mins read

In this article, we will show you how to export all rows while having pagination in a Flutter DataTable.

Initialize the SfDataGrid widget with all the necessary properties. When exporting to Excel, both the exportDataGridToExcel and exportToExcelWorksheet methods, and when exporting to PDF, both the exportDataGridToPdfDocument and exportDataGridToPdfGrid methods, include a named property called rows.This property enables you to export specific rows. You can pass all DataGridRows to the rows property to export the entire set of rows.

  Future<void> exportDataGridToExcel() async {
    // Generate data grid rows for entire data source
    List<DataGridRow> datagridRows = _employees
        .map<DataGridRow>((employees) => DataGridRow(cells: [
              DataGridCell<int>(columnName: 'id', value: employees.id),
              DataGridCell<String>(columnName: 'name', value: employees.name),
              DataGridCell<String>(
                  columnName: 'designation', value: employees.designation),
              DataGridCell<int>(columnName: 'salary', value: employees.salary),
            ]))
        .toList();
    final Workbook workbook =
        _key.currentState!.exportToExcelWorkbook(rows: datagridRows);
    final List<int> bytes = workbook.saveAsStream();

    await helper.saveAndLaunchFile(bytes, 'DataGrid.xlsx');
    workbook.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Syncfusion Flutter DataGrid')),
      body: LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
        return Column(children: [
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: SizedBox(
              height: 40,
              child: ElevatedButton(
                onPressed: exportDataGridToExcel,
                child: const Text('Export To Excel'),
              ),
            ),
          ),
          SizedBox(
              height: constraints.maxHeight - 120,
              child: buildDataGrid(constraints)),
          SizedBox(
              height: 60,
              child: SfDataPager(
                  pageCount: pageCount, delegate: _employeeDataSource))
        ]);
      }),
    );
  }

View the GitHub sample here.

Conclusion

I hope you enjoyed learning about how to export all rows while having pagination in a Flutter DataTable.

You can refer to our Flutter DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied