|
@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 { get; set; }
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);
}
}
|