如何创建一个快速创建 Excel 文件的 Nodejs 工具
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何创建一个快速创建 Excel 文件的 Nodejs 工具,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3148字,纯文字阅读大概需要5分钟。
内容图文
![如何创建一个快速创建 Excel 文件的 Nodejs 工具](/upload/InfoBanner/zyjiaocheng/593/5eb087c1680942e7be67b49822537682.jpg)
核心使用 node-xlsx
模块
1.定义 ExcelCreator.js
模块
const xlsx = require('node-xlsx').default; const fs = require('fs'); class ExcelCreator{ constructor(opts) { let {filePath,sheets} = opts; this.filePath_ = filePath; this.sheets_ = sheets; } build(){ let buffer = xlsx.build(this.sheets_); // Returns a buffer fs.writeFile(this.filePath_, buffer, "binary",function(err) { }); } } module.exports = ExcelCreator;
2.定义 SheetDataCreator.js
模块
class SheetDataCreator{ constructor(opts = {}) { let {rowLen,colLen,cell,calcConf} = opts; this.rowLen_ = rowLen; this.colLen_ = colLen; this.cell_ = cell; this.calcConf_ = calcConf; this.cellTextLength_ = this.cell_ && this.cell_.text.length; } getCellTextDesc(){ if(this.cell_.textFixed){ return `文本固定${this.cell_.text.length}个字符`; } return ''; } buildFileName(opts = {}){ const {fileExt = 'xlsx'} = opts; const prefix = `EP-${this.rowLen_}行-${this.colLen_}列-${this.rowLen_*this.colLen_}个单元格`; if(this.calcConf_){ return `${prefix}包含计算公式${this.calcConf_.expressing}.${fileExt}` } return `${prefix}${this.getCellTextDesc()}.${fileExt}` } getRandomCellDataForCalc(rowIndex,colIndex){ if(rowIndex===0){ return colIndex; } return {f:`=${this.calcConf_.expressing}(A1:L1)`}; } getRandomCellData(rowIndex,colIndex){ if(this.calcConf_){ return this.getRandomCellDataForCalc(rowIndex,colIndex); } if(this.cell_ && this.cell_.text){ if(this.cell_.textFixed){ return this.cell_.text; } if(this.cellTextLength_){ let end = parseInt(Math.random(0,1) * this.cellTextLength_); return this.cell_.text.substr(parseInt(end*Math.random(0,1)), end); } } } getCellsData(){ let result = []; for (let i = 0; i < this.rowLen_; i++) { let rowData = []; for (let j = 0; j < this.colLen_; j++) { rowData.push(this.getRandomCellData(i,j)) } result.push(rowData); } return result; } buildSheets(){ let cellsData = this.getCellsData(); let result = [{ name: "mySheetName", data: cellsData }]; // console.log(result) return result; } } module.exports = SheetDataCreator;
3.按指定特性创建 xlsx
文件
const fs = require('fs'); const path = require('path'); const ExcelCreator = require('./lib/ExcelCreator'); const SheetDataCreator = require('./lib/SheetDataCreator'); const sd = new SheetDataCreator({ "rowLen": 2500, //行数 "colLen": 10, //列数 "calcConf": { "expressing": "SUM" //计算公式配置 }, "cell": { text: 'none' //文本内容 } }); let ec = new ExcelCreator({ filePath: path.join('cal-output', sd.buildFileName({ fileExt: 'xlsx', })), //配置输出目录 sheets: sd.buildSheets() //构建表格 }); ec.build(); //执行任务
内容总结
以上是互联网集市为您收集整理的如何创建一个快速创建 Excel 文件的 Nodejs 工具全部内容,希望文章能够帮你解决如何创建一个快速创建 Excel 文件的 Nodejs 工具所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。