json 大型数据集上的剑道网格导出到Excel错误

ohfgkhjo  于 2023-08-08  发布在  其他
关注(0)|答案(2)|浏览(102)

我得到下面的错误时,试图导出到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);
}

字符串

inkz8wg9

inkz8wg91#

去掉“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");
    }`


请注意,我使用自定义模型,也使用this library

im9ewurl

im9ewurl2#

我希望能帮上忙。这样我就解决了问题

public virtual ActionResult Index_Read([DataSourceRequest] DataSourceRequest request, bool isActive)
        {
            return new LargeJsonResult { Data = getAssets(isActive).ToDataSourceResult(request), MaxJsonLength = int.MaxValue };

        }

public class LargeJsonResult : JsonResult
{
    public int MaxJsonLength { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        HttpResponseBase response = context.HttpContext.Response;

        if (!String.IsNullOrEmpty(ContentType))
        {
            response.ContentType = ContentType;
        }
        else
        {
            response.ContentType = "application/json";
        }

        if (ContentEncoding != null)
        {
            response.ContentEncoding = ContentEncoding;
        }

        if (Data != null)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            if (MaxJsonLength.HasValue)
            {
                serializer.MaxJsonLength = MaxJsonLength.Value;
            }
            response.Write(serializer.Serialize(Data));
        }
    }
}

字符串

相关问题