Export to Excel
The export feature allows you to save the data from the DataGrid component to few different formats. This article shows how to save it to Excel document formats - xlsx and xls.
Exporting the Document
The feature works with the Export
and ExportAsync
methods of RadDataGrid
. The methods allow you to provide a stream (usually pointing to a file), a document format and export options objects.
Export DataGrid to Xlsx format
string projectRootFolderPath = System.IO.Path.GetFullPath("../../../../../../", AppDomain.CurrentDomain.BaseDirectory);
using (var stream = File.Open(projectRootFolderPath + "\" + "SampleDocument.xlsx", FileMode.OpenOrCreate))
{
this.dataGrid.Export(stream, ExportFormat.Xlsx);
}
Xls
format, set the second parameter of the Export
method to ExportFormat.Xls
.
Export DataGrid to Xls format
using (var stream = File.Open(projectRootFolderPath + "\" + "SampleDocument.xls", FileMode.OpenOrCreate))
{
this.dataGrid.Export(stream, ExportFormat.Xls);
}
DataGridExportOptions
class. Read more about the available settings in the Modifying Exported Data article.
Using the DataGridExportOptions
string projectRootFolderPath = System.IO.Path.GetFullPath("../../../../../../", AppDomain.CurrentDomain.BaseDirectory);
using (var stream = File.Open(projectRootFolderPath + "\" + "SampleDocument.xlsx", FileMode.OpenOrCreate))
{
this.dataGrid.Export(stream, ExportFormat.Xlsx, new DataGridExportOptions()
{
AutoFitColumnsWidth = true,
ShowColumnFooters = true,
ShowColumnHeaders = false,
IgnoreCollapsedGroups = true
});
}
The exporting process can be customized at runtime using the
ElementExporting
andElementExported
events ofRadDataGrid
. Read more in the Modifying Exported Data article.