Articles in this section

How to export DataGrid content with a stacked header to PDF and Excel documents in Flutter DataGrid?

In this article, you can learn about how to export DataGrid content with a Stacked Header to PDF and Excel documents in the Flutter DataGrid.

Initialize the SfDataGrid widget with all the required properties. To ensure proper export of the StackedHeaderRow to Excel or PDF, it is crucial to define the corresponding text within the StackedHeaderCell.text property. As widgets cannot be exported to Excel or PDF, setting this property is essential to provide the appropriate text during the export process of the SfDataGrid to Excel or PDF.

  Future<void> exportDataGridToPDF() async {
    final sf_pdf.PdfDocument document = key.currentState!.exportToPdfDocument();
    final List<int> bytes = document.saveSync();
    await helper.saveAndLaunchFile(bytes, 'DataGrid.pdf');
    document.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Syncfusion Flutter DataGrid'),
      ),
      body: Column(
        children: [
          Container(
            margin: const EdgeInsets.symmetric(vertical: 10),
            width: 250,
            child: ElevatedButton.icon(
                icon: const Icon(Icons.print),
                onPressed: () => exportDataGridToPDF(),
                label: const Text('Export to PDF')),
          ),
          Expanded(
            child: SfDataGrid(
              key: key,
              source: employeeDataSource,
              stackedHeaderRows: [
                StackedHeaderRow(
                  cells: [
                    StackedHeaderCell(
                      text: 'Employee Details', // Text used when exporting
                      columnNames: ['id', 'name', 'designation', 'salary'],
                      child: const Center(child: Text('Employee Details')),
                    ),
                  ],
                ),
              ],
              columns: getColumns,
            ),
          ),
        ],
      ),
    );
  }

View the GitHub sample here

Conclusion

I hope you enjoyed learning about how to export DataGrid content with a stacked header to PDF and Excel documents in Flutter DataGrid.

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)
Access denied
Access denied