1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| var path = require("path"); var fs = require("fs"); var xlsx = require('node-xlsx');
var dirs = [];
fs.readdir(__dirname+'/excel/', function(err, files){ (function iterator(i){ if(i == files.length) { console.log('所有文件在一个数组里:',dirs); for(let i1 =0;i1<dirs.length;i1++) { let ss = dirs[i1].split("."); console.log('把文件名提取出来:',ss[0]);
var obj = xlsx.parse(__dirname+'/excel/'+dirs[i1]); var excelObj=obj[0].data; console.log(excelObj); 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); }
console.log('data:',data);
var array_json = {}; var array_json_index = [];
for(let i=1,j=0;i<data.length;i++,j++) { array_json_index[j] = data[i]; }
console.log('array_json_index:',array_json_index);
for(let i=1;i<array_json_index.length;i++) { array_json[array_json_index[i][0]] = {}; console.log('array_json_index[0].length',array_json_index[0].length); for(let j=1;j<array_json_index[0].length;j++) { array_json[array_json_index[i][0]][array_json_index[0][j]] = array_json_index[i][j]; } } console.log('array_json:',array_json);
let str = JSON.stringify(array_json) let dir_json = ss[0]+'.json'; fs.writeFile(__dirname+'/excel/'+dir_json,str,function(err){ if (err) {res.status(500).send('Server is error...')} }) }
return ; } fs.stat(path.join(__dirname+'/excel/', files[i]), function(err, data){ if(data.isFile()){ dirs.push(files[i]); } iterator(i+1); }); })(0); });
|