![](/img/trans.png)
[英]How to set excelsheet row height to auto so that the content can autofit in the cell using exceljs
[英]How to Auto resize row in excelJs?
您可以通過執行如下所示的循環來執行此操作:
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;
}
雖然最初我不知道它可以做到。 如果可以從某個單元格高度開始:
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
你可以這樣做:
Foreach 您的數據並通過索引獲取所需的行
const row = worksheet.getRow(index);
row.height = row.height * 20 / row.width;
*筆記:
我固定了數字“20”,但您也可以將其設為變量
在我的 excel 中,我的數據數據從第四行開始,所以我這樣做:
const row = worksheet.getRow(index + 4);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.