Back to Devexpress

XlsxExportOptionsEx.CustomizeCell Event

corelibraries-devexpress-dot-xtraprinting-dot-xlsxexportoptionsex-c8322d39.md

latest8.5 KB
Original Source

XlsxExportOptionsEx.CustomizeCell Event

Allows you to customize a cell in the output document. Only available in data-aware export mode.

Namespace : DevExpress.XtraPrinting

Assembly : DevExpress.Printing.v25.2.Core.dll

NuGet Package : DevExpress.Printing.Core

Declaration

csharp
public event CustomizeCellEventHandler CustomizeCell
vb
Public Event CustomizeCell As CustomizeCellEventHandler

Remarks

To learn about the functionality provided by this event, see XlsExportOptionsEx.CustomizeCell.

Example

This example uses the XlsxExportOptionsEx.CustomizeCell event to replace values in the Discontinued column in an XLSX document (a result of data exporting from a Grid Control) with special symbols. The ColumnName event parameter allows recognizing the desired column. The Value parameter is utilized to substitute certain cell values. The Handled parameter is set to true to apply the changes made.

Note

A complete sample project is available at https://github.com/DevExpress-Examples/winforms-grid-customize-data-aware-export-output

csharp
// Specify the value alignment for Discontinued field.
XlCellAlignment aligmentForDiscontinuedColumn = new XlCellAlignment() {
    HorizontalAlignment = XlHorizontalAlignment.Center,
    VerticalAlignment = XlVerticalAlignment.Center
};

void options_CustomizeCell(CustomizeCellEventArgs e){
    // Substitute Boolean values within the Discontinued column by special symbols.
    if(e.ColumnFieldName == "Discontinued"){
        if(e.Value is bool){
            e.Handled = true;
            e.Formatting.Alignment = aligmentForDiscontinuedColumn;
            e.Value = ((bool) e.Value) ? "☑" : "☐";
        }
    }
}
vb
' Specify the value alignment for Discontinued field.
Private aligmentForDiscontinuedColumn As New XlCellAlignment() With {.HorizontalAlignment = XlHorizontalAlignment.Center, .VerticalAlignment = XlVerticalAlignment.Center}

Private Sub options_CustomizeCell(ByVal e As CustomizeCellEventArgs)
    ' Substitute Boolean values within the Discontinued column by special symbols.
    If e.ColumnFieldName = "Discontinued" Then
        If TypeOf e.Value Is Boolean Then
            e.Handled = True
            e.Formatting.Alignment = aligmentForDiscontinuedColumn
            e.Value = If(CBool(e.Value), "☑", "☐")
        End If
    End If
End Sub

Example

The following example exports data from a GridControl to XLS format. The XlsExportOptionsEx.CustomizeCell event is used to change the background of the grid control’s City column in the output document, and to provide hyperlinks for this column’s cells. Note that the Handled event parameter is set to true to apply the changes made.

csharp
using DevExpress.XtraPrinting;

// Ensure that the data-aware export mode is enabled.
DevExpress.Export.ExportSettings.DefaultExportType = DevExpress.Export.ExportType.DataAware;

private void button1_Click(object sender, EventArgs e) {
    string file = "c:\\work\\grid-export.xls";
    XlsExportOptionsEx op = new XlsExportOptionsEx();
    op.CustomizeCell += op_CustomizeCell;
    gridView1.ExportToXls(file, op);
    System.Diagnostics.Process.Start(file);
}

void op_CustomizeCell(DevExpress.Export.CustomizeCellEventArgs ea) {
    if (ea.ColumnFieldName == "City") {
        ea.Hyperlink = "https://www.google.com/search?q=" + ea.Value.ToString();
        ea.Formatting.BackColor = Color.Pink;
        ea.Handled = true;
    }
}
vb
Imports DevExpress.XtraPrinting

' Ensure that the data-aware export mode is enabled.
DevExpress.Export.ExportSettings.DefaultExportType = DevExpress.Export.ExportType.DataAware

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim file As String = "c:\work\grid-export.xls"
    Dim op As New XlsExportOptionsEx()
    AddHandler op.CustomizeCell, AddressOf op_CustomizeCell
    GridView1.ExportToXls(file, op)
    System.Diagnostics.Process.Start(file)
End Sub

Private Sub op_CustomizeCell(ea As DevExpress.Export.CustomizeCellEventArgs)
    If ea.ColumnFieldName = "City" Then
        ea.Hyperlink = "https://www.google.com/search?q=" + ea.Value.ToString()
        ea.Formatting.BackColor = Color.Pink
        ea.Handled = True
    End If
End Sub

The following code snippets (auto-collected from DevExpress Examples) contain references to the CustomizeCell event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-grid-customize-data-aware-export-output/CS/GridDataAwareExportCustomization/Form1.cs#L49

csharp
options.CustomizeSheetHeader += options_CustomizeSheetHeader;
options.CustomizeCell += options_CustomizeCell;
options.CustomizeSheetFooter += options_CustomizeSheetFooter;

asp-net-mvc-grid-export-colored-grid-in-data-aware-mode/CS/Controllers/HomeController.cs#L24

csharp
XlsxExportOptionsEx exportOptions = new XlsxExportOptionsEx();
exportOptions.CustomizeCell += new DevExpress.Export.CustomizeCellEventHandler(exportOptions_CustomizeCell);
return GridViewExtension.ExportToXlsx(GridViewHelper.ExportGridViewSettings, MyModel.GetProducts(), exportOptions);

winforms-grid-customize-data-aware-export-output/VB/GridDataAwareExportCustomization/Form1.vb#L52

vb
AddHandler options.CustomizeSheetHeader, AddressOf options_CustomizeSheetHeader
AddHandler options.CustomizeCell, AddressOf options_CustomizeCell
AddHandler options.CustomizeSheetFooter, AddressOf options_CustomizeSheetFooter

asp-net-mvc-grid-export-colored-grid-in-data-aware-mode/VB/Controllers/HomeController.vb#L25

vb
Dim exportOptions As New XlsxExportOptionsEx()
AddHandler exportOptions.CustomizeCell, AddressOf exportOptions_CustomizeCell
Return GridViewExtension.ExportToXlsx(GridViewHelper.ExportGridViewSettings, MyModel.GetProducts(), exportOptions)

See Also

CustomizeCell

CustomizeSheetHeader

CustomizeSheetFooter

CustomizeSheetSettings

AfterAddRow

XlsxExportOptionsEx Class

XlsxExportOptionsEx Members

DevExpress.XtraPrinting Namespace