Column width didn't get set properly in excel output

2 posts, 0 answers
  1. Yifan
    Yifan avatar
    8 posts
    Member since:
    Oct 2017

    Posted 20 Oct 2017 Link to this post

    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? 

  2. Tsvetina
    Admin
    Tsvetina avatar
    2481 posts

    Posted 24 Oct 2017 Link to this post

    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.
Back to Top