好的,我使用FileUploader模块将文件从角上传到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文件,然后将每一行添加到我的数据库中。
但是,我不太确定如何从Node.js中读取文件,我已经调试了服务器,在任何地方都找不到文件,但是api是从我的Angular应用程序中调用的。
有人能把我推向正确的方向吗?)
发布于 2015-03-04 19:17:12
有几个不同的库在解析Excel文件(.xlsx)。我会列出两个我觉得有趣和值得研究的项目。
节点-xlsx
Excel解析器和生成器。它是流行项目https://github.com/SheetJS/js-xlsx的包装器,它是Office规范的纯javascript实现。
https://github.com/mgcrea/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 bufferExcelJS
将电子表格数据和样式读取、操作和写入到XLSX和JSON。这是一个积极的项目。在撰写本报告时,最近一次提交是在9小时前。我自己还没有测试过这一点,但是api看起来很广泛,有很多可能性。
https://github.com/guyonroche/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());发布于 2017-07-26 08:27:13
您还可以使用这个称为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);发布于 2018-06-29 07:08:19
安装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);
}
});https://stackoverflow.com/questions/28860728
复制相似问题