简体   繁体   English

Kendo Grid在大型数据集上导出到Excel错误

[英]Kendo Grid Export to Excel error on large dataset

I am getting the below error when trying to export to Excel from a Kendo grid of large data. 尝试从Kendo大数据网格导出到Excel时出现以下错误。 It's working fine when we have small data on grid. 当我们在网格上有少量数据时,它工作正常。

Error during serialization or deserialization using the JSON JavaScriptSerializer. 使用JSON JavaScriptSerializer进行序列化或反序列化时出错。 The length of the string exceeds the value set on the maxJsonLength property 字符串的长度超过在maxJsonLength属性上设置的值

.Excel(excel => excel
    .FileName("Trip List Export.xlsx")
    .Filterable(false)
    .ProxyURL(Url.Action("ExcelExport", "Grid"))
    .AllPages(true)
)

public ActionResult ExcelExport(string contentType, string base64, string fileName)
{
    var fileContents = Convert.FromBase64String(base64);
    return File(fileContents, contentType, fileName);
}

Remove the "Excel" part and add this 删除“ Excel”部分并添加

.ToolBar(toolBar => { toolBar.Custom().Text("Export To Excel")
            .HtmlAttributes(new { @class = "export" })
            .Url(Url.Action("ExcelExport", "Grid")); })

then , in controller: 然后,在控制器中:

    public ActionResult ExcelExport([DataSourceRequest]DataSourceRequest request 
    { 

        var workbook = new HSSFWorkbook();

        //Create new Excel sheet
        var sheet = workbook.CreateSheet();

        //(Optional) set the width of the columns
        sheet.SetColumnWidth(0, 10 * 256);
        sheet.SetColumnWidth(1, 50 * 256);
        sheet.SetColumnWidth(2, 50 * 256);
        sheet.SetColumnWidth(3, 50 * 256);

        //Create a header row
        var headerRow = sheet.CreateRow(0);

        //Set the column names in the header row
        headerRow.CreateCell(0).SetCellValue("Product ID");
        headerRow.CreateCell(1).SetCellValue("Product Name");
        headerRow.CreateCell(2).SetCellValue("Unit Price");
        headerRow.CreateCell(3).SetCellValue("Quantity Per Unit");

        //(Optional) freeze the header row so it is not scrolled
        sheet.CreateFreezePane(0, 1, 0, 1);
        var models = _priceRepository.Get().ToDataSourceResult(request).Data;
        int rowNumber = 1;

        //Populate the sheet with values from the grid data
        foreach (RentPriceModel model in models)
        {
            //Create a new row
            var row = sheet.CreateRow(rowNumber++);

            //Set values for the cells
            row.CreateCell(0).SetCellValue(model.Id);
            row.CreateCell(1).SetCellValue(model.Branch.Name);
            row.CreateCell(2).SetCellValue(model.CarClass.Name);
            row.CreateCell(3).SetCellValue(model.Interval);
        }

        //Write the workbook to a memory stream
        MemoryStream output = new MemoryStream();
        workbook.Write(output);

        //Return the result to the end user

        return File(output.ToArray(),   //The binary data of the XLS file
            "application/vnd.ms-excel", //MIME type of Excel files
            "GridExcelExport.xls");
    }`

Note that I use my custom models and also this library 请注意,我使用我的自定义模型以及该库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM