使用 node.js 读取 Excel 文件 [英] Reading Excel file using node.js
问题描述
好的,所以我正在使用 FileUploader
模块将我的文件从 angular 上传到我的 REST API
:
Okay so i am using the FileUploader
module to upload my file from angular to my REST API
:
var uploader = $scope.uploader = new FileUploader({
url: api.getUrl('uploadCompetence',null)
});
这被发送到以下 POST
函数:
This is sent to the following POST
function:
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);
});
})
现在我的目标是读取 excel
文件,然后将每一行添加到我的数据库中.
Now my goal is to read the excel
file and then add each row to my database.
但是我不太确定如何从 Node.js
读取文件我已经调试了我的服务器并且在任何地方都找不到该文件,但是 api 正在从我的 Angular
应用程序
However im not quite sure how i can read the file from Node.js
i have debugged my server and couldnt find the file anywhere but the the api is being called from my Angular
application
有人能把我推向正确的方向吗?:)
Can anyone push me in the right direction? :)
推荐答案
有几个不同的库可以解析 Excel 文件 (.xlsx).我将列出两个我觉得有趣且值得研究的项目.
There are a few different libraries doing parsing of Excel files (.xlsx). I will list two projects I find interesting and worth looking into.
Excel 解析器和构建器.它是一个流行项目的包装器JS-XLSX,其中是来自 Office Open XML 规范的纯 JavaScript 实现.
Excel parser and builder. It's kind of a wrapper for a popular project JS-XLSX, which is a pure javascript implementation from the Office Open XML spec.
解析文件的例子
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
ExcelJS
读取、操作电子表格数据和样式并将其写入 XLSX 和 JSON.这是一个活跃的项目.在撰写本文时,最近一次提交是在 9 小时前.我自己还没有测试过这个,但是这个 api 看起来很广泛,有很多可能性.
ExcelJS
Read, manipulate and write spreadsheet data and styles to XLSX and JSON. It's an active project. At the time of writing the latest commit was 9 hours ago. I haven't tested this myself, but the api looks extensive with a lot of possibilites.
代码示例:
// 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());
这篇关于使用 node.js 读取 Excel 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!