Category / Section
How to Export custom formatting with Pivot Grid Control
1 min read
We have implemented an event called “QueryExportCellInfo” to achieve this requirement. You could have to hook that event and set the style (background color, foreground color, etc) on the exported XlsIO object (e) based on the other passed arguments (PivotCellInfo, rowIndex, ColumnIndex and ExcelCellInfo). Please refer the below workaround which illustrates the same
C#
Hooking the event and handle the same at Sample: private void Export_Click(object sender, RoutedEventArgs e) { SaveFileDialog savedialog = new SaveFileDialog(); savedialog.AddExtension = true; savedialog.FileName = "Sample"; savedialog.DefaultExt = "xlsx"; savedialog.Filter = "Excel file (.xlsx)|*.xlsx"; if (savedialog.ShowDialog() == true) { GridExcelExport excelExport = new GridExcelExport(Pivot, Syncfusion.XlsIO.ExcelVersion.Excel2007, ExportModes.Cell); excelExport.QueryExportCellInfo += new QueryExportCellInfoEventHandler(excelExport_QueryExportCellInfo); excelExport.Export(savedialog.FileName); MessageBox.Show ("Excel sheet exported successfully!."); } } void excelExport_QueryExportCellInfo(object sender, ExportingToExcelEventArgs e) { if (((e.PivotCellInfo.CellType & PivotCellType.ColumnHeaderCell) != 0) && ((e.PivotCellInfo.CellType & PivotCellType.TotalCell) == 0) && ((e.PivotCellInfo.CellType & PivotCellType.GrandTotalCell) == 0)) { e.ExcelCellInfo.CellStyle.Color = System.Drawing.Color.Pink; } if (((e.PivotCellInfo.CellType & PivotCellType.RowHeaderCell) != 0) && ((e.PivotCellInfo.CellType & PivotCellType.TotalCell) == 0) && ((e.PivotCellInfo.CellType & PivotCellType.GrandTotalCell) == 0)) { e.ExcelCellInfo.CellStyle.Color = System.Drawing.Color.Red; } else if (e.PivotCellInfo.CellType == PivotCellType.ValueCell) { e.ExcelCellInfo.CellStyle.Color = System.Drawing.Color.Orange; } }