Initiates the Excel export (Large number of rows) using kendoGrid

假装没事ソ 提交于 2021-01-28 09:41:17

问题


After the data get loaded in the Grid , I have a requirement to 'Export to Excel' This works perfectly when i have lesser number of rows around 100s but now I am getting data which is exactly 12250 rows. This is getting loaded to the Grid but when I try to 'Export to Excel' its not working.

This load's my grid`

.Columns(column =>
            {
                column.Bound(p => p.CBCustomerName).Title("CB Customers");
                column.Bound(p => p.CBReceiveDateTimeUTC).Title("CB Date").Format("{0:MM/dd/yyyy}");
                column.Bound(p => p.CBExpirationDate).Title("CBExpiration Date").Format("{0:MM/dd/yyyy HH:mm:ss}");               

            })
            //.Events(e => e.DataBound("onDataBound"))
            .Sortable()
            .Scrollable()
            .Filterable()
            .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(5))
            .DataSource(dataSource => dataSource
            .Ajax()
            .Group(x=> { x.Add(p => p.ReceiveDateTimeUTC);})
            .PageSize(20)
            .Events(events => events.Error("error_handler"))
            .Read(read => read.Action("uspPendingWork", "PendingWork").Data("getGridData"))

        )`

'Export to Excel'

 $(document).ready(function () {
            $("#ExportXL").on('click', function () {
                var grid = $("#PendingWorkGrid").data("kendoGrid");
                grid.saveAsExcel();
                $("#SearchBT").click();
            })
        });

NOTE: Loading to Grid is working properly. NOTE: Export to Excel also works when rows are less(around 100 or 200) But If rows are more like 12250 "Export to Excel" is not happening.

Please let me if I can correct something. So that I will be able to export to excel.


回答1:


The limit of records (row items) that can be exported to Excel from client-side function varies between browsers, as Daniel said in a similar issue:

Every browser has its own limitations and I'm afraid that in most cases we don't have control over them. That said, exporting large amounts of data during export will generate strings that might be too long for the given browser.

Because file contents constructed entirely in client-side, it depends in each browser ability to store formatted strings (e.g. JSON) that processed by Spreadsheet.saveAsExcel() function.

If you want exporting to Excel file with large amount of records/rows (say 12,250 rows), I recommend using server-side processing by utilizing Telerik.Documents.SpreadsheetStreaming namespace to generate in-memory stream of exported records, then convert it to a byte array which can be passed as FileResult. Here is an example to perform server-side processing in controller action method (some parts snipped for brevity):

[HttpPost]
public ActionResult ExportToExcel(int rowsCount, string fileName)
{
    // column header names, separated by comma
    string[] columnHeaderNames = { "CBCustomerName", "CBReceiveDateTimeUTC", "CBExpirationDate" };

    // column width values, separated by comma    
    double[] columnWidths = { 30, 14.5, 14.5 };

    SpreadDocumentFormat exportFormat = SpreadDocumentFormat.Xlsx;
    string mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    string ext = "xlsx";

    string fileNameWithExt = string.Format("{0}.{1}", fileName, ext);

    // byte array to hold MemoryStream result
    byte[] rendered = null;

    using (var stream = new MemoryStream())
    {
        using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(exportFormat, stream))
        {
            using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("WorksheetName"))
            {
                for (int i = 0; i < columnWidths.Length; i++)
                {
                    using (IColumnExporter columnExport = worksheet.CreateColumnExporter())
                    {
                        // export column settings here
                    }
                }

                // other stuff

                for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++)
                {
                    using (IRowExporter rowExport = worksheet.CreateRowExporter())
                    {
                        // export row settings here
                    }
                }

                // other stuff
            }
        }

        rendered = stream.ToArray();
    }

    return File(rendered, mimeType, fileNameWithExt);
}

Similar issues:

Export to Excel Row Limit

Excel export not working with more than a thousand records

Additional references (server-side export to Excel):

Large Document Export with SpreadsheetStreaming (Telerik Example)

GridExcelSpreadProcessing example




回答2:


Found out a simple solution for this. After some research came to know that if data size is more then the only option is Server Side Export Implementation

<button id="ExportXL" name="ExportXL" type="button" value="Export to Excel" onclick="location.href='@Url.Action("ExportToExcel", "Controler Name")'" />



public ActionResult ExportToExcel()
    {
        using (var excel = new ExcelPackage())
        {
            var workSheet = excel.Workbook.Worksheets.Add("Worksheet Name");
            workSheet.Cells[1, 1].LoadFromCollection(result, PrintHeaders: true, TableStyle: OfficeOpenXml.Table.TableStyles.Medium6);
            workSheet.Cells[workSheet.Dimension.Address].AutoFitColumns();
            return File(excel.GetAsByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Reports.xlsx");
        }

    }

** result is your collection of data (Which you might have from your DB)

** TableStyle: OfficeOpenXml.Table.TableStyles.Medium6 - Excel row Style

** ExcelPackage - (EPPlus) Can include on Package config ----> Install-Package EPPlus -Version 4.5.1



来源:https://stackoverflow.com/questions/49922780/initiates-the-excel-export-large-number-of-rows-using-kendogrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!