支持读写Excel的node.js模块
node-xlsx: 基于Node.js解析excel文件数据及生成excel文件,仅支持xlsx格式文件;
excel-parser: 基于Node.js解析excel文件数据,支持xls及xlsx格式文件;
excel-export : 基于Node.js将数据生成导出excel文件,生成文件格式为xlsx;
node-xlrd: 基于node.js从excel文件中提取数据,仅支持xls格式文件。
我将展示通过node-xlsx提取上传上来的excel文件里的数据,以及生成新的excel文件。代码如下:
下面代码来源于:http://xfenglin.com/a/12000904485.html
var xlsx = require('node-xlsx');
var fs = require('fs');
//读取文件内容
var obj = xlsx.parse(__dirname+'/test.xlsx');//配置excel文件的路径
var excelObj=obj[0].data;//excelObj是excel文件里第一个sheet文档的数据,obj[i].data表示excel文件第i+1个sheet文档的全部内容
console.log(excelObj);
//一个sheet文档中的内容包含sheet表头 一个excelObj表示一个二维数组,excelObj[i]表示sheet文档中第i+1行的数据集(一行的数据也是数组形式,访问从索引0开始)
var data = [];
for(var i in excelObj){
var arr=[];
var value=excelObj[i];
for(var j in value){
arr.push(value[j]);
}
data.push(arr);
}
var buffer = xlsx.build([
{
name:'sheet1',
data:data
}
]);
//将文件内容插入新的文件中
fs.writeFileSync('test1.xlsx',buffer,{'flag':'w'});
或者参考下面代码(来源于:http://www.itnose.net/detail/6290038.html)
var express = require('express');
var router = express.Router();
var xlsx = require('node-xlsx');
var fs = require('fs');
/* GET import excel test. */
router.get('/importExcel', function(req, res, next) {
var filename='./public/test.xlsx';
console.error(filename);
// read from a file
var obj = xlsx.parse(filename);
console.log(JSON.stringify(obj));
res.send('import successfully!');
});
/* GET export excel test. */
router.get('/exportExcel', function(req, res, next) {
// write
var data = [[1,2,3],[true, false, null, 'sheetjs'],['foo','bar',new Date('2014-02-19T14:30Z'), '0.3'], ['baz', null, 'qux']];
var buffer = xlsx.build([{name: "mySheetName", data: data}]);
fs.writeFileSync('b.xlsx', buffer, 'binary');
res.send('export successfully!');
});
Excel文件的读写可能用到的模块:
node-xlsx 模块:https://github.com/mgcrea/node-xlsx
node-fs-extra模块:https://github.com/jprichardson/node-fs-extra
node-fs模块:https://www.npmjs.com/package/node-fs
代码示例:
var excelObj = obj[0].data;
console.log("start read customer_exchange excel ..");
//将整个二维数组中空的单元格置空处理(不置空的话,读取时undefined报错),处理一些脏数据
for (var i = 1; i < excelObj.length; i++)
{
for (var j = 0; j < excelObj[0].length; j++)
{
//处理excel表格中的空数据和脏数据
if (excelObj[i][j] == undefined || excelObj[i][j] == '/')
{
excelObj[i][j] = '';
}
}
}
var bagpipe = new Bagpipe(1, {
timeout: 1000
});
var customer_exchange_handle = function (index, item, callback) {
var data = {};
data.external_model = item[0];
data.internal_model = item[1];
data.color = item[2];
data.imei = item[3];
data.mark_defects = item[4];
data.function_check = item[5];
data.qc_note = item[6];
data.fault_classification = item[7];
data.fault_model = item[8];
data.device = item[9];
data.preliminary_analyze = item[10];
data.vendor = item[11];
data.software_version = item[12];
data.is_external_repair = item[13];
data.analyze_date = item[14];
data.note = item[15];
data.sn_number = item[16];
data.purchase_date = item[17];
data.phone_state = item[18];
data.push_warehouse_date = item[19];
data.user_information = item[20];
data.pcb = item[21];
data.patch_date = item[22];
data.handler = item[23];
data.accessory_lack_situation = item[24];
data.phone_or_whole_machine = item[25];
data.region = item[26];
data.tester = item[27];
data.facade_check = item[28];
data.last_reviser = user.name;
data.last_revise_time = (new Date()).toLocaleString();
data.creater = user.name;
data.creation_time = (new Date()).toLocaleString();
DeviceItem.save('customer_exchange', data, function (err, is_ok) {
if (err) {
console.log("customer_exchange save " + index + " error");
callback(index, err, null);
}
if (is_ok) {
console.log("customer_exchange save " + index + " success");
callback(index, null, is_ok);
}
});
};
//创建循环队列
for (var t = 1; t < excelObj.length; t++)
{
bagpipe.push(customer_exchange_handle, t, excelObj[t], function (index, err, is_ok) {
if (err) {
//console.log("客退售后记录 第"+index+" 条保存失败");
}
if (is_ok) {
//console.log("客退售后记录 第"+index+" 条保存成功");
}
if (index == excelObj.length - 1) {
console.log("customer_exchange_data save complete!");
}
});
}
//监测队列是否阻塞
bagpipe.on('full', function (length) {
console.warn('爬虫队列阻塞,延迟排队,长度为:' + length);
});