reactjsexceljs

How to set header at row 3 or 4 in exceljs


I want to set header at row 3 or 4 in exceljs.I have seen a similler question here and I have tried like what they suggest but it dosen't work for me.Can you guys please find out where i am doing wrong

        var filename = this.state.id + "no_machinereport.xlsx";
        var workbook = new Excel.Workbook();
        workbook.creator = "Sourav Singha";
        workbook.lastModifiedBy = "Sourav Singha";
        workbook.modified = new Date();
        workbook.lastPrinted = new Date();
        var worksheet = workbook.addWorksheet("Publications", { properties: { tabColor: { argb: '6B5B95' }, defaultRowHeight: 30, defaultColWidth: 25 } });
        worksheet.getRow(1).height = 48;
        worksheet.getRow(2).height = 21;

        // Print Title
        worksheet.mergeCells(1, 1, 1, 8);
        worksheet.getCell(1, 1).value = "Machine " + this.state.id + " Report";
        worksheet.getCell(1, 1).style = { font: { size: 16, underline: true, bold: true }, alignment: { vertical: "middle", horizontal: "center" }, width: 25 };

        var header_row = worksheet.getRow(3)
        const Header = [
            { header: "Date", key: "date", width: 30, style: { font: { size: 12, underline: false, bold: true }, alignment: { vertical: "middle", horizontal: "center" } } },
            { header: "Hour", key: "hour", width: 30, style: { font: { size: 12, underline: false, bold: true }, alignment: { vertical: "middle", horizontal: "center" } } },
            { header: "Runtime(runtime)", key: "runtime", width: 30, style: { font: { size: 12, underline: false, bold: true }, alignment: { vertical: "middle", horizontal: "center" } } },
            { header: "Stoptime(stoptime)", key: "stoptime", width: 30, style: { font: { size: 12, underline: false, bold: true }, alignment: { vertical: "middle", horizontal: "center" } } },
            { header: "Offline", key: "offline", width: 30, style: { font: { size: 12, underline: false, bold: true }, alignment: { vertical: "middle", horizontal: "center" } } },
            { header: "Stoppaage", key: "stoppage", width: 30, style: { font: { size: 12, underline: false, bold: true }, alignment: { vertical: "middle", horizontal: "center" } } },
        ];
        header_row.header = Header

Solution

  • What you're missing - and their document is really bad! because this isn't clear! - is that you need to create an array of objects with a key property for each column in the table. Then set that equal to worksheet.columns, and then you can populate your table rows at any row number you want:

    worksheet.addRow(['']) // blank Row
    worksheet.addRow(['']) // blank Row
    
    const headers = [
        { header: 'First name', key: 'fn', width: 15 },
        { header: 'Last name', key: 'ln', width: 15 },
        { header: 'Occupation', key: 'occ', width: 15 },
        { header: 'Salary', key: 'sl', width: 15 },
    ]
    
    worksheet.columns = headers;
    
    worksheet.addRow({fn:'John', ln:'Doe', occ:'gardener', sl:1230});
    worksheet.addRow({fn:'Roger', ln:'Roe', occ:'driver', sl:980]);