Kendo Grid导出到Excel货币格式

时间:2017-05-24 22:46:27

标签: c# jquery kendo-ui kendo-grid

我正在尝试将我的Kendo网格导出为ex​​cel。它工作正常,除了缺少格式。我认为这是因为我使用的是模板。

The Telerik documentation明确指出:

  

要在将网格导出到Excel期间格式化单元格值,请进行设置   单元格的格式选项。

我试过这个并且它无法正常工作:

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很简单??

2 个答案:

答案 0 :(得分:4)

我认为template不会对导出的数据产生任何影响(因为excelExport基于dataSource)。

以下是working example of 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";
            }
        }
    }      
  }

答案 1 :(得分:3)

我最终解决了这个问题,类似于桑德曼的建议。我将输出格式基于每个列(输入)的字段名称。

我还将网格的列标题附加到Excel列标题行。有点伤心剑道还没有做到这一点。

excelExport: function (e) {  
    var sheet = e.workbook.sheets[0];
    var row = sheet.rows[0];

    //Excel output - create a header row
    $("#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 output - column formatting
    var formatMask = null;
    for (var rowIndex = 1; rowIndex < sheet.rows.length; rowIndex++) { //begin on first row (skip header)
        row = sheet.rows[rowIndex];
        for (var cellIndex = 0; cellIndex < row.cells.length; cellIndex++) { //for each row, check each cell by field name
            switch (this.columns[cellIndex].field) {
                case "Field1":
                    formatMask = "###,##0";
                    break;
                case "Field2":
                    formatMask = "$#,##0";
                    break;
                case "Field3":
                    formatMask = "$#,##0";
                    break;
                case "Field4":
                    formatMask = "###,##0";
                    break;
                case "Field5":
                    formatMask = "###,##0";
                    break;
                case "Field6":
                    formatMask = "###,##0";
                    break;
                case "Field7":
                    formatMask = null;
                    row.cells[cellIndex].value = row.cells[cellIndex].value;
                    break;
                case "Field8":
                    formatMask = "###,##0";
                    break;
                case "Field9":
                    formatMask = "###,##0";
                    break;
                case "Field10":
                    formatMask = "###,##0";
                    break;
                case "Field11":
                    formatMask = "###,##0";
                    break;
                case "Field12":
                    if (row.cells[cellIndex].value === false) {
                        row.cells[cellIndex].value = "No";
                    } else {
                        row.cells[cellIndex].value = "Yes";
                    }
                    break;
                default:
                    break;
            }
            row.cells[cellIndex].format = formatMask; //apply column format mask
        }

    }
},