Manipulating grid columns during export to excel and pdf

1 Answer 1205 Views
Grid
Michael
Top achievements
Rank 1
Michael asked on 20 Sep 2021, 03:27 PM | edited on 21 Sep 2021, 07:59 AM

I have a Kendo grid that uses Export-to-excel and Export-to-pdf.

One particular column consists of data with padded zeros (so that column sorting works). Then, this column uses a template to display the data without the padded zeros (a business requirement). This is perfect for the grid.

Now, the export functions do not export the template, they export the underlying data (this is documented in the Known Limitations). So my exports show the data with padded-zeros. But... I need to show the data without padded zeros. So I have been looking for a workaround.

Workaround attempt A)
I created two columns padded and non-padded. The idea was this:
Column i/ Data = padded; Grid view = non-padded; do not export.
Column ii/ Data = non-padded; Grid view = hidden; export.

However, this doesn't work for two reasons. 
Column i/ columns: exportable: { pdf: false, excel: false } doesn't actually seem to work(!!!)
Column ii/ This isn't legal anyway. If you hide the data in the grid you can't export it anyway.

Workaround attempt B)
In the excelExport() function I did this:

      excelExport: function (e) {
        for (var j = 0; j < e.data.length; j++) {
e.data[j].padded_column = e.data[j].non-padded_column;
        }
      },

In the console this appears to work fine, that is I replace the value of the padded column with the data of the non-padded column. However, it makes no difference to what appears on the spreadsheet. My guess is that this is because the spreadsheet has already been generated before excelExport() modifies the data.

So, I need a new approach. Can anybody help?

ADDITIONAL INFORMATION

For further reference, here is the code for the column:

      columns: [{
          field: 'sys_id_sorted', 
          title: 'File ref',
          hidden: false,
          template: function (dataItem) {
            var ctyClass = '';
            switch (dataItem.cty_id) {
              case '1':
                ctyClass = 'CHAP';
                break;
              case '2':
                ctyClass = 'EU-PILOT';
                break;
              case '3':
                ctyClass = 'NIF';
                break;
              case '4':
                ctyClass = 'OTHER';
                break;
              default:
                ctyClass = 'default';
                break;
            }
            return '<div class="label label-' + ctyClass + ' origin">' + dataItem.sys_id + '</div>';
          }
        },

'sys_id_sorted' is the field that has padded zeros.
'dataItem.sys_id' is the field with no padded zeros.      

1 Answer, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 23 Sep 2021, 11:10 AM

Hi Michael,

It is recommended to utilize the kendo.ooxml.Workbook in the excelExport event handler and export the template data instead of the data source data.

More information and a runnable demo about this you can find in the following article:

Let me know if you have any questions.

Regards,
Nikolay
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Share this question
or