-4

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

Gilles Heinesch
  • 2,889
  • 1
  • 22
  • 43
  • 1
    Does 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 Answers3

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

Source

Gilles Heinesch
  • 2,889
  • 1
  • 22
  • 43
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