Search code examples
javascriptjquerykendo-uikendo-gridkendospreadsheet

kendo Grid excelExport's hideColumn not working


I am trying to hide one column from excelExport. I have tried some traditional ways shown on google like e.sender.hideColumn(13) or e.sender.hideColumn("bID") or grid.hideColumn(13). but none of this working for me. I have also done exportPDF which is working perfectly.

here is JS code I am attaching, to show what I am doing.

$("#ALReport").kendoGrid({
        toolbar: ["excel", "pdf"],
        excel: {
            allPages: true,
            fileName: "ALReport" + todayDateFormatted() + ".xlsx",
            proxyURL: "/content",
            filterable: true
        },
        excelExport: function (e) {
            e.sender.hideColumn(12);
            var grid = $("#ALReport").data("kendoGrid");
            grid.hideColumn("Bid");
        },
        pdf: {
            allPages: true,
            filterable: true,
            fileName: "ALReport" + todayDateFormatted() + ".pdf",
            proxyURL: "/content",
            margin: {
                left: 10,
                right: "10pt",
                top: "10mm",
                bottom: "1in"
            }
        },
        pdfExport: function (e) {
            var grid = $("#ALReport").data("kendoGrid");
            grid.hideColumn("Bid");
            $(".k-grid-toolbar").hide();
            e.promise
           .done(function () {
               grid.showColumn("Bid");
               $(".k-grid-toolbar").show();
           });
        },
        dataSource: {
            serverSorting: true,
            serverPaging: true,
            transport: {
                read: getActionURL() + "ALReport....
            },

.....

This is my js code. Can anyone guide where i am making mistake?


Solution

  • Your export to excel has no effect because event is fired after all data have been collected. For your example try following approach:

    var exportFlag = false;
        $("#grid").kendoGrid({
             toolbar: ["excel", "pdf"],
             excel: {
              allPages: true,
              fileName: "ALReport" + todayDateFormatted() + ".xlsx",
              proxyURL: "/content",
              filterable: true
            },
           excelExport: function (e) {
            if (!exportFlag) {
              e.sender.hideColumn("Bid");
              e.preventDefault();
              exportFlag = true;
              setTimeout(function () {
                  e.sender.saveAsExcel();
              });
          } else {
              e.sender.showColumn("Bid");
              exportFlag = false;
          }
        }
    

    For this example event is prevented and fired one more time excluding hidden column.