これらの Telerik ドキュメントに示されているように、カスタム セル書式設定を適用して、Excel にエクスポートするための Grid コンポーネントの組み込みサポートを使用しようとしています。
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 モデルで追加のヘッダー行/列を生成する列/行グループを操作するためのソリューションも必要です。