https://github.com/sail-sail/ejsExcel
const ejsexcel = require("ejsexcel");const fs = require('fs');const path = require("path");function writeExcel(data) {const templateXls = fs.readFileSync(path.resolve(__dirname, '../public/模板.xlsx'));//渲染Excel表格ejsexcel.renderExcel(templateXls, data).then(function (exlBuf) {//写入文件 指定文件名fs.writeFileSync(path.resolve(__dirname, `../public/CMCC.xlsx`), exlBuf);console.log(`生成:xlsx成功`);}).catch(function (err) {console.error(err);});}
示例输入:
const data = {num:1,list:[{name:'zhang',age:18},{name:'li',age:15}]}
模板.xlsx示例如下
| 今日有<%~data.num%>个 | ||
|---|---|---|
| 序号 | 姓名 | 年龄 |
| <%forRow item, i in data.list%><%~i+1%> | <%=item.name%> | <%~item.age%> |
输出:
| 今日有1个 | ||
|---|---|---|
| 序号 | 姓名 | 年龄 |
| 1 | zhang | 18 |
| 2 | li | 15 |
