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.
|
@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();
}
}
}
|
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?
|
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;
}
} |