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

Column width didn't get set properly in excel output

1 Answer 536 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Yifan
Top achievements
Rank 1
Yifan asked on 20 Oct 2017, 02:28 PM

Hi,

 

When I export the excel using the ooxml, I try to set the column width of all the columns. The following the the loop i used to set the width:

 

 for (var i = 0; i < sheet.columns.length; i++) {
                    if (i < 3) {
                        sheet.columns[i].width = 100;
                    } else if (i == 3) {
                        sheet.columns[i].width = 50;
                    } else {
                        sheet.columns[i].width = 25;
                    }
                }

It works perfectly for the first 2 conditions, I get 100 pixel for columns A-C and 50 pixels for column D. However, weird thing happens to the third condition. 25 indeed get set to cell but its in the excel default width unit (character) rather than in pixel. So it would be converted to 180 pixels on output excel and its way too wide for my purpose. I used the same code for another grid and that one works out perfectly. In ooxml doc, I don't see anything that allows me to change the width unit for the excel. It doesn't make any sense to me. Can anyone please tell me why? 

1 Answer, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 24 Oct 2017, 01:00 PM
Hello Yifan,

I am copying my reply from the support thread that you opened in case anyone else encounters the same problem.

The Grid uses autoWidth by default when exporting to Excel and I could not see if the default autoWidth setting of the Grid export worksheet is disabled in your code. The following should fix the problem:
excelExport: function(e){
  var sheet = e.workbook.sheets[0];
  
  for (var i = 0; i < sheet.columns.length; i++) {
    sheet.columns[i].autoWidth = false;
    if (i < 3) {
      sheet.columns[i].width = 100;
    } else if (i == 3) {
      sheet.columns[i].width = 50;
    } else {
      sheet.columns[i].width = 25;
    }
  }
}

I tried it in the following Dojo where it works as expected.
http://dojo.telerik.com/@tsveti/aJUFa/3

Regards,
Tsvetina
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Yifan
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or