Beginning briefly, this is a read and write Excel, xlsx functions, mainly to help us to print or read some of the data.
Scenario: The main scenario is users need to export data, the similar we consume several years of money on a website, we need to look at running account, we as developers can be data corresponding finishing node of the service layer then give to customers through a written Excel.
step1: Load Module
Use Module: fs-- mainly for writing files;
node-xlsx xlsx primarily used to convert the data file and reading of data xlsx;
the require FS = const ( 'FS') const = xlsx the require ( 'Node-xlsx'); // read xlsx plug
step2: reading function
Reading function is relatively simple, direct use Prase module (path) can be read Method
var list = xlsx.parse("./doc/hello.xlsx");
Return results, name: table name, data: two-dimensional array - due Excel expressed in tabular form, the form data is thus receives two-dimensional array.
[ { name: 'firstSheet', data: [ [Array], [Array] ] }, { name: 'secondSheet', data: [ [Array], [Array] ] } ]
step3: write function
Write function, the write data corresponding to the above read function returns the format needs to be consistent, in the form of an array, name: data per row and two-dimensional array --Excel: table name, data.
The code below, generates an Excel file, are two tables, each table name is firstSheet, secondSheet
let xlsxObj = [ { name: 'firstSheet', data: [ [1, 2, 3], [4, 5, 6] ], }, { name: 'secondSheet', data: [ [7, 8, 9], [10, 1, 12] ], } ]
Generate Excel files
fs.writeFileSync (path, Buffer data)
xlsx.build (xlsxObj) the array will be converted into data Buffer
Execute the following command will generate a corresponding Excel file.
fs.writeFileSync('./doc/hello.xlsx',xlsx.build(xlsxObj),"binary");
These are the write operation by reading the new node to the Excel spreadsheet carried out.