Exporting to PDF, XLS, XLSX, DOCX, CSV and RTF
The Grid View control allows you to export data with ease. Data can be exported to a file or stream in the following formats:
Note that in this demo, export to table formats (XLS, XLSX, and CSV) is performed in the WYSIWYG (What You See Is What You Get) mode. Therefore, the table formatting, e.g., grouping, is lost and grid summaries are converted to strings. However, you can maintain such information by exporting grid data in the data-aware mode.
The buttons in this demo are not built-in to the GridView control and export operations are performed on the server-side button's "Click" handler. You can also use the built-in GridView Toolbar which contains commands for the Export operations.
Export To:
Drag a column header here to group by that column
Chai | Beverages | Exotic Liquids | 10 boxes x 20 bags | $18.00 |
Chang | Beverages | Exotic Liquids | 24 - 12 oz bottles | $19.00 |
Aniseed Syrup | Condiments | Exotic Liquids | 12 - 550 ml bottles | $10.00 |
Chef Anton's Cajun Seasoning | Condiments | New Orleans Cajun Delights | 48 - 6 oz jars | $22.00 |
Chef Anton's Gumbo Mix | Condiments | New Orleans Cajun Delights | 36 boxes | $21.00 |
Grandma's Boysenberry Spread | Condiments | Grandma Kelly's Homestead | 12 - 8 oz jars | $25.00 |
Uncle Bob's Organic Dried Pears | Produce | Grandma Kelly's Homestead | 12 - 1 lb pkgs. | $30.00 |
Northwoods Cranberry Sauce | Condiments | Grandma Kelly's Homestead | 12 - 12 oz jars | $40.00 |
Mishi Kobe Niku | Meat/Poultry | Tokyo Traders | 18 - 500 g pkgs. | $97.00 |
Ikura | Seafood | Tokyo Traders | 12 - 200 ml jars | $31.00 |
<dx:BootstrapButton runat="server" Text="PDF" ID="ButtonPDF1" OnClick="ButtonPDF1_Click">
<CssClasses Icon="fa fa-file-pdf" />
</dx:BootstrapButton>
BeginCollapse
<dx:BootstrapButton runat="server" Text="XLS" ID="ButtonXLS1" OnClick="ButtonXLS1_Click">
<CssClasses Icon="far fa-file-excel" />
</dx:BootstrapButton>
<dx:BootstrapButton runat="server" Text="XLSX" ID="ButtonXLSX1" OnClick="ButtonXLSX1_Click">
<CssClasses Icon="far fa-file-excel" />
</dx:BootstrapButton>
<dx:BootstrapButton runat="server" Text="DOCX" ID="ButtonDOCX1" OnClick="ButtonDOCX1_Click">
<CssClasses Icon="far fa-file-alt" />
</dx:BootstrapButton>
<dx:BootstrapButton runat="server" Text="CSV" ID="ButtonCSV1" OnClick="ButtonCSV1_Click">
<CssClasses Icon="far fa-file-alt" />
</dx:BootstrapButton>
<dx:BootstrapButton runat="server" Text="RTF" ID="ButtonRTF1" OnClick="ButtonRTF1_Click">
<CssClasses Icon="far fa-file-word" />
</dx:BootstrapButton>
<dx:BootstrapGridView ID="GridViewExport" runat="server" DataSourceID="DataSource1">
<Settings ShowGroupPanel="True" />
<Columns>
<dx:BootstrapGridViewDataColumn FieldName="ProductName" />
<dx:BootstrapGridViewDataColumn FieldName="Category" />
<dx:BootstrapGridViewDataColumn FieldName="Supplier" />
<dx:BootstrapGridViewDataColumn FieldName="QuantityPerUnit" />
<dx:BootstrapGridViewTextColumn FieldName="UnitPrice">
<PropertiesTextEdit DisplayFormatString="c" />
</dx:BootstrapGridViewTextColumn>
</Columns>
</dx:BootstrapGridView>
protected void ButtonPDF1_Click(object sender, EventArgs e) {
GridViewExport.ExportPdfToResponse();
}
protected void ButtonXLS1_Click(object sender, EventArgs e) {
GridViewExport.ExportXlsToResponse(new XlsExportOptions());
}
protected void ButtonXLSX1_Click(object sender, EventArgs e) {
GridViewExport.ExportXlsxToResponse(new XlsxExportOptions());
}
protected void ButtonCSV1_Click(object sender, EventArgs e) {
GridViewExport.ExportCsvToResponse(new CsvExportOptions());
}
protected void ButtonDOCX1_Click(object sender, EventArgs e) {
GridViewExport.ExportDocxToResponse();
}
protected void ButtonRTF1_Click(object sender, EventArgs e) {
GridViewExport.ExportRtfToResponse();
}
Data Aware Export
The Grid View can export data to XLS and XLSX formats in the data-aware mode. In this mode, the DevExpress Export Engine maintains data grouping layout and automatically transforms summary items to corresponding Excel functions.
| Ship Country: Austria (Continued on the next page) |
| 10351 | 11/11/2014 | Ernst Handel | 11/20/2014 12:00:00 AM | Graz | $5,445.00 |
| 10353 | 11/13/2014 | Piccolo und mehr | 11/25/2014 12:00:00 AM | Salzburg | $8,603.00 |
| 10382 | 12/13/2014 | Ernst Handel | 12/16/2014 12:00:00 AM | Graz | $2,900.00 |
| 10402 | 1/2/2015 | Ernst Handel | 1/10/2015 12:00:00 AM | Graz | $2,695.00 |
| 10430 | 1/30/2015 | Ernst Handel | 2/3/2015 12:00:00 AM | Graz | $4,880.00 |
| 10514 | 4/22/2015 | Ernst Handel | 5/16/2015 12:00:00 AM | Graz | $8,648.00 |
| 10530 | 5/8/2015 | Piccolo und mehr | 5/12/2015 12:00:00 AM | Salzburg | $4,190.00 |
| 10595 | 7/10/2015 | Ernst Handel | 7/14/2015 12:00:00 AM | Graz | $4,770.00 |
| 10633 | 8/15/2015 | Ernst Handel | 8/18/2015 12:00:00 AM | Graz | $5,483.00 |
| 10698 | 10/9/2015 | Ernst Handel | 10/17/2015 12:00:00 AM | Graz | $3,436.00 |
| 10776 | 12/15/2015 | Ernst Handel | 12/18/2015 12:00:00 AM | Graz | $6,656.00 |
<dx:BootstrapGridView ID="GridViewExportDataAware" runat="server" DataSourceID="DataSource2">
<Columns>
<dx:BootstrapGridViewTextColumn FieldName="OrderID" />
<dx:BootstrapGridViewDateColumn FieldName="OrderDate" />
<dx:BootstrapGridViewTextColumn FieldName="ShipCountry" GroupIndex="0" />
<dx:BootstrapGridViewTextColumn FieldName="CompanyName" />
<dx:BootstrapGridViewTextColumn FieldName="ShippedDate" />
<dx:BootstrapGridViewTextColumn FieldName="ShipCity" />
<dx:BootstrapGridViewTextColumn FieldName="SaleAmount">
<PropertiesTextEdit DisplayFormatString="c" />
</dx:BootstrapGridViewTextColumn>
</Columns>
<Settings ShowGroupPanel="True" ShowFooter="True" ShowGroupFooter="VisibleIfExpanded" />
<GroupSummary>
<dx:ASPxSummaryItem FieldName="OrderID" SummaryType="Count" ShowInGroupFooterColumn="OrderDate" />
<dx:ASPxSummaryItem FieldName="SaleAmount" SummaryType="Sum" ShowInGroupFooterColumn="SaleAmount" />
</GroupSummary>
<TotalSummary>
<dx:ASPxSummaryItem FieldName="OrderID" SummaryType="Count" ShowInColumn="OrderDate" />
<dx:ASPxSummaryItem FieldName="SaleAmount" SummaryType="Sum" />
</TotalSummary>
<SettingsPager PageSize="12" NumericButtonCount="7"></SettingsPager>
</dx:BootstrapGridView>
protected void ButtonXLS2_Click(object sender, EventArgs e) {
GridViewExportDataAware.ExportXlsToResponse(new XlsExportOptionsEx() { ExportType = ExportType.DataAware });
}
protected void ButtonXLSX2_Click(object sender, EventArgs e) {
GridViewExportDataAware.ExportXlsxToResponse(new XlsxExportOptionsEx() { ExportType = ExportType.DataAware });
}
Export Selected Records
To export only the currently selected grid rows, set the Grid View's SettingsExport.ExportSelectedRowsOnly property to true.
Export To:
<dx:BootstrapGridView ID="GridViewExportSelectedRecords" runat="server" DataSourceID="DataSource3" AutoGenerateColumns="False" KeyFieldName="ProductID">
<SettingsExport ExportSelectedRowsOnly="true"></SettingsExport>
<Columns>
<dx:BootstrapGridViewCommandColumn SelectAllCheckboxMode="AllPages" ShowSelectCheckbox="true"></dx:BootstrapGridViewCommandColumn>
<dx:BootstrapGridViewDataColumn FieldName="ProductName" />
<dx:BootstrapGridViewDataColumn FieldName="Category" />
<dx:BootstrapGridViewDataColumn FieldName="Supplier" />
<dx:BootstrapGridViewDataColumn FieldName="QuantityPerUnit" />
<dx:BootstrapGridViewTextColumn FieldName="UnitPrice">
<PropertiesTextEdit DisplayFormatString="c" />
</dx:BootstrapGridViewTextColumn>
</Columns>
<SettingsPager NumericButtonCount="7"></SettingsPager>
</dx:BootstrapGridView>
protected void ButtonRTF3_Click(object sender, EventArgs e) {
GridViewExportSelectedRecords.ExportRtfToResponse();
}
protected void ButtonCSV3_Click(object sender, EventArgs e) {
GridViewExportSelectedRecords.ExportCsvToResponse();
}
protected void ButtonXLSX3_Click(object sender, EventArgs e) {
GridViewExportSelectedRecords.ExportXlsxToResponse();
}
protected void ButtonXLS3_Click(object sender, EventArgs e) {
GridViewExportSelectedRecords.ExportXlsToResponse();
}
protected void ButtonPDF3_Click(object sender, EventArgs e) {
GridViewExportSelectedRecords.ExportPdfToResponse();
}
When data is exported to a PDF or RTF file, the Grid View maintains the style settings applied to the cells (with the exception of images). Moreover, when we export data to Excel formats (XLS and XLSX), the format conditions are transformed to corresponding Excel conditional formatting rules. Therefore, you can manage the rules in Excel after export.
Export To:
Vins et alcools Chevalier | $14.00 | 0% | 12 | $168.00 |
Vins et alcools Chevalier | $10.00 | 0% | 10 | $100.00 |
Vins et alcools Chevalier | $35.00 | 0% | 5 | $175.00 |
Toms Spezialitäten | $19.00 | 0% | 9 | $171.00 |
Toms Spezialitäten | $42.00 | 0% | 40 | $1,680.00 |
Hanari Carnes | $8.00 | 0% | 10 | $80.00 |
Hanari Carnes | $42.00 | 15% | 35 | $1,249.50 |
Hanari Carnes | $17.00 | 15% | 15 | $216.75 |
Victuailles en stock | $17.00 | 5% | 6 | $96.90 |
Victuailles en stock | $16.00 | 5% | 15 | $228.00 |
<dx:BootstrapGridView ID="GridViewExportWithFormatConditions" runat="server" DataSourceID="DataSource4" KeyFieldName="OrderID;ProductID">
<Columns>
<dx:BootstrapGridViewDataColumn FieldName="CompanyName" Width="260px" />
<dx:BootstrapGridViewTextColumn FieldName="UnitPrice">
<PropertiesTextEdit DisplayFormatString="c" />
</dx:BootstrapGridViewTextColumn>
<dx:BootstrapGridViewSpinEditColumn FieldName="Discount" Settings-FilterMode="Value">
<PropertiesSpinEdit DisplayFormatString="p0" />
</dx:BootstrapGridViewSpinEditColumn>
<dx:BootstrapGridViewDataColumn FieldName="Quantity" />
<dx:BootstrapGridViewTextColumn FieldName="Total" UnboundType="Decimal" UnboundExpression="UnitPrice * Quantity * (1 - Discount)">
<PropertiesTextEdit DisplayFormatString="c" />
</dx:BootstrapGridViewTextColumn>
</Columns>
<FormatConditions>
<dx:GridViewFormatConditionTopBottom FieldName="UnitPrice" Rule="AboveAverage" Format="ItalicText" />
<dx:GridViewFormatConditionTopBottom FieldName="UnitPrice" Rule="AboveAverage" Format="RedText" />
<dx:GridViewFormatConditionTopBottom FieldName="Discount" Rule="TopItems" Threshold="15" Format="BoldText" />
<dx:GridViewFormatConditionHighlight FieldName="Discount" Expression="[Discount] > 0" Format="GreenFillWithDarkGreenText" />
<dx:GridViewFormatConditionColorScale FieldName="Quantity" Format="GreenWhite" />
<dx:GridViewFormatConditionIconSet FieldName="Quantity" Format="Ratings4" />
<dx:GridViewFormatConditionTopBottom FieldName="Total" Rule="TopPercent" Threshold="20" Format="Custom">
<CellStyle Font-Bold="true" ForeColor="#9c0006" />
</dx:GridViewFormatConditionTopBottom>
</FormatConditions>
</dx:BootstrapGridView>
protected void ButtonPDF4_Click(object sender, EventArgs e) {
GridViewExportWithFormatConditions.ExportPdfToResponse();
}
protected void ButtonXLS4_Click(object sender, EventArgs e) {
GridViewExportWithFormatConditions.ExportXlsToResponse();
}
protected void ButtonXLSX4_Click(object sender, EventArgs e) {
GridViewExportWithFormatConditions.ExportXlsxToResponse();
}
protected void ButtonRTF4_Click(object sender, EventArgs e) {
GridViewExportWithFormatConditions.ExportRtfToResponse();
}