Kendo网格打印所有的数据,而不仅仅是可见的数据

比方说,我有80个项目的网格,页面大小是10,从控制器打印时,我想打印所有的数据,而不仅仅是第一页上的可见数据。

我有很好的Telerik的“ Export Grid to Excel ”testing项目,而且我的导出function全部被覆盖,像魅力一样工作。 基本上只包括NPOI文件并开始使用它。

有没有一种方法可以迭代DataSourceRequest中的所有产品数据?

我的代码示例:

public FileResult Export([DataSourceRequest]DataSourceRequest request) { //Get the data representing the current grid state - page, sort and filter IEnumerable products = db.Products.ToDataSourceResult(request).Data; //TODO: Get all data but not just the data from the visible page as above!!! //Create new Excel workbook 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); int rowNumber = 1; //Populate the sheet with values from the grid data foreach (Product product in products) { //Create a new row var row = sheet.CreateRow(rowNumber++); //Set values for the cells row.CreateCell(0).SetCellValue(product.ProductID); row.CreateCell(1).SetCellValue(product.ProductName); row.CreateCell(2).SetCellValue(product.UnitPrice.ToString()); row.CreateCell(3).SetCellValue(product.QuantityPerUnit.ToString()); } //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"); //Suggested file name in the "Save as" dialog which will be displayed to the end user } 

DataSourceRequest类的来源可以在这里find。

可能如果你禁用分页属性,你会得到所有过滤+sorting的数据:

 public FileResult Export([DataSourceRequest]DataSourceRequest request) { request.Take = 9999999; request.Skip = 0; // Get the data representing the current grid state : sort and filter IEnumerable products = db.Products.ToDataSourceResult(request).Data; 

你可以使用javascript来打印所有的数据,如下所示

 function ExportToCSV() { var dataSource = $("#grid").data("kendoGrid").dataSource; var filteredDataSource = new kendo.data.DataSource({ data: dataSource.data(), filter: dataSource.filter() }); filteredDataSource.read(); var data = filteredDataSource.view(); var result = "data:application/vnd.ms-excel,"; result += "<table><tr><th>ProductID</th><th>ProductName</th><th>UnitPrice</th><th>Discontinued</th><th>UnitsInStock</th><th>Category</th></tr>"; for (var i = 0; i < data.length; i++) { result += "<tr>"; result += "<td>"; result += data[i].ProductID; result += "</td>"; result += "<td>"; result += data[i].ProductName; result += "</td>"; .. result += "</tr>"; } result += "</table>"; if (window.navigator.msSaveBlob) { window.navigator.msSaveBlob(new Blob([result]), 'export.xls'); } else { window.open(result); } e.preventDefault(); } 

希望这可能有所帮助

过了一段时间,我偶然发现了一个可行的答案。 @Stef答案让我走上了正轨,尽pipe我没有真正使用他的答案,所以我会提出他的答案。 我find了一个计算页数的方法,然后简单地编辑每个页面的DataSourceRequest。 这样可以确保数据库中的所有页面。 我希望这可以帮助别人在未来:)

 public FileResult Export([DataSourceRequest]DataSourceRequest request) { //Count pages to use as iterator when adding to list var pages = db.Products.ToDataSourceResult(request).Total/request.PageSize; //Get the data representing the current grid state - page, sort and filter //IEnumerable products = db.Products.ToDataSourceResult(request).Data; //Get the data representing the current grid state - page, sort and filter var products = new List<Product>(); //To ensure all pages get fetched from db for (int i = 1; i < pages + 1; i++) { request.Page = i; IEnumerable prod = db.Products.ToDataSourceResult(request).Data; products.AddRange(prod.Cast<Product>().ToList()); } //Create new Excel workbook 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); int rowNumber = 1; //Populate the sheet with values from the grid data foreach (Product product in products) { //Create a new row var row = sheet.CreateRow(rowNumber++); //Set values for the cells row.CreateCell(0).SetCellValue(product.ProductID); row.CreateCell(1).SetCellValue(product.ProductName); row.CreateCell(2).SetCellValue(product.UnitPrice.ToString()); row.CreateCell(3).SetCellValue(product.QuantityPerUnit.ToString()); } //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"); //Suggested file name in the "Save as" dialog which will be displayed to the end user }