Skip to main content

How to: Customize a Cell in the Exported Excel Document

  • 2 minutes to read

When exporting an ASPxPivotGrid control to XLSX (or XLS) format, you can customize a cell appearance in the exported document using the PivotXlsxExportOptions.CustomizeCell (or PivotXlsExportOptions.CustomizeCell) event.

In this example, custom appearance settings (the azure background and italic font) are applied to the cells that correspond to the Pivot Grid’s data area. The CustomizePivotCellEventArgs.ExportArea property is used to identify cell location in the exported Excel document. The cell format is set by the CustomizePivotCellEventArgs.Formatting property.

using System;
using System.Drawing;
using DevExpress.Web.ASPxPivotGrid;

namespace WebPivotExportCustomizeCell
{
    public partial class Default : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e)
        {
            ASPxPivotGridExporter1.ASPxPivotGridID = "ASPxPivotGrid1";
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            var exportOptions = new PivotXlsxExportOptions();
            exportOptions.CustomizeCell += 
                new CustomizePivotCellEventHandler(exportOptions_CustomizeCell);
            ASPxPivotGridExporter1.ExportXlsxToResponse("PivotGrid", exportOptions);
        }

        void exportOptions_CustomizeCell(CustomizePivotCellEventArgs e)
        {
            if (e.ExportArea == DevExpress.XtraPivotGrid.PivotExportArea.Data) {
                e.Formatting.BackColor = Color.Azure;
                e.Formatting.Font.Italic = true; 
            }
            e.Handled = true;
        }
    }
}