New to Telerik UI for WPF? Download free 30-day trial

Modifying GridView Exported XLSX Spreadsheet

Environment

Product Version 2019.3.1023
Product RadGridView for WPF

Description

There are several ways to export the RadGridView as a spreadsheet. This article shows you how to leverage the RadGridView's ExportToWorkbook method to make changes to the spreadsheet before saving as Excel xlsx file.

This article will show you how to add an extra row at the top of the speadsheet and insert a title.

Solution

The first step is to invoke the GridView's ExportToWorkbook method, optionally passing a GridViewDocumentExportOptions parameter.

var exportOptions = new GridViewDocumentExportOptions() 
{ 
        ShowColumnFooters = true, 
        ShowColumnHeaders = true, 
        ShowGroupFooters = true, 
    ShowGroupHeaderRowAggregates = true 
}; 
Workbook workbook = MyGridView.ExportToWorkbook(exportOptions); 
With the Workbook exported, you can now use the features of the Telerik Document Processing Libraries' RadSpreadProcessing to modify the document.

In this example, we are inserting a new row above the exported data to add a title.

// Step 1. Get the first worksheet 
Worksheet worksheet = workbook.ActiveWorksheet; 
 
// Step 2. Insert a new row at the top 
worksheet.Rows.Insert(0); 
 
// Step 3. Select the worksheet's A4 cell 
CellSelection selection = worksheet.Cells[0, 4]; 
 
// Step 4. Insert the text "GridView Export" into the selected cell 
selection.SetValue("GridView Export"); 
Finally, we can now save it as an xlsx file using the XlsxFormatProvider.

var dialog = new SaveFileDialog 
{ 
    DefaultExt = "xlsx", 
    Filter = $"Excel files (.xlsx)|.xlsx|All files (.)|.", 
    FilterIndex = 1, 
    FileName = "GridView Export" 
}; 
 
if (dialog.ShowDialog() == true) 
{ 
    using (var output = dialog.OpenFile()) 
    { 
    new XlsxFormatProvider().Export(workbook, output); 
    } 
} 

Resources

For more information, visit the following documentation articles:

In this article