Kendo UI Grid - Excel export with hidden columns and custom formatting


Steve Chapman

I'm trying to export to excel using the Grid component's built-in support, with custom cell formatting applied as shown in the following Telerik documentation:

http://docs.telerik.com/kendo-ui/controls/data-management/grid/how-to/excel/cell-format

There is a fairly obvious problem with the approach of using hardcoded row/cell indices in the export when exporting a grid showing previously hidden columns - the best way to reproduce is to quote this jsfiddle:

https://jsfiddle.net/3anqpnqt/1/

  1. run violin
  2. Click Export to Excel - Comply with Custom Number Formats
  3. Unhide the subcategory column (using the column menu)
  4. Click export to excel - note the custom number format on column 2 (now "Subcategory")

Refer to this code in the fiddle:

$("#grid").kendoGrid({
    toolbar: ["excel"],
    excel: {
      fileName: "Grid.xlsx",
      filterable: true
    },
    columns: [
      { field: "productName" },
      { field: "category" },
      { field: "subcategory", hidden: true },
      { field: "unitPrice"}
    ],
    dataSource: [
      { productName: "Tea", category: "Beverages", subcategory: "Bev1", unitPrice: 1.5 },
      { productName: "Coffee", category: "Beverages", subcategory: "Bev2", unitPrice: 5.332 },
      { productName: "Ham", category: "Food", subcategory: "Food1", unitPrice: -2.3455 },
      { productName: "Bread", category: "Food", subcategory: "Food2", unitPrice: 6 }
    ],
    columnMenu: true,
    excelExport: function(e) {      
      var sheet = e.workbook.sheets[0];

      for (var rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
        var row = sheet.rows[rowIndex];
        var numericFormat = "#,##0.00;[Red](#,##0.00);-";
        for (var cellIndex = 0; cellIndex < row.cells.length; cellIndex++) {
            var cell = row.cells[cellIndex];
            if (row.type === "data") {
                if (cellIndex == 2) { // how are we able to identify the column without using indexes?
                    cell.format = numericFormat;
                    cell.hAlign = "right";
                }
            }
        }      
      }      
    }
});

All I need to do is identify the cell as 'unitPrice' and apply the formatting, but checking the object model in the excelExporthandler doesn't give me any way to make this link. In my actual application, I have several custom formats (percent, n0, n2, etc.) that can be applied, so it's not as simple as doing it $.isNumeric(cell.value).

renew

I also need to use the solution to handle columns/row groups, which generate additional header rows/columns in the Excel model.

fruit bat

It looks like row[0] is the header row, so you can try changing

