Built-in Export
- 4 minutes to read
Note
Supported Versions The information in this topic applies to DevExpress ASP.NET Grid View versions 17.2 and later.
ASPxGridView provides the following tools for implementing data export: toolbar commands, context menu items, client-side and server-side API.
Note
The built-in toolbar/context menu export commands and client-side API are disabled by default due to possible security issues. To enable this functionality, set the ASPxGridExportSettings.EnableClientSideExportAPI property to true.
Toolbar Commands
The ASPxGridView toolbar provides the following export commands.
- GridViewToolbarCommand.ExportToPdf
- GridViewToolbarCommand.ExportToRtf
- GridViewToolbarCommand.ExportToCsv
- GridViewToolbarCommand.ExportToXls
- GridViewToolbarCommand.ExportToXlsx
- GridViewToolbarCommand.ExportToDocx
Use the GridViewToolbarItem.Command property to specify a toolbar item’s export command.
Refer to the Toolbars topic or Toolbar online demo for more information on grid toolbars.
<dx:ASPxGridView ID="Grid" runat="server" KeyFieldName="ProductID" DataSourceID="ProductsDataSource" Width="100%">
<Toolbars>
<dx:GridViewToolbar EnableAdaptivity="true">
<Items>
<dx:GridViewToolbarItem Command="ExportToXls" />
<dx:GridViewToolbarItem Command="ExportToXlsx" />
<dx:GridViewToolbarItem Command="ExportToCsv" />
</Items>
</dx:GridViewToolbar>
</Toolbars>
<Columns>
...
</Columns>
</dx:ASPxGridView>
Context Menu Items
ASPxGridView provides the context menu’s Export submenu with the following items. Set the GridViewExportMenuItemVisibility.Visible property to true to display the Export submenu.
Item | Item’s Visibility Member | Command Name |
---|---|---|
Export to PDF | GridViewExportMenuItemVisibility.ExportToPdf | GridViewContextMenuCommand.ExportToPdf |
Export to RTF | GridViewExportMenuItemVisibility.ExportToRtf | GridViewContextMenuCommand.ExportToRtf |
Export to DOCX | GridViewExportMenuItemVisibility.ExportToDocx | GridViewContextMenuCommand.ExportToDocx |
Export to CSV | GridViewExportMenuItemVisibility.ExportToCsv | GridViewContextMenuCommand.ExportToCsv |
Export to XLS | GridViewExportMenuItemVisibility.ExportToXls | GridViewContextMenuCommand.ExportToXls |
Export to XLSX | GridViewExportMenuItemVisibility.ExportToXlsx | GridViewContextMenuCommand.ExportToXlsx |
Refer to the Context Menu topic or Context Menu online demo for more information on the grid’s context menu.
Export API
Execute a client or server export method (see the Member Table: Built-in Export) to implement grid data export.
The following code samples illustrate how to use client and server export methods.
Client API
<dx:ASPxGridView ClientInstanceName="grid" ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" KeyFieldName="ProductID"> <SettingsExport EnableClientSideExportAPI="true" /> <Columns> ... </Columns> </dx:ASPxGridView> <br /> <dx:ASPxButton ID="ASPxButton1" runat="server" AutoPostBack="False" Text="Export"> <ClientSideEvents Click="function(s, e) { grid.ExportTo(ASPxClientGridViewExportFormat.Xls); }" /> </dx:ASPxButton>
Server API
switch(e.Item.Name) { case "CustomExportToXLS": grid.ExportXlsToResponse(new DevExpress.XtraPrinting.XlsExportOptionsEx { ExportType = ExportType.WYSIWYG }); break; case "CustomExportToXLSX": grid.ExportXlsxToResponse(new DevExpress.XtraPrinting.XlsxExportOptionsEx { ExportType = ExportType.WYSIWYG }); break; default: break; }
The ASPxGridViewDetailSettings object provides the following export settings:
Property | Description |
---|---|
Specifies whether you can expand several master rows simultaneously. | |
Specifies the detail view’s position among other views in a printed document. | |
Specifies whether the grid export mode. | |
Specifies whether the grid displays detail buttons. | |
Specifies whether the grid displays detail rows. |
You can customize exported grid element appearance with the ASPxGridView.StylesExport property.
Note
The ASPxGridView cannot export data columns that are hidden or export template content. If the grid is in a GridViewTemplates.DetailRow template, ASPxGridView allows you to export both master and detail data.
Note that templated elements are exported with their default representation. For instance, if you export a grid with templated cells, they are represented as default labels with text in the obtained file.
Excel Data Aware Export
When you export data in XLS and XLSX formats, the following grid data is maintained in the resulting excel document.
- Data grouping with the ability to collapse/expand groups within a worksheet.
- Data sorting and filtering.
- Totals and group summaries with the ability to modify/change formulas.
- Format conditions with the ability to modify/change conditional formatting rules.
- Data validation for combo-box columns.
- Fixed columns.
See the following Knowledge Base Article for more information: ASPxGridView / MVC GridView Extension - Excel Data Aware Export FAQ.
Export Limitations
- When you export data to Excel formats, the ASPxGridViewExportSettings.Landscape and ASPxGridViewExportSettings.PaperKind properties are not in effect.
- When you export data to Excel formats, rich text formatting is ignored in ASPxGridViewExportSettings.ReportHeader and ASPxGridViewExportSettings.ReportFooter property values.
- The ASPxGridView control does not interpret HTML tags in data cells. Use the ExportRenderBrick (WYSIWYG export mode) and XlsxExportOptionsEx.CustomizeCell (DataAware export mode) events to customize exported documents that contains HTML tags.
Data Aware Mode Limitations
- The ASPxGridView.StylesExport property has no effect. Use either the GridViewColumn.ExportCellStyle at the column level or the XlsExportOptionsEx.CustomizeCell / XlsxExportOptionsEx.CustomizeCell event instead.
- The ASPxGridViewExportSettings.ReportHeader and ASPxGridViewExportSettings.ReportFooter properties are not in effect.
- The ASPxGridView.ExportRenderBrick event does not fire. You can use the XlsExportOptionsEx.CustomizeCell or XlsxExportOptionsEx.CustomizeCell event instead.
- The master-detail grid export is not supported.
Use the WYSIWYG (What You See Is What You Get) export mode to avoid the specified limitations. Note, however, table formatting is lost in this mode.
ASPxGridView1.ExportXlsxToResponse(new XlsxExportOptionsEx { ExportType = ExportType.WYSIWYG });
Online Demos
Exporting to Different Formats