Back to Devexpress

Export to Tabular Formats (CSV, XLS, XLSX)

wpf-400439-controls-and-libraries-pivot-grid-printing-and-exporting-export-to-tabular-formats.md

latest17.2 KB
Original Source

Export to Tabular Formats (CSV, XLS, XLSX)

  • Jun 05, 2023
  • 7 minutes to read

The Export to tabular data formats (CSV, XLS, XLSX) feature uses the data engine the ExportType enumeration specifies. The default engine uses the Excel Export Library to perform a Data-Aware export with improved performance and reduced memory usage. The resultant file is optimized for subsequent analysis in Microsoft Excel.

Data-Aware Export

The following features are retained in XLS and XLSX documents:

  • Data Grouping - you can collapse/expand groups within a worksheet.
  • Fixed Headers - column and row areas stay in sight.
  • Cells Formatting - retains the number format.
  • Display Text/Value export - you can select whether to export display text or values.

Methods

The following table lists the methods that perform data-aware export:

MethodParameter
ExportToCsvUse the PivotGridCsvExportOptions instance instead of the CsvExportOptions object specified in the method signature. The ExportType property should be set to the DataAware value (default).
ExportToXlsUse the PivotGridXlsExportOptions instance instead of the XlsExportOptions object type specified in the method signature. The PivotXlsExportOptionsBase.ExportType (inherited DevExpress.XtraPrinting.XlsExportOptionsEx.ExportType ) property should be set to the DataAware value (default).
ExportToXlsxUse the PivotGridXlsxExportOptions instance instead of the XlsxExportOptions object type specified in the method signature. The PivotXlsxExportOptionsBase.ExportType (inherited DevExpress.XtraPrinting.XlsxExportOptionsEx.ExportType ) property should be set to the DataAware value (default).

Options

The PivotGridXlsExportOptions and PivotGridXlsxExportOptions classes inherits properties from the XlsExportOptionsEx and XlsxExportOptionsEx classes, respectively. The data-aware export engine uses only a subset of available properties. The table below lists the properties which are in effect in the Data Aware mode.

PropertyDescription
PivotGridXlsExportOptions.ExportColumnAreaHeaders, PivotGridXlsxExportOptions.ExportColumnAreaHeadersGets or sets whether to include the column field headers in the exported document.
PivotGridXlsExportOptions.ExportFilterAreaHeaders, PivotGridXlsxExportOptions.ExportFilterAreaHeadersGets or sets whether to include the filter field headers in the exported document.
PivotGridXlsExportOptions.ExportRowAreaHeaders, PivotGridXlsxExportOptions.ExportRowAreaHeadersGets or sets whether to include the row field headers in the exported document.
XlExportOptionsBase.SheetNameGets or sets a name of the sheet in the created XLS file to which a document is exported.
XlsExportOptionsEx.AllowFixedColumns, XlsxExportOptionsEx.AllowFixedColumnsGets or sets whether the row area in a Pivot Grid control becomes a frozen pane in the spreadsheet.
AllowFixedColumnHeaderPanel, AllowFixedColumnHeaderPanelGets or sets whether the column area becomes a frozen pane in the spreadsheet.
AllowGrouping, AllowGroupingGets or sets whether the data in the spreadsheet are grouped as they are displayed in the Pivot Grid control.
ShowPageTitle, ShowPageTitleGets or sets whether each printed page of the exported document displays a title.
ExportType, ExportTypeGets or sets the export mode. You can switch between WYSIWYG and data-aware export.
GroupState, GroupStateGets or sets whether groups are collapsed or expanded in the exported document.
XlExportOptionsBase.RawDataModeEnables the mode that produces simple tabular data without graphic elements, style and appearance settings.
XlExportOptionsBase.TextExportModeGets or sets a value indicating whether the cells in the resulting XLS document should use the same formatting as the original document.

Runtime Customization

Handle the following events to customize exported pivot grid elements individually. Note that these events are in effect only in the Data Aware mode.

EventDescription
XlsExportOptionsEx.AfterAddRow, XlsxExportOptionsEx.AfterAddRowFires after a row is added to the output document and allows you to merge cells, insert an image or add an extra row.
XlsExportOptionsEx.BeforeExportTable, XlsxExportOptionsEx.BeforeExportTableAllows you to customize a native Excel table’s settings before export.
XlsExportOptionsEx.CustomizeDocumentColumn, XlsExportOptionsEx.CustomizeDocumentColumnAllows you to customize an individual column in the exported document - change its width, formatting, collapse the group containing the column or hide the column.
XlsExportOptionsEx.CustomizeSheetFooter, XlsxExportOptionsEx.CustomizeSheetFooterAllows you to add a footer to the output document.
XlsExportOptionsEx.CustomizeSheetHeader, XlsxExportOptionsEx.CustomizeSheetHeaderAllows you to add a header to the output document.
XlsExportOptionsEx.CustomizeSheetSettings, XlsxExportOptionsEx.CustomizeSheetSettingsAllows you to customize the output document’s settings.
XlsExportOptionsEx.DocumentColumnFiltering, XlsxExportOptionsEx.DocumentColumnFilteringAllows you to apply filters to the exported document’s columns.
XlsExportOptionsEx.SkipFooterRow, XlsxExportOptionsEx.SkipFooterRowAllows you to hide certain summary footers (or certain multi-line summary footers’ lines) from the exported document.

