假设我有80个项目的网格,页面大小为10,从控制器打印时我想打印所有数据,而不仅仅是第一页上的可见数据。
我有来自Telerik的好的“Export Grid to Excel”测试项目,我的导出功能全部覆盖并且像魅力一样工作。基本上只包括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
}
答案 0 :(得分:2)
可以找到DataSourceRequest类的源here。
如果您禁用分页属性,您可能会获得所有已过滤的+已排序的数据:
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;
答案 1 :(得分:1)
过了一段时间,我偶然发现了一个有效的答案。 @Stef的回答让我走上了正确的轨道,虽然我实际上没有使用他的答案,但我会提出他的答案。我找到了计算页数的方法,然后只为每个页面编辑了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
}
答案 2 :(得分:0)
您可以使用javascript将所有数据打印到excel,如下所示
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();
}
希望这可能会有所帮助