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

Nodejs operates excel files

php中世界最好的语言
Release: 2018-06-06 15:09:59
Original
3427 people have browsed it

This time I will bring you nodejs to operate excel files. What are the precautions for nodejs to operate excel files? The following is a practical case, let’s take a look.

/**
 * 安装node-xlsx插件
 */
var path = require('path')
var fs = require('fs')
var xlsx = require('node-xlsx')
//去重算法
Array.prototype.unique = function () {
 this.sort(); //先排序
 var res = [this[0]];
 for (var i = 1; i < this.length; i++) {
  if (this[i] !== res[res.length - 1]) {
   res.push(this[i]);
  }
 }
 return res;
}
//取得xlsx
var obj = xlsx.parse(path.resolve(`./xlsx/x.xlsx`))
var newArray = []
//读取第一列
//obj[0].data:指第一个sheet的表格数据
//data内部的数据结构为:
//[[ 'field1','field2','field13' ],[ 'field1','field2','field13' ]]
for (var data of obj[0].data) {
 newArray.push(data[0])
}
//去重之前
console.log(newArray.length)
var openIds = newArray.unique();
//去重之后
console.log(newArray.length)
var j = 0
for (var i = 0; i < newArray.length; i++) {
 //每一行
 console.log(newArray[i])
}
Copy after login

Analysis

The data structure exported by node-xlsx is as follows:

//json结构 
[{
 name: 'sheet1 name',
 data: [['field1', 'field2', 'field13'],
  ['field1', 'field2', 'field13']]
},
 {
  name: 'sheet2 name',
  data: [['field1', 'field2', 'field13'],
   ['field1', 'field2', 'field13']]
 }]
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese website Other related articles!

Recommended reading:

How to analyze the underlying logic of vue files

##What new features have been updated in vue-cli2.9.3

The above is the detailed content of Nodejs operates excel files. 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!