4

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 !!

Shivam Kubde
  • 545
  • 1
  • 8
  • 17

4 Answers4

2

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.

1

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.

anil kumar d
  • 157
  • 2
  • 10
0

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);
    
Allan Bontempo
  • 117
  • 1
  • 5
  • Thanks @allan, seems like above solution does not work if we have large content in a column. – Shivam Kubde Jul 28 '21 at 08:34
  • @ShivamKubde If you raise 20, doesn't it work? – Allan Bontempo Jul 28 '21 at 12:19
  • It would work if I raise it. But I have to raise/decrease it dynamically according to content length. We can set column height as per content length by keeping some variable, But I am looking for exceljs method or props which will do it for me. – Shivam Kubde Jul 29 '21 at 09:52
  • @ShivamKubde were you able to solve this issue ? even am stuck in the same problem and in document i do not see any configuration parameter for this . – anil kumar d Aug 17 '21 at 09:36
  • @anilkumard I didn't found any solid solution, but for temporary I am setting row height as per content length. height = cell.value.toString().trim().length; row.height = height > 20 ? height * 2 / 3 : 20; – Shivam Kubde Aug 17 '21 at 10:44
  • only thing that worked for me was , set the cell width and then set that column or cell with wrapText: true – anil kumar d Jan 03 '22 at 13:44
0

The exceljs library does not provide a straightforward method to automatically adjust row heights based on the content in the cells.

For temporary, set row height as per content length. It work for me

row.height = cell.value.toString().trim().length

From Shivam Kubde's comment

Smith
  • 1