1

これらの Telerik ドキュメントに示されているように、カスタム セル書式設定を適用して、Excel にエクスポートするための Grid コンポーネントの組み込みサポートを使用しようとしています。

http://docs.telerik.com/kendo-ui/controls/data-management/grid/how-to/excel/cell-format

エクスポートでハードコーディングされた行/セル インデックスを使用するアプローチには、以前の非表示の列が表示されたグリッドをエクスポートするときにかなり明白な問題があります。再現する最善の方法は、この jsfiddle を参照することです。

https://jsfiddle.net/3anqpnqt/1/

  1. フィドルを実行
  2. [Excel にエクスポート] をクリックします - カスタムの数値形式を確認します
  3. サブカテゴリ列を再表示 (列メニューを使用)
  4. [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 モデルで追加のヘッダー行/列を生成する列/行グループを操作するためのソリューションも必要です。

4

1 に答える 1

1

row[0] がヘッダー行のように見えるので、変更してみてください

if (cellIndex == 2) {

if (sheet.rows[0].cells[cellIndex].value == "unitPrice") { 

編集:

列グループで動作するようです: https://jsfiddle.net/dwosrs0x/

アップデート:

ワークシートのオブジェクト モデルは明確ではありません。最初の行は、私が調べたさまざまなシナリオの「マスター」ヘッダー行のようです。unitPrice がグループ化されていない場合に機能するように見えるものを次に示します。unitPrice がグループ化されている場合、グループ ヘッダー (row[1]) を含むより複雑な処理が可能になる可能性があります。パズルは、目的の列が最終的にどの位置を占めるかを見つけることです。

var header = sheet.rows[0];
var upIndex = -1;
var upFound = false;

for (var cellIndex = 0; cellIndex < header.cells.length; cellIndex++) {

    if ('colSpan' in header.cells[cellIndex]) 
        upIndex = upIndex + header.cells[cellIndex].colSpan;
    else 
        upIndex = upIndex + 1;

    if (header.cells[cellIndex].value == "unitPrice") { // wot we want
        upFound = true;
        break;
    }
}

for (var rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
    var row = sheet.rows[rowIndex];
    if (row.type === "data" && upFound) {
        var cell = row.cells[upIndex];
        cell.format = numericFormat;
        cell.hAlign = "right";
    }

}

グループをいじる - https://jsfiddle.net/dwosrs0x/4/

単純なグリッドをいじる (まだ機能していることを証明するため) - https://jsfiddle.net/gde4nr0y/1/

これは間違いなく「ボッジ」の匂いがします。

于 2016-03-07T13:36:10.537 に答える