Skip to content

abineshPalanisamy/how-to-customize-column-header-text-during-Excel-and-PDF-export-in-Flutter-DataTable

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to customize column header text during Excel and PDF export in Flutter DataTable (SfDataGrid)

This article explains how to customize the column header when exporting a DataGrid to an Excel or PDF document.

STEP 1:

The DataGridToPdfConverter and DataGridToExcelConverter classes provide the functionality to customize the column header while exporting a DataGrid to an Excel or PDF document. This can be achieved by using the exportColumnHeader method according to your requirements. To modify the column name, you can access the actual column header text from the GridColumn.label property and assign it to the corresponding column header while exporting.

class CustomDataGridToPdfConverter extends DataGridToPdfConverter {
  @override
  void exportColumnHeader(SfDataGrid dataGrid, GridColumn column,
      String columnName, PdfGrid pdfGrid) {
    var label = columnName;
       // Based on the widget tree structure, you can access the value from the GridColumn.label property.
    //For example, if the label property of the GridColumn contains a Container widget, 
    //you need to locate the text widget within it and then retrieve the value stored in its data property
    if (column.label is Container) {
      label = ((column.label as Container).child as Text).data!;
    } else if (column.label is Text) {
      label = (column.label as Text).data!;
    } else if (column.label is Center) {
      label = ((column.label as Center).child as Text).data!;
    }
    super.exportColumnHeader(dataGrid, column, label, pdfGrid);
  }
}

class CustomDataGridToExcelConverter extends DataGridToExcelConverter {
  @override
  void exportColumnHeader(SfDataGrid dataGrid, GridColumn column,
      String columnName, Worksheet worksheet) {
    var label = columnName;
    if (column.label is Container) {
      label = ((column.label as Container).child as Text).data!;
    } else if (column.label is Text) {
      label = (column.label as Text).data!;
    } else if (column.label is Center) {
      label = ((column.label as Center).child as Text).data!;
    }
    super.exportColumnHeader(dataGrid, column, label, worksheet);
  }
}

STEP 2:

The exportDataGridToExcel and exportDataGridToPDF methods are used to export SfDataGrid to Excel and PDF formats, respectively.

Future<void> exportDataGridToExcel() async {
  final Workbook workbook =
      key.currentState!.exportToExcelWorkbook(converter: excelConverter);
  final List<int> bytes = workbook.saveAsStream();

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

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

About

How to customize column header text during Excel and PDF export in Flutter DataTable (SfDataGrid)?

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 39.0%
  • CMake 32.2%
  • Dart 19.7%
  • HTML 3.2%
  • Swift 3.2%
  • C 2.4%
  • Other 0.3%