Home > Article > Web Front-end > Node solves the problem of obtaining Excel content
This article mainly introduces to you the relevant information about using Node to solve a series of simple repetitive problems and obtaining Excel content. The article introduces it in great detail through sample code. It has certain reference learning value for everyone's study or work. Friends who need it Let’s learn together with the editor below.
开户
Environment: Node.js
Framework:
"dependencies": { "node-xlsx": "^0.11.2" }
Logic:
Excel table style
Expected output style
As shown in the above two figures, directly take out the corresponding values and splice them into The output format comes to mind.
The above code
'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" }); }
The logic is relatively easy to understand, so I won’t go into details. Just drag the Excel to be parsed into the node project folder and run it.
Related recommendations:
Using python to convert pictures into excel document format Detailed explanation
Getting the Excel content of Node
Example of common excel export method in php
The above is the detailed content of Node solves the problem of obtaining Excel content. For more information, please follow other related articles on the PHP Chinese website!