aspnet-114650-components-pivot-grid-export-export.md
The Pivot Grid control is shipped with the ASPxPivotGridExporter component. This component allows you to export the ASPxPivotGrid’s data to a file or stream in various formats - HTML, MHT, PDF, RTF, TXT, CSV, XLS or XLSX.
Tip
Watch the video: DevExpress ASP.NET Pivot Grid: Data-Aware Export (YouTube)
Two export types are supported when exporting data from a Pivot Grid control.
By using the XLS(X) and CSV formats you can specify export type in two ways.
To export the Pivot Grid’s data, add the ASPxPivotGridExporter component, assign the pivot grid’s ID to the ASPxPivotGridExporter.ASPxPivotGridID property and call one of the following methods.
Note
If the ASPxPivotGridExporter.ASPxPivotGridID property is not set, the first determined pivot grid on a page will be exported.
|
Output Format
|
Methods
|
Data-Aware Support
|
Description
| | --- | --- | --- | --- | |
HTML
|
|
|
Exports ASPxPivotGrid data to the specified file in HTML format, using the specified character encoding.
| |
MHT
|
|
|
Exports ASPxPivotGrid data to the specified file in MHT format using the specified character encoding with the specified title. The output file can be compressed (secondary characters such as spaces are removed) if required.
| |
|
|
|
Exports ASPxPivotGrids data to the specified file in PDF format.
| |
RTF
|
|
|
Exports ASPxPivotGrid data to the specified file in RTF format.
| |
TXT
|
|
|
Exports ASPxPivotGrid data to the specified file in TXT format, using the specified separator string and encoding settings.
| |
CSV
|
|
|
Exports the ASPxPivotGrid’s data to the specified file in CSV format.
| |
XLS
|
|
|
Exports ASPxPivotGrid data to the specified file in XLS format using the specified options.
| |
XLSX
|
|
|
Exports ASPxPivotGrid data to the specified file in XLSX (MS Excel 2007) format using the specified options.
|
Additional export settings can be customized when calling the ExportToXls(x) method overloads that take an options parameter. This parameter can be set to an XlsExportOptionsEx object (when using the ExportToXls method), to an XlsxExportOptionsEx object (when using the ExportToXlsx method), etc. You can also use the ASPxPivotGridExporter.OptionsPrint property to get an access to the export options.
Note, that the ASPxPivotGridExporter component cannot export the following content.
This example shows how to export the pivot grid’s data using the Data-Aware or WYSIWYG types and set the export options.
<dx:ASPxButton ID="DataAwareExportButton" runat="server"
Text="Export to XLSX" onclick="DataAwareExportButton_Click" ToolTip="Export using the Data-Aware type."/>
<dx:ASPxButton ID="WysiwygExportButton" runat="server" Text="Export to PDF"
onclick="WysiwygExportButton_Click" ToolTip="Export using the WYSIWYG type." />
<dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" ClientIDMode="AutoID"
DataSourceID="SqlDataSource1">
<Fields>
<dx:PivotGridField ID="fieldCountry" Area="ColumnArea" AreaIndex="0"
FieldName="Country">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldCategoryName" Area="RowArea" AreaIndex="0"
Caption="Category" FieldName="CategoryName">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldProductName" Area="RowArea" AreaIndex="1"
Caption="Product" FieldName="ProductName">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldExtendedPrice" Area="DataArea" AreaIndex="0"
Caption="Extended Price" FieldName="Extended_Price">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldSalesPerson" Area="ColumnArea" AreaIndex="1"
Caption="Sales Person" FieldName="Sales_Person">
</dx:PivotGridField>
<dx:PivotGridField ID="fieldQuantity" Area="DataArea" AreaIndex="1"
FieldName="Quantity">
</dx:PivotGridField>
</Fields>
</dx:ASPxPivotGrid>
<dx:ASPxPivotGridExporter ID="ASPxPivotGridExporter1" runat="server">
</dx:ASPxPivotGridExporter>
using System;
using DevExpress.XtraPrinting;
using DevExpress.Utils;
namespace ASPPivotGridExport {
public partial class WebForm1 : System.Web.UI.Page {
protected void DataAwareExportButton_Click(object sender, EventArgs e) {
// Exports using the Data-Aware type.
ASPxPivotGridExporter1.ExportXlsxToResponse("ASPxPivotGrid", new XlsxExportOptionsEx {
AllowFixedColumns = DefaultBoolean.False,
SheetName = "Pivot Grid Export"
},
true);
}
protected void WysiwygExportButton_Click(object sender, EventArgs e) {
// Exports using the WYSIWYG type.
ASPxPivotGridExporter1.ExportPdfToResponse("ASPxPivotGrid", new PdfExportOptions() {
ShowPrintDialogOnOpen = true,
}, true);
}
}
}
Imports System
Imports DevExpress.XtraPrinting
Imports DevExpress.Utils
Namespace ASPPivotGridExport
Partial Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub DataAwareExportButton_Click(ByVal sender As Object, ByVal e As EventArgs)
' Exports using the Data-Aware type.
ASPxPivotGridExporter1.ExportXlsxToResponse("ASPxPivotGrid", New XlsxExportOptionsEx With
{.AllowFixedColumns = DefaultBoolean.False, .SheetName = "Pivot Grid Export"}, True)
End Sub
Protected Sub WysiwygExportButton_Click(ByVal sender As Object, ByVal e As EventArgs)
' Exports using the WYSIWYG type.
ASPxPivotGridExporter1.ExportPdfToResponse("ASPxPivotGrid", New PdfExportOptions() With
{.ShowPrintDialogOnOpen = True}, True)
End Sub
End Class
End Namespace
This example illustrates how to add a header to a document exported to PDF. The ASPxPivotGrid control exports its content to a PDF file, adding two lines of text to the document’s header. The same technique enables you to add custom text to the document’s footer. To change the header text, change the value of the corresponding TextBrick. The image below illustrates the resulting PDF file with custom headers:
When exporting an ASPxPivotGrid control to XLSX (or XLS) format, you can customize a cell appearance in the exported document using the PivotXlsxExportOptions.CustomizeCell (or PivotXlsExportOptions.CustomizeCell) event.
In this example, custom appearance settings (the azure background and italic font) are applied to the cells that correspond to the Pivot Grid’s data area. The CustomizePivotCellEventArgs.ExportArea property is used to identify cell location in the exported Excel document. The cell format is set by the CustomizePivotCellEventArgs.Formatting property.
using System;
using System.Drawing;
using DevExpress.Web.ASPxPivotGrid;
namespace WebPivotExportCustomizeCell
{
public partial class Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
ASPxPivotGridExporter1.ASPxPivotGridID = "ASPxPivotGrid1";
}
protected void ASPxButton1_Click(object sender, EventArgs e)
{
var exportOptions = new PivotXlsxExportOptions();
exportOptions.CustomizeCell +=
new CustomizePivotCellEventHandler(exportOptions_CustomizeCell);
ASPxPivotGridExporter1.ExportXlsxToResponse("PivotGrid", exportOptions);
}
void exportOptions_CustomizeCell(CustomizePivotCellEventArgs e)
{
if (e.ExportArea == DevExpress.XtraPivotGrid.PivotExportArea.Data) {
e.Formatting.BackColor = Color.Azure;
e.Formatting.Font.Italic = true;
}
e.Handled = true;
}
}
}
Imports System
Imports System.Drawing
Imports DevExpress.Web.ASPxPivotGrid
Namespace WebPivotExportCustomizeCell
Partial Public Class [Default]
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
ASPxPivotGridExporter1.ASPxPivotGridID = "ASPxPivotGrid1"
End Sub
Protected Sub ASPxButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim exportOptions = New PivotXlsxExportOptions()
AddHandler exportOptions.CustomizeCell, AddressOf exportOptions_CustomizeCell
ASPxPivotGridExporter1.ExportXlsxToResponse("PivotGrid", exportOptions)
End Sub
Private Sub exportOptions_CustomizeCell(ByVal e As CustomizePivotCellEventArgs)
If e.ExportArea = DevExpress.XtraPivotGrid.PivotExportArea.Data Then
e.Formatting.BackColor = Color.Azure
e.Formatting.Font.Italic = True
End If
e.Handled = True
End Sub
End Class
End Namespace
See Also