F. Vosnim
F. Vosnim

Reputation: 574

How to read xlsx file with ExcelJS?

How does the code should look like to get cell A1 value from the file "C:\1.xlsx"? I tried numbers of examples but still didn't managed to get it work.

var Excel = require('exceljs');

var workbook = new Excel.Workbook();

workbook.xlsx.readFile("C:\1.xlsx")
    .then(function() {
        var worksheet = workbook.getWorksheet('Sheet1');
        var cell = worksheet.getCell('A1').value;
        console.log(cell);
    });

I see no errors, but it doesn't work.

Upvotes: 5

Views: 17384

Answers (3)

F. Vosnim
F. Vosnim

Reputation: 574

await (new Promise((resolve, reject) => {
    var Excel = require('exceljs');
    var workbook = new Excel.Workbook();
    workbook.xlsx.readFile("C:/1.xlsx")
        .then(function() {
            ws = workbook.getWorksheet("Sheet1")
            cell = ws.getCell('A1').value
            console.log(cell)
            resolve()
        });
}));

Upvotes: 0

R. Schifini
R. Schifini

Reputation: 9313

You have to access the worksheet first and then the cell. Like this:

var Excel = require('exceljs');
var workbook = new Excel.Workbook();
workbook.xlsx.readFile("C:/1.xlsx")
    .then(function() {
        ws = workbook.getWorksheet("Sheet1")
        cell = ws.getCell('A1').value
        console.log(cell)
    });

Replace "Sheet1" with the real sheet's name. You can also access the worksheet by id.

ws = workbook.getWorksheet(1)

Upvotes: 7

Vasily  Vlasov
Vasily Vlasov

Reputation: 171

I guess you need to use getCell().value, like:

var cell = worksheet.getCell('C3').value;

Upvotes: 1

Related Questions