Home > Web Front-end > JS Tutorial > body text

Obtaining Excel content from Node

小云云
Release: 2018-01-03 09:36:52
Original
1490 people have browsed it

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"
 }
Copy after login

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 desired output format.

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" });
}
Copy after login

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:

php excel reader reads excel content and stores it in the database implementation code_PHP tutorial

Share link php+ajax implements content acquisition and dynamic modification

Share linkphp+ajax implements content acquisition and dynamic modification solution

The above is the detailed content of Obtaining Excel content from Node. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!