WYSIWYG Export

The WYSIWYG export mode uses the XtraPrinting library engine. It reproduces control elements’ layout in XLS and XLSX formats, omitting data shaping characteristics available in the DataAware export mode.

In CSV format, the WYSIWYG mode can export data area and filter area headers, and add empty columns. When a spreadsheet loads CSV data, it looks similar to the original Pivot Grid control.

|

WYSIWYG Mode

|

Data Aware Mode

| |

|

|

The following table lists the methods that perform the export in WYSIWYG mode:

MethodParameter
ExportToCsvUse the PivotGridCsvExportOptions instance instead of the CsvExportOptions object specified in the method signature. The CsvExportOptionsEx.ExportType property should be set to the WYSIWYG value.
ExportToXlsUse the PivotGridXlsExportOptions instance instead of the XlsExportOptions object specified in the method signature. The XlsExportOptionsEx.ExportType property should be set to the WYSIWYG value.
ExportToXlsxUse the PivotGridXlsxExportOptions instance instead of the XlsxExportOptions object specified in the method signature. The XlsxExportOptionsEx.ExportType property should be set to the WYSIWYG value.

Options

The PivotXlsExportOptions and PivotXlsxExportOptions classes inherits properties from the XlsExportOptionsEx and XlsxExportOptionsEx classes, respectively. The WYSIWYG mode (the Printing Library export engine) uses only a subset of available properties. The table below lists the properties which are in effect in the WYSIWYG mode.

PropertyDescription
XlExportOptionsBase.DocumentOptionsProvides access to options to be embedded as the resulting XLS or XLSX file’s Document Properties.
XlExportOptionsBase.EncryptionOptionsProvides access to the XLS and XLSX file encryption options.
XlsExportOptions.ExportMode, XlsxExportOptions.ExportModeSpecifies whether the source is exported as a single XLSX file or multiple files, and whether each page is exported as a separate worksheet.
XlsExportOptionsEx.ExportType, XlsExportOptionsEx.ExportTypeGets or sets the export type - WYSIWYG or Data Aware.
XlExportOptionsBase.IgnoreErrorsSpecifies the document errors to be ignored in a resulting Excel file.
XlExportOptionsBase.RawDataModeEnables the mode that produces simple tabular data without graphic elements, style and appearance settings.
XlExportOptionsBase.RightToLeftDocumentGets or sets whether the layout of the resulting XLS document should be aligned to support locales using right-to-left fonts.
XlExportOptionsBase.SheetNameGets or sets a name of the sheet in the created XLS file to which a document is exported.
XlExportOptionsBase.ShowGridLinesGets or sets whether worksheet gridlines are visible in the resulting XLS file.
XlsExportOptions.Suppress256ColumnsWarningGets or sets a value indicating whether to suppress the exception that raises when trying to export a document to an XLS file with more than 256 columns.
XlsExportOptions.Suppress65536RowsWarningGets or sets a value indicating whether to suppress the exception that raises when trying to export a document to an XLS file with more than 65,536 rows.
XlExportOptionsBase.TextExportModeGets or sets a value indicating whether the cells in the resulting XLS document should use the same formatting as the original document.

Custom Export

You can customize a PivotGrid’s cell in the exported XLS, XLSX, or CSV document with the following events:

PivotGridCsvExportOptions.CustomizeCellAllows you to substitute a cell value in the output document. Only available in data-aware export mode.PivotGridXlsExportOptions.CustomizeCellAllows you to customize a PivotGrid cell in the exported XLS document. Only available in data-aware export mode.PivotGridXlsxExportOptions.CustomizeCellAllows you to customize a PivotGrid’s cell in the exported XLSX document. Only available in data-aware export mode.

The following example exports data from a PivotGrid Control to Xlsx format. The PivotGridXlsxExportOptions.CustomizeCell event is used to change the background color of the “Category Name” column in the output document.

csharp
//...
void op_CustomizeCell(CustomizePivotCellEventArgs e){
       if (e.ValueItemInfo != null && e.ValueItemInfo.Field == fieldCategoryName) {
          e.Formatting.BackColor = Color.AliceBlue;
          e.Handled = true;
      }

  }
void Button_Click(object sender, RoutedEventArgs e) {
    PivotGridXlsxExportOptions op = new PivotGridXlsxExportOptions();
    op.CustomizeCell += op_CustomizeCell;
    pivorGrid1.ExportToXlsx(file, op);
}
vb
'...
Private Sub op_CustomizeCell(ByVal e As CustomizePivotCellEventArgs)
      If e.ValueItemInfo IsNot Nothing AndAlso e.ValueItemInfo.Field = fieldCategoryName Then
          e.Formatting.BackColor = Color.AliceBlue
          e.Handled = True
      End If

End Sub
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim op As New PivotGridXlsxExportOptions()
    AddHandler op.CustomizeCell, AddressOf op_CustomizeCell
    pivorGrid1.ExportToXlsx(file, op)
End Sub