c# - 大型数据集上的 Kendo Grid Export to Excel 错误
问题描述
尝试从 Kendo 大数据网格导出到 Excel 时出现以下错误。当我们在网格上有小数据时,它工作正常。
使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串长度超过 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);
}
解决方案
删除“Excel”部分并添加这个
.ToolBar(toolBar => { toolBar.Custom().Text("Export To Excel")
.HtmlAttributes(new { @class = "export" })
.Url(Url.Action("ExcelExport", "Grid")); })
然后,在控制器中:
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");
}`
请注意,我使用我的自定义模型和这个库
推荐阅读
- python - 访问列表中的条目
- ios - Codename One(或 ParparVM)如何将 Java 库编译成 iOS 库
- python - Python:当一个类在另一个类中使用时,是否可以覆盖/扩展一个类的实例方法?
- ruby-on-rails - 加密的出生日期未保存在 sinatra/rails 的数据库中
- r - Groupby 并选择最后 10 条记录并变异为 R 中的条件
- python - 使用 ipyvolume 在 jupyter 中绘制 2D 网格
- flutter - Flutter 报错“Cannot access the body fields of a Request”
- sql - 为 group by 中的每个组创建列
- ontology - 如何在 WebProtege 4.0 中列出用户?
- python - 使用 pipenv 安装枕头时出现 Pipfile.Lock 问题