exceljs/exceljs

Excel file corrupted after writing

Aperta

#745 aperta il 14 feb 2019

 (3 commenti) (0 reazioni) (0 assegnatari)JavaScript (1534 fork)batch import
help wanted

Metriche repository

Star
 (11.838 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

Hi, I try to compare two sheet and highlight the difference and all of sudden am seeing my excel is corrupting. Am using exceljs 1.7.0 version and am writing here after trying all possiblity by fixing the async issues.

async function compareTwoSheetsbyRows() {
    try {
         // var filePath = resultfolderpathfinal ;
        var filePath = resultfolderpath + "\\UnsavedReportExport.xlsx";
         resultfolderpathfinal = filePath;
        console.log("Excelfiel  " + filePath)
        var workbook = new Excel.Workbook();

        //  workbook.xlsx.readFile("c:/Results/temp/ConditionDashboard.xlsx").then(()=>{
        await workbook.xlsx.readFile(filePath).then(() => {
            console.log("Inside excel");
            let sheet1 = workbook.getWorksheet('Data');
            let sheet2 = workbook.getWorksheet('BIRSTDB');
            rowin1stSheet = null;
            rownotfound=null;
            columin1stSheet = null;
            var lastRow1 = sheet1.lastRow;
            equalSheets = true;
            Sheet1ColCount = sheet2.columnCount;

            for (a = 1; a <= lastRow1.number; a++) {
                row1 = sheet1.getRow(a).values;
                if (row1.length != 0) {
                    if (sheet1.getRow(a).getCell(1).value != null) {
                        rowin1stSheet = sheet1.getRow(a).getCell(1).value.toString().trim();
                        getrow = getRowName2ndSheet(sheet2, rowin1stSheet);
                        if (getrow != -1) {
                            for (b = 1; b <= Sheet1ColCount; b++) {
                                columin1stSheet = sheet1.getRow(1).getCell(b).toString().trim();
                                if (getColumnName2ndSheet(sheet2, columin1stSheet)) {
                                    cell1 = sheet1.getRow(a).getCell(getColumnHeaderforSheet(sheet1, columin1stSheet));
                                    cell2 = sheet2.getRow(getrow).getCell(getColumnHeaderforSheet(sheet2, columin1stSheet));
                                    if (!compareTwoCells(cell1.value, cell2.value)) {
                                     
                                        equalSheets = false;
                                         applyStyle(cell1, cell2, workbook);
                                        }
                                                                    }

                            }
                        }
                        else if(getrow === -1){
                            rownotfound = sheet1.getRow(a).getCell(1);
                            applyStyleforNotfoundrows(rownotfound, workbook);
                            

                        }
                    }

                    //  }
                }
            }
            return equalSheets;

        })
        console.log("At the end")
    }
    catch (e) {
        console.log(e)
    }
}

The style is applied here

async function applyStyle(cell1, cell2, workbook) {
    var filePath = resultfolderpathfinal;

    var cell1 = cell1 // get the cell
    cell1.style = Object.create(cell1.style);

    cell1.fill = {
        type: 'pattern',
        pattern: 'solid',
        fgColor: { argb: 'FFFF0000' }
    };
    var cell2 = cell2 // get the cell
    cell2.style = Object.create(cell2.style);
    cell2.fill = {
        type: 'pattern',
        pattern: 'solid',
        fgColor: { argb: 'FFFF0000' }
    };

    await workbook.xlsx.writeFile(filePath).then(resolve(workbook));

}

Guida contributor