0

How can I prepend a regular excel row above the table data when exporting to excel? There is a "from" and "to" filter option, but this seems to only affect the column index.

filter: { from: 1, to: 2 }

Changing this from an integer to a string results in a corrupted excel file:

filter: { from: "A2", to: "B2" }

The documentation doesn't seem to mention any other properties for the filter.

I have tried changing the sheets.rows.type option from data to header but that doesn't seem to do anything.

Example export:

var workbook = new kendo.ooxml.Workbook({
  sheets: [
      {
          filter: { from: 1, to: 2 },
          rows: [
            // row above table
            { cells: [ { value: "pre table header", "colSpan": 4 } ] },

            // table: header
            { cells: [ { value: "First Name" }, { value: "Last Name" } ] },
            // table: data
            { cells: [ { value: "John" }, { value: "Doe" } ] },
            { cells: [ { value: "Jane" }, { value: "Doe" } ] }
          ]
      }
  ]
});
kendo.saveAs({
  dataURI: workbook.toDataURL(),
  fileName: "Test.xlsx"
});

https://dojo.telerik.com/emUzuPeq

What I am trying to achieve:

excel snip

What the above code does:

excel snip

BurnsBA
  • 4,347
  • 27
  • 39

1 Answers1

0

I found an undocumented feature in the kendo grid source. It will use a property called "ref" on the filter if available. This also seems to require the from and to properties be changed to a string address in the excel file. Not sure how stable this is, but this works on the latest version of kendoui.

filter: { ref: "A2:B2", from: "A2", to: "B2" },

enter image description here

Implemented in event handler for excel export, it looks something like

var intFrom = sheet["filter"]["from"];
var intTo = sheet["filter"]["to"];

// kendo uses zero based index for column, but need column 1 to be "A"
var colFrom = excelGetCharFromNumber(intFrom + 1);
var colTo = excelGetCharFromNumber(intTo + 1);

// "2" row is hardcoded here
sheet["filter"] = {
    // redefine "from" and "to"
    "ref": "" + colFrom + "2:" + colTo + "2",
    "from": "" + colFrom + "2",
    "to": "" + colTo + "2"
};

Where excelGetCharFromNumber is this answer:

function excelGetCharFromNumber(columnNumber){
    var dividend = columnNumber;
    var columnName = "";
    var modulo;

    while (dividend > 0)
    {
        modulo = (dividend - 1) % 26;
        columnName = String.fromCharCode(65 + modulo).toString() + columnName;
        dividend = parseInt((dividend - modulo) / 26);
    } 
    return  columnName;
}
BurnsBA
  • 4,347
  • 27
  • 39