Nayeer Mahiuddin
Nayeer Mahiuddin

Reputation: 317

Exceljs How to addRow and merge cells

So I need to generate an excel spreadsheet based on data from DB. Once I add in all the data from the DB I need to add in a new row at the bottom that says when the spreadsheet was generated like so:

enter image description here

I don't know how to get the cells to merge here, I can't use worksheet.mergeCells() since I don't know how many rows of data there will be in the DB. How would I merge the cells when using worksheet.addRow()?

Upvotes: 3

Views: 15791

Answers (1)

Aidam1
Aidam1

Reputation: 103

I have come across a similar problem and have used something close to this:

const newRow = worksheet.addRow(["This report is..."]);
const currentRowIdx = worksheet.rowCount; // Find out how many rows are there currently
const endColumnIdx = worksheet.columnCount; // Find out how many columns are in the worksheet
    
// merge by start row, start column, end row, end column
worksheet.mergeCells(currentRowIdx, 1, currentRowIdx, endColumnIdx );

Upvotes: 7

Related Questions