当前位置:网站首页>Reading and writing of nodejs excel (.Xlsx) files
Reading and writing of nodejs excel (.Xlsx) files
2022-06-25 05:05:00 【Yan Xiaoxi】
List of articles
data.xlsx| name | Age |
|---|---|
| Zhang San | 18 |
| Li Si | 19 |
| Wang Wu | 20 |
get data
import xlsx from "xlsx";
const workBook = xlsx.readFile("./data.xlsx");
Get the first one execle Workbook table
let name = workBook.SheetNames[0]
let sheet = workBook.Sheets[name]
1. Output json Format
console.log(xlsx.utils.sheet_to_json(sheet));
[
{
name :" Zhang San ",
Age :"18"
},
{
name :" Li Si ",
Age :"19"
},
{
name :" Wang Wu ",
Age :"20"
}
]
2. Output csv Format
console.log(xlsx.utils.sheet_to_csv(sheet));
name , Age ,,,,,,,,,,,,,,,,,
Zhang San ,18,,,,,,,,,,,,,,,,,
Li Si ,19,,,,,,,,,,,,,,,,,
Wang Wu ,20,,,,,,,,,,,,,,,,,
3. Output html Format
console.log(xlsx.utils.sheet_to_html(sheet));
<html><head><meta charset="utf-8"/><title>SheetJS Table Export</title></head><body><table><tr><td data-t="s" data-v=" name " id="sjs-A1"> name </td><td data-t="s" data-v=" Age " id="sjs-B1"> Age </td></tr><tr><td data-t="s" data-v=" Zhang San " id="sjs-A2"> Zhang San </td><td data-t="n" data-v="18" id="sjs-B2">18</td></tr><tr><td data-t="s" data-v=" Li Si " id="sjs-A3"> Li Si </td><td data-t="n" data-v="19" id="sjs-B3">19</td></tr><tr><td data-t="s" data-v=" Wang Wu " id="sjs-A4"> Wang Wu </td><td data-t="n" data-v="20" id="sjs-B4">20</td></tr></table></body></html>

4. Output formulae Format
console.log(xlsx.utils.sheet_to_formulae(sheet));
[
"A1=' name ", "B1=' Age ",
"A2=' Zhang San ", 'B2=18',
"A3=' Li Si ", 'B3=19',
"A4=' Wang Wu ", 'B4=20'
]
Output to file
xlsx.writeFile(workBook, "./output.xlsx");

All the code
import xlsx from "xlsx";
const workBook = xlsx.readFile("./data.xlsx");
let name = workBook.SheetNames[0];
let sheet = workBook.Sheets[name];
console.log(xlsx.utils.sheet_to_json(sheet));
console.log(xlsx.utils.sheet_to_csv(sheet));
console.log(xlsx.utils.sheet_to_html(sheet));
console.log(xlsx.utils.sheet_to_formulae(sheet));
xlsx.writeFile(workBook, "./output.xlsx");
Method encapsulation
import xlsx from "xlsx";
/** * analysis excel File into json object * @param {string} filePath */
export function parseExcel(filePath) {
// Read the file and parse the workbook
const workBook = xlsx.readFile(filePath);
// Get the first table
let name = workBook.SheetNames[0];
// Analysis table
return xlsx.utils.sheet_to_json(workBook.Sheets[name]);
}
/** * Parse object data into excel * @param {Array} list * @param {string} sheetName The name of the form , Default Sheet1 */
export function writeExcel(list, sheetName = "Sheet1") {
// New workbook
const workBook = xlsx.utils.book_new();
// Add table
xlsx.utils.book_append_sheet(workBook, xlsx.utils.json_to_sheet(list), sheetName);
// The output file
xlsx.writeFile(workBook, filePath);
}
边栏推荐
- DOM document object model (I)
- Ctfhub eggs
- Write shell script error summary
- February 19 CTF exercise
- Go deep into the working principle of browser and JS engine (V8 engine as an example)
- PHP uses JWT
- Compatible with Internet Explorer
- For in JS Of and for in
- How do the defi protocols perform under this round of stress test?
- SQL lab range explanation
猜你喜欢

ThinkPHP 5 log management

MySQL prevents Chinese garbled code and solves the problem of Chinese garbled code

CTFHUB SSRF

TX Text Control 30.0 ActiveX

Summary of SQL injection (I)

WPF uses Maui's self drawing logic

What is Ethernet and how to connect the computer

What if the desktop computer is not connected to WiFi

win11蓝牙无法连接怎么办?win11蓝牙无法连接的解决方法

Five simple data types of JS
随机推荐
【FLink】access closed classloader classloader. check-leaked-classloader
TeeChart Pro ActiveX 2022.1
CopyPlugin Invalid Options options should be array ValidationError: CopyPlugin Invalid Options
Calculate student grade (virtual function and polymorphism)
基于SSH实现的学生成绩管理系统
Implementation of websocket long connection by workman under laravel
How to install the blue lake plug-in to support Photoshop CC 2017
DMA double buffer mode of stm32
IronOCR 2022.1 Crack
Small sample learning data set
Go deep into the working principle of browser and JS engine (V8 engine as an example)
ASEMI大功率场效应管和三极管的区别
以太网是什么要怎么连接电脑
February 20ctf record
本轮压力测试下,DeFi协议们表现如何?
魔法猪系统重装大师怎么使用
Basic knowledge of web pages (URL related)
Response (XI)
XSS (cross site script attack) summary (II)
Use js to simply implement the apply, call and bind methods