How to set excelsheet row height to auto so that the content can autofit in the cell using exceljs
Asked
Active
Viewed 4,394 times
-4

Gilles Heinesch
- 2,889
- 1
- 22
- 43

ISHITA PRAMANICK
- 99
- 1
- 7
-
1Does this answer your question? [How to set excelsheet row height to auto so that the content can autofit in the cell using exceljs](https://stackoverflow.com/questions/66364840/how-to-set-excelsheet-row-height-to-auto-so-that-the-content-can-autofit-in-the) – Bart de Ruijter Oct 18 '21 at 14:41
3 Answers
1
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;
}

Gilles Heinesch
- 2,889
- 1
- 22
- 43
-
I have tried it, It is not working. The row is not getting resized – ISHITA PRAMANICK Apr 05 '21 at 13:14
-
I've edited my post, can you try this new one please @ISHITAPRAMANICK – Gilles Heinesch Apr 05 '21 at 13:25
-
This will increase the width of the column. My requirements is to increase the height of the row – ISHITA PRAMANICK Apr 05 '21 at 13:27
-
0
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]: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Frank
- 403
- 3
- 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