使用node.js读取Excel文件

Mar*_*sen 36 javascript import-from-excel node.js angularjs

好的,所以我使用FileUploader模块将我的文件从angular上传到我的REST API:

var uploader = $scope.uploader = new FileUploader({
    url: api.getUrl('uploadCompetence',null)
});
Run Code Online (Sandbox Code Playgroud)

这将发送到以下POST功能:

        router.route('/api/uploadCompetence')
        .post(function (req, res) {

        // This is where i want to read the file

            var competence = Competence.build(req.body.location);
            competence.add(function (success) {
                    res.json({message: 'quote created!'});
                },
                function (err) {
                    res.status(err).send(err);
                });
        })
Run Code Online (Sandbox Code Playgroud)

现在我的目标是读取excel文件,然后将每一行添加到我的数据库.

但是,我不太确定我如何从Node.js我调试我的服务器读取文件,无法在任何地方找到该文件,但从我的Angular应用程序调用api

任何人都可以把我推向正确的方向吗?:)

alu*_*sen 61

有一些不同的库正在解析Excel文件(.xlsx).我将列出两个我感兴趣并值得研究的项目.

节点XLSX

Excel解析器和构建器.它是一个流行项目JS-XLSX的包装器,它是Office Open XML规范中的纯javascript实现.

node-xlsx项目页面

解析文件的示例

var xlsx = require('node-xlsx');

var obj = xlsx.parse(__dirname + '/myFile.xlsx'); // parses a file

var obj = xlsx.parse(fs.readFileSync(__dirname + '/myFile.xlsx')); // parses a buffer
Run Code Online (Sandbox Code Playgroud)

ExcelJS

读取,操作和编写电子表格数据和样式到XLSX和JSON.这是一个活跃的项目.在撰写本文时,最新的提交是在9小时前.我自己没有测试过这个,但api看起来很广泛,有很多可能性.

exceljs项目页面

代码示例:

// read from a file
var workbook = new Excel.Workbook();
workbook.xlsx.readFile(filename)
    .then(function() {
        // use workbook
    });

// pipe from stream
var workbook = new Excel.Workbook();
stream.pipe(workbook.xlsx.createInputStream());
Run Code Online (Sandbox Code Playgroud)


Cha*_*ena 28

您还可以使用名为js-xlsx的 此节点模块

1)安装模块
npm install xlsx

2)导入模块+代码段

var XLSX = require('xlsx')
var workbook = XLSX.readFile('Master.xlsx');
var sheet_name_list = workbook.SheetNames;
var xlData = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]);
console.log(xlData);
Run Code Online (Sandbox Code Playgroud)


Con*_*sky 8

您可以使用read-excel-file npm。

在这里,您可以指定JSON SchemaXLSX转换为JSON格式。

const readXlsxFile = require('read-excel-file/node');

const schema = {
    'Segment': {
        prop: 'Segment',
        type: String
    },
    'Country': {
        prop: 'Country',
        type: String
    },
    'Product': {
        prop: 'Product',
        type: String
    }
}

readXlsxFile('sample.xlsx', { schema }).then(({ rows, errors }) => {
    console.log(rows);
});
Run Code Online (Sandbox Code Playgroud)


Cod*_*ker 7

安装 exceljs 并使用以下代码,

var Excel = require('exceljs');

var wb = new Excel.Workbook();
var path = require('path');
var filePath = path.resolve(__dirname,'sample.xlsx');

wb.xlsx.readFile(filePath).then(function(){

    var sh = wb.getWorksheet("Sheet1");

    sh.getRow(1).getCell(2).value = 32;
    wb.xlsx.writeFile("sample2.xlsx");
    console.log("Row-3 | Cell-2 - "+sh.getRow(3).getCell(2).value);

    console.log(sh.rowCount);
    //Get all the rows data [1st and 2nd column]
    for (i = 1; i <= sh.rowCount; i++) {
        console.log(sh.getRow(i).getCell(1).value);
        console.log(sh.getRow(i).getCell(2).value);
    }
});
Run Code Online (Sandbox Code Playgroud)