How to customize cell value while exporting the SfDataGrid into Excel and PDF ?
In SfDataGrid, you can customize the cell value while exporting it into Excel and PDF by using CellsExportingEventHandler with ExcelExportingOptions and PdfExportingOptions class.
The cell value is displayed in SfDataGrid like below,
Figure 1: SfDataGrid view
Excel Exporting:
The above cell value of IsClosed (GridCheckBoxColumn) column is customized while exporting to Excel like below,
C#:
var options = new ExcelExportingOptions(); // Assign the customized cellsExportingHandler to ExcelExportingOptions options.CellsExportingEventHandler = cellsExportingHandler; private void cellsExportingHandler(object sender, GridCellExcelExportingEventArgs e) { // Based on the column mapping name and the cell type, we can change the cell values while exporting to excel. if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "IsClosed") { //if the cell value is True, "Y" will be displayed else "N" will be displayed. if (e.CellValue.Equals(true)) e.Range.Cells[0].Value = "Y"; else e.Range.Cells[0].Value = "N"; e.Handled = true; } }
The cell value of IsClosed column has been customized in Exported Excel Sheet like below,
Figure 2: Excel View
PDF Exporting:
The above cell value of IsClosed (GridCheckBoxColumn) column is customized while exporting to PDF like below,
C#:
var options = new PdfExportingOptions(); // Assign the customized cellsExportingHandler to PdfExportingOptions options.CellsExportingEventHandler = cellsExportingEventHandler; private void cellsExportingEventHandler(object sender, GridCellPdfExportingEventArgs e) { // Based on the column mapping name and the cell type, we can change the cell values when exporting to PDF. if(e.CellType==ExportCellType.RecordCell&&e.ColumnName=="IsClosed") { //if the cell value is True, "Y" will be displayed else "N" will be displyed. if (e.CellValue.Equals("True")) e.CellValue= "Y"; else e.CellValue = "N"; } }
The cell value of IsClosed column has been customized in Exported PDF document like below,
Figure 3: PDF View
Sample Links: