简体   繁体   English

如何使用 ExcelJS 读取 xlsx 文件?

[英]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"?从文件“C:\\1.xlsx”中获取单元格 A1 值的代码应该是什么样的? 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.我没有看到任何错误,但它不起作用。

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.将“Sheet1”替换为真实工作表的名称。 You can also access the worksheet by id.您还可以通过 id 访问工作表。

ws = workbook.getWorksheet(1)

我猜你需要使用getCell().value ,比如:

var cell = worksheet.getCell('C3').value;
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()
        });
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM