This is a migrated thread and some comments may be shown as answers.

Export Excel column width for Dates

1 Answer 517 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Koren
Top achievements
Rank 1
Koren asked on 26 Nov 2019, 02:36 PM

Working with the Core grid I noticed that the excel export autowidth is not working for date columns.  Every other column is working but not dates.  Here is my function for the export...

function gridReport_ExcelExport(e) {
    var sheet = e.workbook.sheets[0];
    for (var rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
        var row = sheet.rows[rowIndex];
        var expandRow = false;
        for (var cellIndex = 0; cellIndex < row.cells.length; cellIndex++) {
            var cell = row.cells[cellIndex];
            if (rowIndex > 0)
                cell.vAlign = "top";
            if (cell.value && cell.value.toString().indexOf("<br />") >= 0) {
                cell.value = cell.value.replace("<br />", "\n");
                cell.wrap = true;   
                expandRow = true;
            }
        }
        if (rowIndex > 0)
            sheet.rows[rowIndex].height = 50;
    }   
    for (var colIndex = 0; colIndex < sheet.columns.length; colIndex++) {
        var column = sheet.columns[colIndex];
        delete column.width;
        column.autoWidth = true;
    }
}

 

I found a kendo dojo and was able to duplicate the issue there also.  (http://dojo.telerik.com/oGafisuF)  I modified it as this...

<body>
  <div id="grid"></div>
  <script>
    var dt = new Date();
  $("#grid").kendoGrid({
    dataSource: [
      { foo: "foo", bar: "bar", somedate: dt }
    ],
    toolbar:["excel"],
    columns: [
      { field: "foo", width: "100px"},
      { field: "bar", width: "200px"},
      { field: "somedate", width: "100px", template: "#= kendo.toString(kendo.parseDate(somedate, 'yyyy-MM-dd'), 'MM/dd/yyyy') #"}
    ],
    excelExport: function(e) {
      var sheet = e.workbook.sheets[0];
      for (var i = 0; i < sheet.columns.length; i++) {
        delete sheet.columns[i].width;
        sheet.columns[i].autoWidth = true;
      }
    }
  });
  </script>
</body>

 

1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 29 Nov 2019, 12:19 PM

Hi Koren,

The problem lies in the fact the auto width is calculated basen on the length of a stringified date(example provided below), not a formatted one.

Fri Nov 29 2019 14:13:15 GMT+0200 (Eastern European Standard Time)
 In order to make things work you can format the date in the workbook as demonstrated in this modified dojo.

Regards,
Angel Petrov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Koren
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or