if (cellIndex == 2) {

arrive

if (sheet.rows[0].cells[cellIndex].value == "unitPrice") { 

edit:

Seems to work with column groups : https://jsfiddle.net/dwosrs0x/

renew:

The object model of the worksheet is not the clearest. The first row does seem to be the "main" header row in the various cases I've looked at. This seems to work if the unitPrice is not in the grouping. If the unitPrice is in the group, it may be more complicated to involve the group header (row[1]). The challenge is figuring out where the desired column will end up occupying.

var header = sheet.rows[0];
var upIndex = -1;
var upFound = false;

for (var cellIndex = 0; cellIndex < header.cells.length; cellIndex++) {

    if ('colSpan' in header.cells[cellIndex]) 
        upIndex = upIndex + header.cells[cellIndex].colSpan;
    else 
        upIndex = upIndex + 1;

    if (header.cells[cellIndex].value == "unitPrice") { // wot we want
        upFound = true;
        break;
    }
}

for (var rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
    var row = sheet.rows[rowIndex];
    if (row.type === "data" && upFound) {
        var cell = row.cells[upIndex];
        cell.format = numericFormat;
        cell.hAlign = "right";
    }

}

Fiddle with the group - https://jsfiddle.net/dwosrs0x/4/

Fiddle with a simple grid (prove it still works) - https://jsfiddle.net/gde4nr0y/1/

It definitely has a "stiff" flavor to it.

Related


Kendo UI: Manipulate grid columns during export to excel and pdf

jungle fish I have a Kendo grid using Export-to-excel and Export-to-pdf. A particular column consists of zero-padded data (so that the column ordering is effective). This column then uses a template to display data without zeros (business requirement). This is

Kendo UI Multi-Grid Excel Export

Vincent Here is the code I use to export Kendo multiple grids to Excel, how do I export all pages Example - Multi-Grid Excel Export Jayesh Goyani Please try the following code snippet. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Kendo U

Kendo UI Grid export to Excel not working

Nick Soder I have a similar question, why is my excelexport button not working? What's wrong Thanks for help. $("#grid").kendoGrid({ toolbar: ["create", "excel", "pdf"], excel: { fileName: "Test.xlsx", allPages: true }, columns: [ {

Kendo UI Multi-Grid Excel Export

Vincent Here is the code I use to export Kendo multiple grids to Excel, how do I export all pages Example - Multi-Grid Excel Export Jayesh Goyani Please try the following code snippet. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Kendo U

Kendo UI Multi-Grid Excel Export

Vincent Here is the code I use to export Kendo multiple grids to Excel, how do I export all pages Example - Multi-Grid Excel Export Jayesh Goyani Please try the following code snippet. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Kendo U

Kendo UI Grid export to Excel not working

Nick Soder I have a similar question, why is my excelexport button not working? What's wrong Thanks for help. $("#grid").kendoGrid({ toolbar: ["create", "excel", "pdf"], excel: { fileName: "Test.xlsx", allPages: true }, columns: [ {

Kendo UI Grid export to Excel not working

Nick Soder I have a similar question, why is my excelexport button not working? What's wrong Thanks for help. $("#grid").kendoGrid({ toolbar: ["create", "excel", "pdf"], excel: { fileName: "Test.xlsx", allPages: true }, columns: [ {

Kendo UI Multi-Grid Excel Export

Vincent Here is the code I use to export Kendo multiple grids to Excel, how do I export all pages Example - Multi-Grid Excel Export Jayesh Goyani Please try the following code snippet. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Kendo U

Kendo UI for Angular custom sorting grid columns

h3li0s I have the following column in Kendo Grid (Jquery's UI) which has a special sorting method based on customer needs. field: "daysLeft", title: "Accessible", width: 130, sortable: { compare: function (a, b, descending) { if (a.daysLeft == 'Not start

Kendo UI for Angular custom sorting grid columns

h3li0s I have the following column in Kendo Grid (Jquery's UI) which has a special sorting method based on customer needs. field: "daysLeft", title: "Accessible", width: 130, sortable: { compare: function (a, b, descending) { if (a.daysLeft == 'Not start

Kendo UI for Angular custom sorting grid columns

h3li0s I have the following column in Kendo Grid (Jquery's UI) which has a special sorting method based on customer needs. field: "daysLeft", title: "Accessible", width: 130, sortable: { compare: function (a, b, descending) { if (a.daysLeft == 'Not start

Grid columns shrink in Kendo grid after excel export

Paparis I'm new to Kendo-UI and Web-Engineering and I'm facing a problem that I haven't solved after investing hours. I have a grid with 8 columns and an Excel toolbar button. When I fire the excelExport event, I show another 2 hidden columns to include their

Kendo Excel Export - How to export columns with custom template?

James Willow I have a well defined kendo grid. I enabled the Excel export toolbar bydata-toolbar='["excel"]' The problem is that this will only export fields for which no template is defined. (the first 3 in the grid below: Checkpoint, Location, Patrolled By),

Kendo Excel Export - How to export columns with custom template?

James Willow I have a well defined kendo grid. I enabled the Excel export toolbar bydata-toolbar='["excel"]' The problem is that this will only export fields for which no template is defined. (the first 3 in the grid below: Checkpoint, Location, Patrolled By),

Kendo Excel Export - How to export columns with custom template?

James Willow I have a well defined kendo grid. I enabled the Excel export toolbar bydata-toolbar='["excel"]' The problem is that this will only export fields for which no template is defined. (the first 3 in the grid below: Checkpoint, Location, Patrolled By),

Kendo Excel Export - How to export columns with custom template?

James Willow I have a well defined kendo grid. I enabled the Excel export toolbar bydata-toolbar='["excel"]' The problem is that this will only export fields for which no template is defined. (the first 3 in the grid below: Checkpoint, Location, Patrolled By),

Kendo Excel Export - How to export columns with custom template?

James Willow I have a well defined kendo grid. I enabled the Excel export toolbar bydata-toolbar='["excel"]' The problem is that this will only export fields for which no template is defined. (the first 3 in the grid below: Checkpoint, Location, Patrolled By),

Kendo UI export to Excel

Vicky I am using MVC4 in Razor. Here is the code for my CSS and Javascript files <link rel="stylesheet" href="~/Content/kendo/kendo.common.min.css" type="text/css" > <link rel="stylesheet" href="~/Content/kendo/kendo.rtl.min.css" type="text/css" > <lin

Kendo UI export to Excel

Vicky I am using MVC4 in Razor. Here is the code for my CSS and Javascript files <link rel="stylesheet" href="~/Content/kendo/kendo.common.min.css" type="text/css" > <link rel="stylesheet" href="~/Content/kendo/kendo.rtl.min.css" type="text/css" > <lin

Kendo UI Grid for MVC export to Excel doesn't do anything

Stewart I am using Kendo grid for MVC 4.0. I have the latest DLL 2015.1.318.440. I included jszip.js. I copied and pasted the code from the example: .ToolBar(tools => tools.Excel()) .Excel(excel => excel.FileName("Enrollments.xlsx")) It do

Kendo UI Grid for MVC export to Excel doesn't do anything

Stewart I am using Kendo grid for MVC 4.0. I have the latest DLL 2015.1.318.440. I included jszip.js. I copied and pasted the code from the example: .ToolBar(tools => tools.Excel()) .Excel(excel => excel.FileName("Enrollments.xlsx")) It do

Kendo UI Grid for MVC export to Excel doesn't do anything

Stewart I am using Kendo grid for MVC 4.0. I have the latest DLL 2015.1.318.440. I included jszip.js. I copied and pasted the code from the example: .ToolBar(tools => tools.Excel()) .Excel(excel => excel.FileName("Enrollments.xlsx")) It do

Kendo UI Grid for MVC export to Excel doesn't do anything

Stewart I am using Kendo grid for MVC 4.0. I have the latest DLL 2015.1.318.440. I included jszip.js. I copied and pasted the code from the example: .ToolBar(tools => tools.Excel()) .Excel(excel => excel.FileName("Enrollments.xlsx")) It do

Kendo UI Grid for MVC export to Excel doesn't do anything

Stewart I am using Kendo grid for MVC 4.0. I have the latest DLL 2015.1.318.440. I included jszip.js. I copied and pasted the code from the example: .ToolBar(tools => tools.Excel()) .Excel(excel => excel.FileName("Enrollments.xlsx")) It do

Kendo UI Grid for MVC export to Excel doesn't do anything

Stewart I am using Kendo grid for MVC 4.0. I have the latest DLL 2015.1.318.440. I included jszip.js. I copied and pasted the code from the example: .ToolBar(tools => tools.Excel()) .Excel(excel => excel.FileName("Enrollments.xlsx")) It do

Kendo UI Grid Template Columns

Ethan Saddji I have this grid in my page and it works great: @(Html.Kendo().Grid<SalaryAdmin.Classes.ReturnedCompareHoghough>() .Name("Grid") .EnableCustomBinding(true) .BindTo(Model.Comparehoghough) .Columns(columns => { columns.Te

Kendo UI Grid Template Columns

Ethan Saddji I have this grid in my page and it works great: @(Html.Kendo().Grid<SalaryAdmin.Classes.ReturnedCompareHoghough>() .Name("Grid") .EnableCustomBinding(true) .BindTo(Model.Comparehoghough) .Columns(columns => { columns.Te

Custom Commands in Kendo UI Grid

Said Roohullah Allem I want to write some code to display the details of my item using the Action method. @(Html.Kendo().Grid<Jahan.Blog.Model.Article>() .Name("ArticleAdmin").Navigatable() .Resizable(c => c.Columns(true)) .HtmlAt

Load Kendo UI grid into hidden div?

Zach B Has anyone loaded a Kendo UI grid into a hidden div (for example display:none;)? I ran into the problem I was having trying to do it myself and I ended up with an empty grid. Rebuilding the grid after the divs reappeared on screen solved the problem. Wo