I want to specify excel export to server path in grid using custom adapter.

I am using a custom adapter.


When exporting to Excel If there are more than 1 million rows, the web page becomes unresponsive.

I want to save the excel file on the server and change it so that it can be downloaded later, please give me an example. 


3 Replies

MS Monisha Saravanan Syncfusion Team February 24, 2022 07:32 AM UTC

Hi Arcblue, 
 
Thanks for contacting Syncfusion support.  
 
Query: “ Exporting to Excel If there are more than 1 million rows, the web page becomes unresponsive 
 
We have analyzed your query and we would like to inform you that currently we don’t have direct support to store the data in server when exporting the data to excel format. 
 
Instead we can convert the Grid data to memory stream and convert it to Byte array. Here we have saved the Grid data to memory stream and you can use this memory stream in your server location.  
 
Kindly refer the below code example.  
 
@using Syncfusion.Blazor.Grids 
@using Syncfusion.XlsIO; 
@using System.IO; 
 
<SfGrid ID="Grid" @ref="DefaultGrid" DataSource="@Orders" AllowSorting="true" Toolbar="@(new List<string>() { "ExcelExport" })" AllowExcelExport="true" AllowPaging="true"> 
    <GridEvents OnToolbarClick="ToolbarClickHandler" TValue="Order"></GridEvents> 
    . . . . . .  
</SfGrid> 
 
@code{ 
    private SfGrid<Order> DefaultGrid; 
 
    public List<Order> Orders { getset; } 
 
    public async Task ToolbarClickHandler(Syncfusion.Blazor.Navigations.ClickEventArgs args) 
    { 
        if (args.Item.Id == "Grid_excelexport") //Id is combination of Grid's ID and itemname 
        { 
            using (ExcelEngine excelEngine = new ExcelEngine()) 
            { 
                IApplication application = excelEngine.Excel; 
                application.DefaultVersion = ExcelVersion.Excel2013; 
                IWorkbook workbook = application.Workbooks.Create(1); 
                IWorksheet worksheet = workbook.Worksheets[0]; 
 
                //Import the data to worksheet 
                IList<Order> reports = Orders; // pass the datasoruce  
                worksheet.ImportData(reports, 2, 1, true); 
 
                MemoryStream stream = new MemoryStream(); 
 
                //save the data in memory stream 
                workbook.SaveAs(stream); 
                //Convert the stream to array 
                byte[] array = stream.ToArray(); 
            } 
        } 
    } 
 
 
Kindly get back to us if you have further queries.  
 
Regards, 
Monisha 



AR arcblue replied to Monisha Saravanan February 25, 2022 04:50 AM UTC

When used as in the example, normal data output is impossible because it does not go through a custom adapter.

Can you provide an example using a custom adapter?




VN Vignesh Natarajan Syncfusion Team February 28, 2022 11:57 AM UTC

Hi Arcblue, 

Query: “because it does not go through a custom adapter. Can you provide an example using a custom adapter?” 

We would like to inform you that in the previous update, we have provided support to store the excel data in form of memory stream using external support, since we do not have direct support to achieve your requirement. So we suggest you to fetch the data from CustomAdaptor using the below solution.  

Refer the below code example.  

    CustomAdaptor dm1 { get; set; } = new CustomAdaptor(); 
    DataManagerRequest DMRequest = new DataManagerRequest(); 
    public async Task<List<Order>> GetCustomData() 
    { 
        var dataItems = await dm1.ReadAsync(DMRequest); 
        if (dataItems != null) 
        { 
            return dataItems as List<Order>;// dataItems; 
        } 
        else 
        { 
            return new List<Order>(); 
        } 
    } 
    public async Task ToolbarClickHandler(Syncfusion.Blazor.Navigations.ClickEventArgs args) 
    { 
        if (args.Item.Id == "Grid_excelexport") //Id is combination of Grid's ID and itemname 
        { 
            using (ExcelEngine excelEngine = new ExcelEngine()) 
            { 
                IApplication application = excelEngine.Excel; 
                application.DefaultVersion = ExcelVersion.Excel2013; 
                IWorkbook workbook = application.Workbooks.Create(1); 
                IWorksheet worksheet = workbook.Worksheets[0]; 
 
                var dataOrder = await GetCustomData(); 
                //Import the data to worksheet 
                IList<Order> reports = dataOrder; // pass the datasoruce 
                worksheet.ImportData(reports, 2, 1, true); 
 
                MemoryStream stream = new MemoryStream(); 
 
                //save the data in memory stream 
                workbook.SaveAs(stream); 
                //Convert the stream to array 
                byte[] array = stream.ToArray(); 
            } 
        } 
 
    } 
. . . .. . . . . 
 
    // Implementing custom adaptor by extending the DataAdaptor class 
    public class CustomAdaptor : DataAdaptor 
    { 
        // Performs data Read operation 
        public override async Task<object> ReadAsync(DataManagerRequest dm, string key = null) 
        { 
            IEnumerable<Order> DataSource = Orders; 
            . . . . . . 
            return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource; 
        } 
    } 
 

Externally we need to fetch the data from CustomAdaptor and send the data to external export function. Kindly refer the below sample for your reference 


Please get back to us if you have further queries or if above solution does not resolve your query.    

Regards, 
Vignesh Natarajan 


Loader.
Up arrow icon