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

1

3 Answers

You can do this by doing a loop as shown below:

const workbook = new Excel.Workbook(); const sheet1 = workbook.addWorksheet('sheet1'); for (let i = 0; i < sheet1.columns.length; i += 1) { let dataMax = 0; const column = sheet1.columns[i]; for (let j = 1; j < column.values.length; j += 1) { const columnLength = column.values[j].length; if (columnLength > dataMax) { dataMax = columnLength; } } column.height = dataMax < 10 ? 10 : dataMax; } 

Source

4

Although initially I do not know that it can be done. If it can be started with a certain cell height:

yourDataArray.forEach((e, index) => { worksheet.getRow(index).height = 28; // Height for file = 28 inches worksheet.addRow({ ...e // data Object [Destructuring][1] }) } ´´´ [1]: 

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);