Grid View for MVC - How to export a colored grid in Data Aware export mode
This example shows how to color the exported grid when Data Aware export mode is used.
The grid does not export cell styles applied in the HtmlDataCellPrepared event.
To color the exported grid in Data Aware export mode, handle the XlsxExportOptionsEx.CustomizeCell event.
public ActionResult ExportTo() {
XlsxExportOptionsEx exportOptions = new XlsxExportOptionsEx();
exportOptions.CustomizeCell += new DevExpress.Export.CustomizeCellEventHandler(exportOptions_CustomizeCell);
return GridViewExtension.ExportToXlsx(GridViewHelper.ExportGridViewSettings, MyModel.GetProducts(), exportOptions);
}
void exportOptions_CustomizeCell(DevExpress.Export.CustomizeCellEventArgs ea) {
if(ea.AreaType != DevExpress.Export.SheetAreaType.Header && ea.ColumnFieldName == "UnitPrice") {
if(Convert.ToDecimal(ea.Value) > 15)
ea.Formatting.BackColor = System.Drawing.Color.Yellow;
else
ea.Formatting.BackColor = System.Drawing.Color.Green;
ea.Handled = true;
}
}
Note
Starting from v15.2, the grid maintains conditional formatting styles in the exported document. You can use the GridViewSettings.FormatConditions rules to define conditional formatting in browse mode and keep the applied appearance in the exported document.
Files to Review
- HomeController.cs (VB: HomeController.vb)
- Model.cs (VB: Model.vb)
- GridViewPartialProducts.cshtml
- Index.cshtml