Hi Dan,
When exporting RadGrid to Xlsx (Open XML) format, the RadGrid uses the Telerik Document Processing library which has its own documentation.
Telerik Document Processing. To modify the output of the exported file you can use the InfrastructureExporting event of the RadGrid, access the structure created by the grid and use the APIs from the
RadSpreadProcessing to modify it.
For example:
protected
void
RadGrid1_InfrastructureExporting(
object
sender, GridInfrastructureExportingEventArgs e)
{
// using directives to shorten the code.
// using ExportStructure = Telerik.Web.UI.ExportInfrastructure;
// using XLSX = Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx;
// using WB = Telerik.Windows.Documents.Spreadsheet.Model;
// Structure generated by RadGrid
ExportStructure.ExportStructure structure = e.ExportStructure;
// Render the structure to XLSX format and return it to a byte array
ExportStructure.XlsxRenderer renderer =
new
ExportStructure.XlsxRenderer(structure);
byte
[] xlsxByteArray = renderer.Render();
// create an format provider instance
XLSX.XlsxFormatProvider formatProvider =
new
XLSX.XlsxFormatProvider();
// import the newly created document into a Workbook
WB.Workbook myWorkbook = formatProvider.Import(xlsxByteArray);
// Modify the Workbook using the Documents Processing APIs
// once finished with changes convert the workbook back to byte array
byte
[] data = formatProvider.Export(myWorkbook);
// return the byte array for downloading.
Response.ContentType =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
;
Response.Headers.Remove(
"Content-Disposition"
);
Response.AppendHeader(
"Content-Disposition"
,
string
.Format(
"attachment; filename={0}.xlsx"
, RadGrid1.ExportSettings.FileName));
Response.BinaryWrite(data);
Response.End();
}
I hope this will prove helpful.
Kind regards,
Attila Antal
Progress Telerik
Get
quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers.
Learn More.