Search code examples
excelxmlxlsx

How can we give height based on content of a cell in xlsx?


I am trying to create an Excel file through XML, How can we give a height based on the content of the cell. Here I manually added ht=27 in a row, is there any other option to change the height based on the content? So it will look nice even if it is 3 lines or 10 lines

<row r="26" spans="1:26" ht="27" x14ac:dyDescent="0.3">
       <c r="B26" s="3"/>
       ......
       ......
       <c r="I26" s="41" t="inlineStr">
           <is><t>long text/short text (dynamic text)</t></is>
       </c>
       ......
       ......
       <c r="Z26" s="3"/>
   </row>

Any idea/suggestions, how to do that or alternate option


Solution

  • If you would completely remove the ht and the x14ac:dyDescent attribute from the row's XML, then the height will be auto calculated by content as long as there are not any merged cells in that row.

    So

       <row r="26" spans="1:26">
           <c r="B26" s="3"/>
           ......
           ......
           <c r="I26" s="41" t="inlineStr">
               <is><t>long text/short text (dynamic text)</t></is>
           </c>
           ......
           ......
           <c r="Z26" s="3"/>
       </row>
    

    should look as you want as long as there are not any merged cells in that row and s="41" points to a cell style where wrapText is set true.

    Why to remove the explicit set of ht should be self explanatory. If not present, implicit height calculation will be done.

    Why additional the remove of x14ac:dyDescent is necessary? Documentation of 2.5.3 dyDescent tells it:

    The dyDescent attribute has a side effect; it sets the customHeight attribute to true even if the customHeight attribute is explicitly set to false.

    So if set x14ac:dyDescent, then no implicit height calculation will be done.