我试图使用Grid组件的内置导出到Excel,应用自定义单元格的格式如图这些Telerik的文档支持:剑道UI格 - Excel导出隐藏列和自定义格式
http://docs.telerik.com/kendo-ui/controls/data-management/grid/how-to/excel/cell-format
在出口使用硬编码的行/细胞指数的方法带有一个比较明显的问题,出口显示与先前隐藏的列网格时 - 重现最好的办法就是参考这个的jsfiddle:
https://jsfiddle.net/3anqpnqt/1/
- 运行小提琴
- 点击导出到Excel - 观察格式化
- 取消隐藏子类别列(使用列菜单)
- 单击导出到excel自定义数字 - 观察自定义数字第2栏的格式也就是现在的“子类别“
参考该代码在小提琴:
$("#grid").kendoGrid({
toolbar: ["excel"],
excel: {
fileName: "Grid.xlsx",
filterable: true
},
columns: [
{ field: "productName" },
{ field: "category" },
{ field: "subcategory", hidden: true },
{ field: "unitPrice"}
],
dataSource: [
{ productName: "Tea", category: "Beverages", subcategory: "Bev1", unitPrice: 1.5 },
{ productName: "Coffee", category: "Beverages", subcategory: "Bev2", unitPrice: 5.332 },
{ productName: "Ham", category: "Food", subcategory: "Food1", unitPrice: -2.3455 },
{ productName: "Bread", category: "Food", subcategory: "Food2", unitPrice: 6 }
],
columnMenu: true,
excelExport: function(e) {
var sheet = e.workbook.sheets[0];
for (var rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
var row = sheet.rows[rowIndex];
var numericFormat = "#,##0.00;[Red](#,##0.00);-";
for (var cellIndex = 0; cellIndex < row.cells.length; cellIndex++) {
var cell = row.cells[cellIndex];
if (row.type === "data") {
if (cellIndex == 2) { // how are we able to identify the column without using indexes?
cell.format = numericFormat;
cell.hAlign = "right";
}
}
}
}
}
});
我需要做的是将单元格标识为'unitPrice'并应用该格式,但在excelExport
处理程序中检查对象模型并没有给出任何方法来创建此链接。在我的真实应用程序中,我有几种自定义格式可以应用(百分比,n0,n2等),所以它不如$.isNumeric(cell.value)
或其他。
更新
我还需要解决与行/列组,产生额外的标题行/列在Excel的模型工作。
我不知道这个链接是否有帮助,但我不得不在官方支持之前编写我自己的解析器和导出器到excel和pdf。我认为底层网格结构中的列/行和递归分组依然存在。如果你可以解开许多年前的代码,那么你可能会发现组和列之间的递归关系。 - > https://stackoverflow.com/questions/25753051/having-difficulty-creating-an-array-of-json-objects-for-use-in-a-kendo-grid/25753400#25753400 –