Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 430
F. Vosnim Avatar asked Nov 28 '18 13:11

F. Vosnim


People also ask

How do I read an XLSX file?

Method #1: OpenPyXL OpenPyXL is a Python library created for reading and writing Excel 2010 xlsx/xlsm/xltx/xltm files. It can read both the . xlsx and . xlsm file formats, which includes support for charts, graphs, and other data visualizations.

Can you open Xlsx in Matlab?

If your computer does not have Excel for Windows® or if you are using MATLAB® Online™, xlsread automatically operates in basic import mode, which supports XLS, XLSX, XLSM, XLTX, and XLTM files.


2 Answers

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)
like image 109
R. Schifini Avatar answered Oct 03 '22 02:10

R. Schifini


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

var cell = worksheet.getCell('C3').value;
like image 45
Vasily Vlasov Avatar answered Oct 03 '22 01:10

Vasily Vlasov