Planned
Last Updated: 18 Nov 2021 06:36 by ADMIN
Scheduled for 3.1.0
License
Created on: 28 May 2020 13:47
Category: Grid
Type: Feature Request
49
Include all pages in Excel export should work with OnRead

With OnRead, AllPages cannot be exported because the grid Data only has the current page: https://docs.telerik.com/blazor-ui/components/grid/export/excel#notes:

If you are using the OnRead event, only the current page of data will be exported, because that's all the grid has at the time of the export action.

 

I load my data via "OnRead", because i need to implement pagination by myself, and I would like the export option to work with that too.

5 comments
Matt
Posted on: 29 Jul 2021 16:03

Here's my workaround for this. If somebody really needs a full example I'm sure I can whip up a quick razor page, or maybe Marin can take this and build a full example. But this should be enough for anybody to work off of.

First thing I do is get the datagrid's current state.  Then I build a DataSourceRequest off of that.  For the page size, I set it to the total number of records that exist.  I can then send that request off to the same service that I am using to populate the data grid.  I can then build my own excel document using the library of my choice, and I'm sure this can easily be done with Telerik Document Processing library.  I'm using SpreadSheetLight (https://spreadsheetlight.com/)

var state = this.dataGridRef.GetState();
DataSourceRequest req = new();
req.Filters = new List<IFilterDescriptor>(state.FilterDescriptors.OfType<IFilterDescriptor>());
req.Sorts = new List<SortDescriptor>(state.SortDescriptors);
req.PageSize = this.TotalRecords;

DataSourceResult dsr = await this.dataService.GetSomeData(req);
var dataToExport = dsr.Data as IEnumerable<SomeDataModel>();

// Build your excel export using the library of your choice
SLDocument excelDoc = new();
int row = 1;
foreach(var record in dataToExport)
{
    excelDoc.SetCellValue(row, 1, record.Value1);
    excelDoc.SetCellValue(row, 2, record.Value2);
    row++;
}

// You need to then convert the excel to a Base64String needed to download the file
// The SLDocument.SaveAs method accepts a stream as a destination so I'm writing to a memory stream instead of to a file

string b64ExportData;
using(MemoryStream outStream = new())
{
    excelDoc.SaveAs(outStream);
    var exportRaw = outStream.ToArray();
    b64ExportData = Convert.ToBase64String(exportRaw);
}

// Download the extract once you have the base 64 string. 
// I'm using this javascript function found here which I found here:
//  https://github.com/telerik/blazor-ui/blob/master/grid/pdf-export-server/ServerSideSample/wwwroot/saveFile.js 
await this.JSRuntine.InvokeVoidAsync("saveFile", b64ExportData, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "DataGridExport.xlsx");

 

ADMIN
Marin Bratanov
Posted on: 27 Jul 2021 05:12

Hello Meindert,

The only workaround I can offer is generating the export entirely in the application code, you can find a sample here: https://feedback.telerik.com/blazor/1485764-customize-the-excel-file-before-it-gets-to-the-client

Regards,
Marin Bratanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Meindert
Posted on: 26 Jul 2021 10:17
Would be functional if this item is solved/implemented. Is there a workaround?
Ian
Posted on: 04 Jun 2021 13:03

I agree, this would be really useful.

Also useful if you could set a MAX number of records, this could ensure the export is not too big.

Umakant
Posted on: 21 May 2021 07:01
Its a very useful feature. Request Telerik to work on it.