我正在尝试将我的Kendo网格导出为Excel。它工作正常,但缺少格式。我认为这是因为我正在使用模板。
Telerik文档明确指出:
要在将网格导出到Excel的过程中格式化单元格值,请设置单元格的format选项。
我已经尝试过了,但它不起作用:
columns: [
{
field: "EntryWage",
headerTemplate: entryLevelWageColumnHeading + "<span name='EntryWage' class='k-icon k-i-close remove' style='float: right;'></span>",
width: 125,
attributes: { style: "text-align:right;" },
format: "{0:c}",
template: "#= (EntryWage != null) ? kendo.toString(EntryWage, 'C') : 'N/A' #"
}];
我也有此功能(用于Excel网格定义):
excelExport: function (e) {
var sheet = e.workbook.sheets[0];
var row = sheet.rows[0];
$("#grid .k-grid-header .k-link").each(function (index) { //for each column header in the grid...
row.cells[index].value = $(this).text(); //set cell text from grid column text
row.cells[index].background = "#0070C0"; //set cell to "blue" color
});
},
我需要在这里解析每个单元格吗?难道我做错了什么?我认为这将非常简单,因为整个“导出到Excel”非常简单?
我认为template
对导出的数据不会有任何影响(因为excelExport
是以为主dataSource
)。
这是一个工作示例的excelExport
jsFiddle,它更改每个的格式cell
。
注意excelExport
代码之间的区别:
excelExport: function(e) {
var sheet = e.workbook.sheets[0];
for (var rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
var row = sheet.rows[rowIndex];
for (var cellIndex = 0; cellIndex < row.cells.length; cellIndex++) {
var cell = row.cells[cellIndex];
if (row.type === "data") {
//if (cellIndex == 2) {
if (sheet.rows[0].cells[cellIndex].value == "unitPrice") {// like this
cell.format = "number";
cell.background = "#0070C0"
cell.hAlign = "right";
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句