Kendo ui Kendo UI网格-Excel导出,带隐藏列和自定义格式

Kendo ui Kendo UI网格-Excel导出,带隐藏列和自定义格式,kendo-ui,kendo-grid,kendo-ui-grid,Kendo Ui,Kendo Grid,Kendo Ui Grid,我试图使用网格组件的内置支持导出到excel,应用自定义单元格格式,如以下Telerik文档所示: 在导出显示先前隐藏列的网格时,在导出中使用硬编码行/单元格索引的方法会带来一个相当明显的问题-最好的复制方法是参考以下JSFIDLE: 拉小提琴 单击“导出到excel-遵守自定义数字格式” 取消隐藏子类别列(使用列菜单) 单击导出到excel-观察第2列上的自定义数字格式,该列现在为“子类别” 关于小提琴中的此代码: $("#grid").kendoGrid({ toolbar: [

我试图使用网格组件的内置支持导出到excel,应用自定义单元格格式,如以下Telerik文档所示:

在导出显示先前隐藏列的网格时,在导出中使用硬编码行/单元格索引的方法会带来一个相当明显的问题-最好的复制方法是参考以下JSFIDLE:

  • 拉小提琴
  • 单击“导出到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";
                    }
                }
            }      
          }      
        }
    });
    
    $(“#网格”).kendoGrid({
    工具栏:[“excel”],
    卓越:{
    文件名:“Grid.xlsx”,
    可过滤:真
    },
    栏目:[
    {字段:“productName”},
    {字段:“类别”},
    {字段:“子类别”,隐藏:true},
    {字段:“单价”}
    ],
    数据源:[
    {产品名称:“茶”,类别:“饮料”,子类别:“Bev1”,单价:1.5},
    {productName:“咖啡”,类别:“饮料”,子类别:“Bev2”,单价:5.332},
    {productName:“火腿”,类别:“食品”,子类别:“食品1”,单价:-2.3455},
    {productName:“面包”,类别:“食品”,子类别:“食品2”,单价:6}
    ],
    专栏菜单:是的,
    excelExport:函数(e){
    var sheet=e.workbook.sheets[0];
    对于(var rowIndex=0;rowIndex
    我需要能够做的是将单元格标识为“单价”并应用格式,但是检查
    excelExport
    处理程序中的对象模型并不能让我创建此链接。在我的实际应用程序中,我有几个自定义格式要应用(百分比、n0、n2等),因此它不像使用
    $.isNumeric(cell.value)
    或其他格式那样简单

    更新


    我还需要解决方案来处理列/行组,这些组在Excel模型中生成额外的标题行/列。

    看起来行[0]是标题行,因此您可以尝试更改

    if (cellIndex == 2) {
    

    编辑:

    似乎与列组一起工作:

    更新:

    工作表的对象模型不是最清晰的。在我所看到的各种场景中,第一行似乎是一个“主”标题行。如果单价不在一个分组中,这里有一些似乎有效的方法。如果单价在一个分组中,那么可能会出现涉及组标题(行[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";
        }
    
    }
    
    var header=sheet.rows[0];
    var-upIndex=-1;
    var-upFound=false;
    对于(var-cellIndex=0;cellIndex
    摆弄团体-

    摆弄简单的网格(以证明它仍然有效)——


    这无疑带有一点“bodge”的味道。

    这是一个好主意,但我认为它不适用于具有列组和/或行组的网格(我承认在最初的fiddle示例中没有包括这些)。给定一个列组,会包含一个额外的标题行,其中包含合并/跨区单元格,同样,对于如何计算正确的索引没有明确的方法。给定一个行组,excel模型中将包含一个附加列以创建“空白”组列。我将用这些要点来更新这个问题,因为我正在寻找所有配置的解决方案。@SteveChapman似乎与column group一起工作(参见fiddle)。我不知道你们是怎么排的。也许你可以用代码来说明。看这个例子(这是你使用的小提琴的更新):@SteveChapman我感觉到了你的痛苦。在单价不属于列组的情况下进行更新(希望如此)。我敢说它也可以扩展以适应这种情况。我感谢你在这里所做的努力。正如您所说,这有点不稳定,但在数据/excel模型上缺乏某种DSL的情况下,我认为这是一个可行的解决方案。已经向剑道提出了一个问题,因为这不可能是一个仅限于我的问题。。。我将标记为正确,当任何消息出现时,我将重新访问。我不知道此链接是否有帮助,但我必须在正式支持之前将自己的解析器和导出器编写为excel和pdf。我认为底层网格结构中的列/行和递归分组仍然存在。如果您能解开我多年前的代码,那么您可能会发现组和列之间的递归关系。-->
    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";
        }
    
    }