开整
环境 : node.js
框架 :
dependencies: { node-xlsx: ^0.11.2 }
逻辑 :
excel表格样式
期待输出样式
如上两图展示那样,直接取出相应的值拼接成想到的输出格式。
上代码
'use strict' var xlsx = require('node-xlsx'); var fs = require('fs'); var path = require('path') var files = path.resolve(__dirname); var excelpath; fs.readdir(files ,function (error,allfiles){ if(error){ console.log(error); }else{ allfiles.foreach(function(filename){ var filedir = path.join(files,filename); fs.stat(filedir,function(error,stats){ if(error){ console.log(error); }else{ // console.log(filedir); if (filedir.indexof('xlsx') > 0){ excelpath = filedir; craeatlanguagetext(); } } }); }) } }); function craeatlanguagetext(){ console.log(excelpath); //读取文件内容 var obj = xlsx.parse(excelpath); var excelobj = obj[0].data; var data = []; var arr = [];// 语言种类 for (var i in excelobj) { var value = excelobj[i]; for (var j in value) { if (i == 0) { if (j > 1) { arr.push(value[j]); } } else { if (j > 1) { var str = arr[j - 2]; var item = value[0] var vaue = value[j]; var reg = /'/ vaue = vaue.replace(reg, '); str = str + '\n' + '' + item + '' + ' ' + '=' + + '' + vaue + '' + ';'; arr[j - 2] = str; console.log(str); } } } } var languagestr = arr.join('\n'); //将文件内容插入新的文件中 fs.writefilesync('language.text', languagestr, { encoding: utf8 }); }
逻辑比较容易看懂,不再赘述,直接在node项目文件夹拖入要解析的excel 运行即可。
相关推荐:
利用python将图片转换成excel文档格式详解
node之excel内容的获取
php中通用的excel导出方法实例
以上就是node解决excel内容的获取问题的详细内容。
