I am facing an issue while merging the cells using ExcelJS, below is the code where I want to merge the cells from B1 to C1, instead of merging the cell it duplicates the value of B1 into C1. See the attached screenshot:
async getProjectReport(data) {
try {
// Create a new workbook and add a worksheet for the report
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Project Report');
// Add headers for the project data table
worksheet.addRow(['Project No.', 'Project Name']).font = {
bold: true,
};
// merge cells
worksheet.mergeCells('B1:C1');
// Set column widths for the project data table
worksheet.columns = [{ width: 12 }, { width: 30 }, { width: 15 }];
const projectDetailsRow = worksheet.addRow(['1', 'Project Abc']);
// Write the workbook to a buffer and return it
const buffer = await workbook.xlsx.writeBuffer();
return buffer;
} catch (error) {
throw new Error(error);
}
}
I want a output like the attached screenshot.
I also tried the below function to achieve the output I want but got the same result.
// merge by start row, start column, end row, end column (equivalent to K10:M12)
worksheet.mergeCells(1,2,2,3);
New contributor
Bharat Kumar Rajor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.