使用kendoGrid启动Excel导出(大量行)

时间:2018-04-19 13:49:01

标签: asp.net asp.net-mvc kendo-ui kendo-grid

数据加载到网格后,我需要导出到Excel '当我在100s左右的行数较少时这很好用,但现在我得到的数据恰好是12250行。这是加载到网格,但当我尝试导出到Excel '它不起作用。

这个加载了我的网格`

.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"))

        )`

' 导出到Excel '

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

注意:加载到网格工作正常。 注意:导出到Excel也适用于行数较少(约100或200)       但如果行更像是12250"导出到Excel"没有发生。

如果我能纠正某事,请告诉我。这样我就可以导出到excel了。

2 个答案:

答案 0 :(得分:3)

可以从客户端功能导出到Excel的记录(行项目)的限制因浏览器而异as Daniel said in a similar issue

  

每个浏览器都有自己的局限性,我担心在大多数情况下我们都是这样   无法控制他们。那就是说,出口大量的   导出期间的数据将生成可能太长的字符串   给定的浏览器。

因为文件内容完全在客户端构建,所以它依赖于每个浏览器存储由Spreadsheet.saveAsExcel()函数处理的格式化字符串(例如JSON)的能力。

如果要导出到包含大量记录/行(例如12,250行)的Excel文件,我建议使用Telerik.Documents.SpreadsheetStreaming命名空间来生成内存中的导出记录流,然后转换为服务器端处理它是一个字节数组,可以作为FileResult传递。下面是一个在控制器操作方法中执行服务器端处理的示例(为简洁起见,剪切了一些部分):

[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);
}

类似问题:

Export to Excel Row Limit

Excel export not working with more than a thousand records

其他参考(服务器端导出到Excel):

Large Document Export with SpreadsheetStreaming (Telerik Example)

GridExcelSpreadProcessing example

答案 1 :(得分:0)

为此找到了一个简单的解决方案。经过一些研究后才知道,如果数据量更大,那么唯一的选择就是服务器端出口实施

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.10.11.RELEASE</version>
</dependency>

**结果是您的数据集合(可能来自您的数据库)

** TableStyle:OfficeOpenXml.Table.TableStyles.Medium6 - Excel行样式

** ExcelPackage - (EPPlus)可以包含在Package config ----&gt;安装包EPPlus -Version 4.5.1