使用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
函数:
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
读取文件我调试了我的服务器并且无法在任何地方找到该文件,但是从我的 Angular $调用了api c $ c>申请
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屋!