Grid - How to get exported excel bytes in version 18.2.0.59

Hello Syncfusion Team,

I am using your Grid component (18.2.0.59 nuget version). I started to learn the export function and it works fine with the following code:

...
ExcelExportProperties ExportProperties = new();
ExportProperties.FileName = "ExcelExport.xlsx";
ExportProperties.ExportType = ExportType.AllPages;
await Grid.ExcelExport(ExportProperties);
...

When I download the exported Excel file, I would like to get its bytes as well in order to save it to my database. Is there a way to achieve this requirement?

Thanks in advance!

Regards,
Attila

1 Reply

VN Vignesh Natarajan Syncfusion Team June 9, 2021 04:39 AM UTC

Hi Szöke, 
 
Thanks for contacting Syncfusion support.  
 
Query: “ I would like to get its bytes as well in order to save it to my database. Is there a way to achieve this requirement? 
 
We have analyzed your query and we would like to inform you that we do not have direct support to achieve your requirement. But we have achieved your requirement by manually exporting the data to memory stream and convert it to Byte array.  
 
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(); 
            } 
  
  
            ExcelExportProperties ExportProperties = new(); 
            ExportProperties.FileName = "ExcelExport.xlsx"; 
            ExportProperties.ExportType = ExportType.AllPages; 
            await DefaultGrid.ExcelExport(ExportProperties); 
        } 
    } 
 
 
Kindly download the sample from below which we have  
 
 
Please get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan  


Loader.
Up arrow icon