当前位置:网站首页>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);
}
边栏推荐
- Triangle class (construction and deconstruction)
- What if the desktop computer is not connected to WiFi
- WPF uses Maui's self drawing logic
- Visual studio 2022 interface beautification tutorial
- Eyeshot Ultimate 2022 Crack By Xacker
- My IC journey - the growth of senior chip design verification engineers - "Hu" said that IC engineers are perfect and advanced
- February 19 CTF exercise
- How to install the blue lake plug-in to support Photoshop CC 2017
- PHP uses JWT
- SRC platform summary
猜你喜欢

Wechat applet new version prompt update

Region of Halcon: generation of multiple regions (3)

TX Text Control 30.0 ActiveX

dotnet-exec 0.4.0 released

Go deep into the working principle of browser and JS engine (V8 engine as an example)

Difference between asemi high power FET and triode

Startup mode of SoC verification environment

Attack and defense world web baby Web

CSRF (Cross Site Request Forgery) &ssrf (server request forgery) (IV)

小白一键重装官网下载使用方法
随机推荐
Difference between asemi high power FET and triode
Route parameters to jump to the page and transfer parameters -- > hidden parameter list
基于SSH实现的学生成绩管理系统
渗透测试-目录遍历漏洞
基于Cortex-M3、M4的精准延时(系统定时器SysTick延时,可用于STM32、ADuCM4050等)
Implementation of websocket long connection by workman under laravel
Apache+php uploading large files
Introduction to the hardest core PWN in the whole network_ Graphic analysis
EL & JSTL (XIII)
Everything is an object
Student achievement management system based on SSH
Visual studio 2022 interface beautification tutorial
PHP calls map API
Five simple data types of JS
Database query optimization method
Leader: who can use redis expired monitoring to close orders and get out of here!
great! Auto like, I use pyautogui!
Array: force deduction dichotomy
File upload vulnerability (III)
buuctf web