从Kendo Grid生成的Excel中删除HTML

时间:2019-05-17 10:16:31

标签: javascript jquery kendo-ui kendo-grid

我正在使用以下代码加载网格数据,并从Excel中的页脚和组页脚中删除HTML。我只使用推荐的代码,但不知何故,结果也没有变化。

var GridParams = function(fromDate, toDate) {
  var groupSortDirection = groupByPaymentDate.value();
  return {
    id: divReportGrid,
    showGroup: true,
    serverSorting: false,
    url: formUrls.gridUrl,
    columns: columns(),
    pageSize: 50,
    showReport: true,
    fileName: "Report.xlsx",
    toolbar: ["excel"],
    ExcelExport: function(e) {
      var rows = e.workbook.sheets[0].rows;
      for (var ri = 0; ri < rows.length; ri++) {
        var row = rows[ri];
        if (row.type == "group-footer" || row.type == "footer") {
          for (var ci = 0; ci < row.cells.length; ci++) {
            var cell = row.cells[ci];
            if (cell.value) {
              // Use jQuery.fn.text to remove the HTML and //get only the text
              cell.value = $(cell.value).text();
              // Set the alignment
              cell.hAlign = "right";
            }
          }
        }
      }
    },
    data: {
      "fromDate": fromDate,
      "toDate": toDate,
      "groupSortDirection": groupSortDirection
    },
    group: [{
      field: "PaymentDate",
      dir: groupSortDirection,
      aggregates: [
        // calculate max price
        {
          field: "CasTotal",
          aggregate: "sum"
        },
        {
          field: "ChkTotal",
          aggregate: "sum"
        },
        {
          field: "AmxTotal",
          aggregate: "sum"
        },
        {
          field: "VisTotal",
          aggregate: "sum"
        },
        {
          field: "MasTotal",
          aggregate: "sum"
        },
        {
          field: "CcrTotal",
          aggregate: "sum"
        },
        {
          field: "GcrTotal",
          aggregate: "sum"
        },
        {
          field: "DisTotal",
          aggregate: "sum"
        },
        {
          field: "CckTotal",
          aggregate: "sum"
        },
        {
          field: "SapTotal",
          aggregate: "sum"
        },
        {
          field: "IckTotal",
          aggregate: "sum"
        },
        {
          field: "Total",
          aggregate: "sum"
        }
      ]
    }]
  };
};

1 个答案:

答案 0 :(得分:1)

在您的演示中,jQuery text()函数导致JavaScript错误。改用正则表达式替换:

    $("#grid").kendoGrid({
        toolbar: ["excel"],
        excelExport: function(e) {
          var rows = e.workbook.sheets[0].rows;
          for (var ri = 0; ri < rows.length; ri++) {
            var row = rows[ri];
            if (row.type == "group-footer" || row.type == "footer") {
              for (var ci = 0; ci < row.cells.length; ci++) {
                var cell = row.cells[ci];
                if (cell.value) {
                  if (cell.value.indexOf("<div") > -1) {
                    var val = cell.value.replace(/<(?:.|\n)*?>/gm, '');
                    cell.value = val;
                  }
                  cell.hAlign = "right";
                }
              }
            }
          }
        },
        excel: {
            fileName: "Kendo UI Grid Export.xlsx",                  
            proxyURL: "https://demos.telerik.com/kendo-ui/service/export",
            filterable: true
        },

更新了 DEMO