最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin8浏览0评论

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.

I wanted cells to look like this

I have gone through the exceljs documentation but didn't find anything.

Thanks in advance !!

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.

I wanted cells to look like this

I have gone through the exceljs documentation but didn't find anything.

Thanks in advance !!

Share Improve this question asked Feb 25, 2021 at 8:26 Shivam KubdeShivam Kubde 6233 gold badges9 silver badges18 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 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.

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.

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 ment

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

Do not mention the height that's automatically set. You need to use the following:

let rowValue = worksheet.addRow(RecordValues);
rowValue.eachCell(cell => {
       cell.alignment = 
{ horizontal: 'center',vertical :'middle' ,wrapText: true};
})

Here, RecordValues is the table of displayed values.

I was working on a similar issue, this is how i solved it

this.xlRow = 18

    let slno = 1

    console.log(excelData.data.inoviceDetails)
    excelData.data.inoviceDetails.forEach((d: any) => {
        this.xlCol = 1

        // worksheet.getCell(this.xlRow, this.xlCol++).value = slno

        let cell1  = worksheet.getCell(this.xlRow, this.xlCol++);
        cell1.value = slno
        cell1.alignment = {vertical :'top'}


        let cell2 = worksheet.getCell(this.xlRow, this.xlCol++);
        cell2.value = d.particular;
        cell2.alignment = { wrapText: true, vertical :'top' };  // Enable text wrapping
        
        // worksheet.getCell(this.xlRow, 9).value = d.value
        let cell3 = worksheet.getCell(this.xlRow, 9)
        cell3.value = d.value
        cell3.alignment = {vertical :'top'}

        // Merge cells from B to H
        let m1 = worksheet.getCell(this.xlRow, 2);  // Cell B
        let m2 = worksheet.getCell(this.xlRow, 8);  // Cell H
        worksheet.mergeCells(m1.address, m2.address);

        var row = worksheet.getRow(this.xlRow);
        row.height = row.getCell(2).toString().trim().length/4

        slno++

        this.xlRow++;
    }
    );
worksheet.getRow(indexRow).height = undefined

Working for me

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论