Back to Devexpress

Data-Aware Export

wpf-10018-controls-and-libraries-data-grid-printing-and-exporting-data-aware-export.md

latest10.3 KB
Original Source

Data-Aware Export

  • Oct 01, 2024
  • 4 minutes to read

Run Demo: Data-Aware Export

The data-aware export is the default export mode for the XLS , XLSX , and CSV formats. This mode allows you to transfer data shaping operations (filters, groups, conditional formatting, etc.) into a document. The data-aware mode provides improved performance and memory usage compared to WYSIWYG mode.

Note

The CardView does not support the data-aware export.

Supported Data Shaping Features

The data-aware export mode retains the following grid data shaping features in the exported documents:

Export Data

The following code sample exports the GridControl to the Xls file:

csharp
void Button_Click_Export(object sender, RoutedEventArgs e) {
    view.ExportToXls(@"c:\Example\grid_export.xls");
}
vb
Private Sub Button_Click_Export(ByVal sender As Object, ByVal e As RoutedEventArgs)
    view.ExportToXls("c:\Example\grid_export.xls")
End Sub

The following methods export GridControl data in the data-aware mode:

MethodDescription
TableView.ExportToCsv, TreeListView.ExportToCsvExports a grid to the specified stream in CSV format.
TableView.ExportToXls, TreeListView.ExportToXlsExports a grid to the specified file path in XLS format.
TableView.ExportToXlsx, TreeListView.ExportToXlsxExports a grid to the specified stream in XLSX format.

The BaseColumn.AllowPrinting property specifies whether to export the column.

Customize Appearance

Note

The exported GridControl ignores the DataViewBase.CellTemplate and regular Template/Style properties. Refer to the Format Cell Values topic for information on what properties affect the data-aware export mode.

The following code sample changes the Birthday column’s width and the background color of odd rows in the exported document:

csharp
void Button_Click_Export(object sender, RoutedEventArgs e) {
    XlsExportOptionsEx options = new XlsExportOptionsEx();
    options.CustomizeCell += Options_CustomizeCell;
    options.CustomizeDocumentColumn += Options_CustomizeDocumentColumn;
    view.ExportToXls(@"c:\Example\grid_export.xls", options);
}
void Options_CustomizeCell(CustomizeCellEventArgs e) {
    if (e.DocumentRow % 2 != 0)
        e.Formatting.BackColor = System.Drawing.Color.PeachPuff;
    e.Handled = true;
}
void Options_CustomizeDocumentColumn(CustomizeDocumentColumnEventArgs e) {
    if (e.ColumnFieldName == "Birthday")
        e.DocumentColumn.WidthInPixels = 300;
}
vb
Private Sub Button_Click_Export(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim options As XlsExportOptionsEx = New XlsExportOptionsEx()
    options.CustomizeCell += AddressOf Options_CustomizeCell
    options.CustomizeDocumentColumn += AddressOf Options_CustomizeDocumentColumn
    view.ExportToXls("c:\Example\grid_export.xls", options)
End Sub

Private Sub Options_CustomizeCell(ByVal e As CustomizeCellEventArgs)
    If e.DocumentRow Mod 2 <> 0 Then e.Formatting.BackColor = System.Drawing.Color.PeachPuff
    e.Handled = True
End Sub

Private Sub Options_CustomizeDocumentColumn(ByVal e As CustomizeDocumentColumnEventArgs)
    If e.ColumnFieldName = "Birthday" Then e.DocumentColumn.WidthInPixels = 300
End Sub

To customize the resulting document in the data-aware export mode, use members of the XlsxExportOptionsEx, XlsExportOptionsEx, or CsvExportOptionsEx classes.

View Example: Add Page Headers and Footers to Exported Data

EventDescription
XlsxExportOptionsEx.CustomizeCell, XlsExportOptionsEx.CustomizeCell, CsvExportOptionsEx.CustomizeCellAllows you to customize a cell in the output document.
XlsxExportOptionsEx.CustomizeDocumentColumn, XlsExportOptionsEx.CustomizeDocumentColumnAllows you to customize an individual column in the output document (for example, change its width, formatting, collapse the group containing the column or hide the column).
XlsxExportOptionsEx.CustomizeSheetFooter, XlsExportOptionsEx.CustomizeSheetFooterAllows you to add a footer to the output document.
XlsxExportOptionsEx.CustomizeSheetHeader, XlsExportOptionsEx.CustomizeSheetHeaderAllows you to add a header to the output document.
XlsxExportOptionsEx.CustomizeSheetSettings, XlsExportOptionsEx.CustomizeSheetSettingsAllows you to customize the output document’s settings.

Limitations

Refer to the following help topic for more information: Excel Export Specifications and Limits.

See Also

Export the GridControl to a Native Excel Table