How to customize cell value while exporting the DataGrid into Excel and PDF in WPF?
In WPF DataGrid, 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:
Conclusion
I hope you enjoyed learning about how to customize cell value while exporting the DataGrid into Excel and PDF.
You can refer to our WPF
DataGrid feature tour page to know about
its other groundbreaking feature representations and documentation, and
how to quickly get started for configuration specifications. You can also
explore our WPF DataGrid example
to understand how to create and manipulate
data.
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!