Skip to content

abineshPalanisamy/how-to-apply-a-specific-format-to-the-DateTime-value-during-Excel-export-in-Flutter-DataTable

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to apply a specific format to the DateTime value during Excel export in Flutter DataTable (SfDataGrid)

This article describes how to add DateTime value format to an Excel spreadsheet when exporting a Flutter DataGrid

Initialize the SfDataGrid widget with all the necessary properties. In the cellExport callback, check if the column name is "DOJ" and the cell type is DataGridExportCellType.row. If the conditions are met, set the desired format for the number using the excelRange.numberFormat property and assign the DateTime value to the cell using the excelRange.value property.

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

Future<void> exportDataGridToExcel() async {
    final Workbook workbook = Workbook();
    final Worksheet worksheet = workbook.worksheets[0];
    _key.currentState!.exportToExcelWorksheet(
      worksheet,
      cellExport: (details) {
        if (details.columnName == 'DOJ' &&
            details.cellType == DataGridExportCellType.row) {
          details.excelRange.numberFormat = 'y-mm-d';

          details.excelRange.value = details.cellValue;
        }
      },
    );
    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('SfDataGrid Demo'),
      ),
      body: Column(children: [
        Container(
            margin: const EdgeInsets.all(20),
            child: ElevatedButton(
                onPressed: exportDataGridToExcel,
                child: const Text('Export DataGrid to Excel'))),
        Expanded(
          child: SfDataGrid(
              source: _employeeDataSource, key: _key, columns: getColumns),
        ),
      ]),
    );
  }

About

How to apply a specific format to the DateTime value during Excel export in Flutter DataTable (SfDataGrid)?

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • CMake 48.4%
  • Dart 24.4%
  • C++ 12.6%
  • HTML 5.6%
  • Swift 5.5%
  • C 3.0%
  • Other 0.5%