Remove HTML from Excel generated from Kendo Grid

旧巷老猫 提交于 2019-12-04 06:41:37

问题


I'm using the following code to load grid data and for removing the HTML from the footer and group footer in Excel. I'm using the recommended code only but somehow it's not working and there is no change in result as well.

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:


In your demo, the jQuery text() function is causing a javascript error. Use a regular expression replacement instead:

    $("#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
        },

Updated DEMO



来源:https://stackoverflow.com/questions/56184354/remove-html-from-excel-generated-from-kendo-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!