2016-03-07 120 views
1

我试图使用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/

  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的模型工作。

+0

我不知道这个链接是否有帮助,但我不得不在官方支持之前编写我自己的解析器和导出器到excel和pdf。我认为底层网格结构中的列/行和递归分组依然存在。如果你可以解开许多年前的代码,那么你可能会发现组和列之间的递归关系。 - > https://stackoverflow.com/questions/25753051/having-difficulty-creating-an-array-of-json-objects-for-use-in-a-kendo-grid/25753400#25753400 –

回答

1

它看起来像行[0]为标题行,所以你可以尝试改变

if (cellIndex == 2) { 

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

编辑:

似乎与栏目组的工作:https://jsfiddle.net/dwosrs0x/

更新:

工作表的对象模型不是最清晰的。在我看到的各种场景中,第一行似乎是一个“主”标题行。如果unitPrice不在分组中,这似乎有效。如果unitPrice处于分组中,那么涉及组标题(行[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/

这绝对有 “bodge” 关于它的味儿。

+0

这是一个好主意,但我不认为它适用于具有列组和/或行组的网格(我承认它并未包含在原始的小提琴示例中)。给定一个列组,包括一个额外的标题行,其中包括合并/跨越的单元格,并且,关于如何计算正确的索引也没有明确的方法。给定一个行组,Excel模型中会包含一个额外的列来创建一个“空白”组列。我会用这些观点更新这个问题,因为我是在为所有配置提供解决方案之后。 – SteveChapman

+0

@SteveChapman似乎与列组一起工作(请参阅小提琴)。我不知道你是如何做排队的。也许你可以用代码来说明。 – Fruitbat

+0

看到这个例子(这是你使用的小提琴的更新):https://jsfiddle.net/dwosrs0x/2/ – SteveChapman