@using Syncfusion.XlsIO;
@using System.IO;
@inject IJSRuntime Runtime;
<SfButton OnClick="CsvExport" Content="CSV Export"></SfButton>
<SfGrid @ref="DefaultGrid" DataSource="@Orders" AllowSorting="true" AllowExcelExport="true" AllowPaging="true">
. .. .. . . . .
</SfGrid>
@code{
private SfGrid<Order> DefaultGrid;
public List<Order> Orders { get; set; }
public async void CsvExport()
{
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 to csvexport
worksheet.ImportData(reports, 2, 1, true);
MemoryStream stream = new MemoryStream();
workbook.SaveAs(stream,";"); // save the workbook with different delimiter
//to download the memory stream as file
await Runtime.InvokeVoidAsync("exportSave", new object[] { "export.csv", Convert.ToBase64String(stream.ToArray()) });
}
}
}
[export.js]
|