Home > Web Front-end > JS Tutorial > Nodejs simple method example of reading and writing excel content

Nodejs simple method example of reading and writing excel content

亚连
Release: 2018-05-29 18:02:58
Original
3094 people have browsed it

This article mainly introduces the method of nodejs to simply read and write excel content, briefly analyzes the common nodejs reading and writing Excel modules, and analyzes the specific operation skills of nodejs reading and writing Excel in the form of examples. Friends who need it can refer to it

The example in this article describes how nodejs can simply read and write excel content. Share it with everyone for your reference, the details are as follows:

The node.js module that supports reading and writing Excel

node-xlsx: Based on Node.js parsing excel file data and Generate excel files, only supports xlsx format files;
excel-parser: Parses excel file data based on Node.js, supports xls and xlsx format files;
excel-export: Generate and export data to excel files based on Node.js. The generated file format is xlsx;
node-xlrd: Extract data from excel files based on node.js. Only xls format files are supported.

I will show how to extract the data in the uploaded excel file through node-xlsx and generate a new excel file. The code is as follows:


var xlsx = require('node-xlsx');
var fs = require('fs');
//读取文件内容
var obj = xlsx.parse(__dirname+'/test.xlsx');
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);
}
var buffer = xlsx.build([
  {
    name:'sheet1',
    data:data
  }
]);
//将文件内容插入新的文件中
fs.writeFileSync('test1.xlsx',buffer,{'flag':'w'});
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

The secret behind JavaScript EventEmitter

Understanding JavaScript EventEmitter

Vue implements the function of exporting excel tables


The above is the detailed content of Nodejs simple method example of reading and writing excel content. 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