This is a migrated thread and some comments may be shown as answers.

Multiple RadGridView export to Excel, save once

1 Answer 294 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Arash
Top achievements
Rank 1
Veteran
Arash asked on 24 Nov 2020, 03:39 PM

Hi,

All examples, forums and online docs discuss calling GridViewSpreadExport's RunExport method on every iteration of a loop when exporting multiple RadGridViews.

This is costly as the Excel document is opened, written to and saved on every iteration.

Is there a way of avoiding having to call RunExport on every iteration?
Ideally, you'd want to setup multiple Excel sheets from RadgRidViews in memory and then issue the RunExport command outside of the loop.

Is there a mechanism to achieve this, either with GridViewSpreadExport  / SpreadExportRenderer or other...?

 

Thanks

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 26 Nov 2020, 11:46 AM

Hello, Arash,

You can not avoid calling the RunExport method on each iteration when exporting multiple grids. If you want to achieve a better performance, I would suggest you using the GridViewSpreadStreamExport in this case. The GridViewSpreadStreamExport uses the RadSpreadStreamProcessing library which allows you to create big documents (without loading the entire document in the memory) and export them. 

Spread streaming is a document processing paradigm that allows you to create big spreadsheet documents with great performance and minimal memory footprint. The key for the memory efficiency is that the spread streaming library writes the spreadsheet content directly to a stream without creating and preserving the spreadsheet document model in memory. Each time an exporter object is disposed, the set values are written into the stream. RadSpreadStreamProcessing library to create and export a large amount of data with a low memory footprint which I believe would be suitable if you export multiple grids.

It offers FileExportMode property. You can set it so that you export multiple grids in a single file each being on a separate sheet:

private void radButton1_Click(object sender, EventArgs e)
{
    foreach (Control grd in this.Controls)
    {
        if (grd is RadGridView)
        {
            GridViewSpreadStreamExport spreadStreamExport = new GridViewSpreadStreamExport(grd as RadGridView);
            spreadStreamExport.ExportVisualSettings = true;
            spreadStreamExport.FileExportMode = FileExportMode.NewSheetInExistingFile;
            SpreadStreamExportRenderer exportRenderer = new SpreadStreamExportRenderer();
            spreadStreamExport.RunExport(@"D:\StreamExport.xlsx", exportRenderer);
        }
    }
}

An alternative approach is to create a workbook on your own, iterate all the grids and populate the workbook with the data from the grids programmatically. Once all grids are traversed and the workbook is filled with data, you can export it to an Excel file. The following blogs are quite useful on this topic:

https://www.telerik.com/blogs/getting-started-with-radspreadprocessing-volume-1 

https://www.telerik.com/blogs/getting-started-with-radspreadprocessing-vol2 

https://www.telerik.com/blogs/getting-started-with-radspreadprocessing-vol3 

I hope this information is useful. If you have other questions please let me know.

Regards,
Nadya
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/.

Tags
GridView
Asked by
Arash
Top achievements
Rank 1
Veteran
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or