How to set excelsheet row height to auto so that the content can autofit in the cell using exceljs

Viewed 4579

I am using exceljs npm to export data in excelsheet. As my content in the cell is huge so I wanted text to be wrapped in the cell and should take enough height to show full content. I tried by giving fixed width -

worksheet.getRow(1).height = 20;

The problem with fixed height is that if my content is large than this size then it won't fit in the cell and it shows cropped text like shown in the screenshot.

enter image description here

I wanted cells to look like this enter image description here

I have gone through the exceljs documentation but didn't find anything.

Thanks in advance !!

3 Answers

Have you already tried not to define the height of the row? I had the same problem and I solved it like this and that way the height of the row adjusts automatically.

I hope the solution I applied in my case is useful to you. Greetings.

what worked for me was ,

worksheet.addRow(temp);
const row = worksheet.lastRow;
row.getCell(3).alignment = { wrapText: true };

when ever a new row is added, that rows 3rd cell alignment has to be set.

or

once all the rows are added in that sheet , iterated over all the rows and set the alignment property.

You can do something like this: Foreach on your data and get the desired row through the index

const row = worksheet.getRow(index);
    row.height = row.height * 20 / row.width;

*NOTE:

  • I fixed the number '20', but you can also make it a variable

  • In my excel, my data data starts at line four onwards, so I do:

     const row = worksheet.getRow(index + 4);
    
Related