当前位置: 首页 > news >正文

Node.Js 实现模板生成Word、Word转Pdf文件、Excel生成、上传和下载

第三方插件依赖

const XLSX = require('xlsx');
const Docxtemplater = require("docxtemplater"); // 引入Docxtemplater模块
const Enumerable = require('linq');
const fs = require('fs');
const PizZip = require("jszip"); // 引入PizZip=>jszip模块

function genFile(templatePath,outPath,data){if (!fs.existsSync(templatePath)) {fs.mkdirSync(templatePath, { recursive: true });// throw new Error("File not found");return;}var content = fs.readFileSync(templatePath, "binary");// 解压文件的内容const zip = new PizZip(content); // 创建一个新的PizZip实例// 解析模板,并在模板无效时抛出错误,例如,如果模板是"{user"(没有闭合标签)const doc = new Docxtemplater(zip, { // 创建Docxtemplater实例paragraphLoop: true, // 允许段落循环linebreaks: true, // 保持换行符});doc.render(data);// 获取zip文档并将其生成为Node.js缓冲区const buf = doc.getZip().generate({ // 生成文档的zip格式type: "nodebuffer", // 生成类型为nodebuffercompression: "DEFLATE", // 压缩类型为DEFLATE});fs.writeFileSync(outPath, buf); // 将渲染后的文档写入到downName.docx文件
}

模板文件参照

生成文件

excel文件如下

//移除不需要导出的列for(var obj of newQualityData){delete obj.deviceType;delete obj.qualityType;delete obj.sStation;delete obj.taskType;}const ws = XLSX.utils.json_to_sheet(newQualityData);var time=moment().format('x');//返回字符串型毫秒时间戳   (req.body.time).format("YYYYMMDDHHmmss")// 创建一个新的工作簿并添加工作表const wb = XLSX.utils.book_new();XLSX.utils.book_append_sheet(wb, ws, "Sheet1");// 写入文件// console.log(path.resolve(__dirname, '..'));//根目录// console.log(path.resolve(__dirname, '../'));//上一级目录var savePath=path.join(path.resolve(__dirname, '../')+"/download_file",`${time}.xlsx`);XLSX.writeFile(wb, savePath);res.send({code:200,res:true,str:`${time}.xlsx`});

上传和下载

//上传
router.all('/Common/Upload',async(req,res,next)=>{try{let ret_files=[];if (req.files){const allowedMimes = ['image/jpeg','image/png','image/gif','image/webp','application/pdf','text/plain','application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document'];// 允许的文件类型let files;files=req.files;for(let file of files){if (!allowedMimes.includes(file.mimetype)) {res.send({code:500,rc:false,str:'不支持的文件格式!'});} let file_ext=file.originalname.substring(file.originalname.lastIndexOf('.')+1);//文件后缀let file_name=moment().format('x')+"."+file_ext;//时间戳作为文件名称fs.renameSync(process.cwd()+"/download_file/upload/temp/"+file.filename,process.cwd()+"/download_file/upload/"+file_name);//移动文件兵修改文件名称ret_files.push({name:file_name,url:'/download_file/upload/'+file_name})}}res.send({code:200,rc:true,ret_files});}catch(error){console.log(error);res.send({ code:500,rc: false,str: error.message});}finally{}});//下载
router.all('/Common/Download',async(req,res,next)=>{try{res.download(process.cwd()+req.body.file_url);}catch(error){console.log(error);res.send({ code:500,rc: false,str: error.message});}finally{}});

http://www.dtcms.com/a/565081.html

相关文章:

  • 山西省建设监理协会官方网站好书推荐ppt模板免费下载
  • 机器学习中的Hello World:线性回归(一)
  • RT-DETR解码模块(Decoder)
  • 做炫舞情侣头像动态图网站建设公司网站怎么弄
  • 诚通凯胜生态建设有限公司网站莆田企业网站建设
  • IVWorks率先将8英寸GaN纳米线片商业化
  • ip网段扫描机器shell脚本
  • 智能家居品牌出海新打法:用海外网红营销讲好“未来生活剧本”
  • 做网站有效果吗网络推广公司方案
  • 基于ROS与YOLOv3的智能采购机器人设计(智能车创意组-讯飞智慧生活组)
  • 优化Elasticsearch批量写入性能:从单分片瓶颈到多索引架构
  • 高效文献阅读与笔记整理方法:从 筛选 到 3Pass 精读
  • 一步一步教你做网站后台视频广东新闻联播主持人
  • 如何用 Simulink 驱动 UE5 的海况切换
  • 多线程——并发三特性
  • 5.25基于labview交通灯+分时段控制红绿灯时间。手动输入控制labview交通灯labview版本2018,比我高都能打开。
  • 访问网站速度很慢discuz 做家教网站
  • 【树莓派5安装实时RT内核】最新版
  • CANN自定义算子开发实战:Softmax优化全流程解析
  • XAI 面经分享|从模型解释到面试考察,全流程复盘
  • 免费做简历下载的网站wordpress thetime
  • python实现象棋
  • 开发巴西外贸客户有什么好方法
  • 安徽省住房和城乡建设厅网站查询seo自学教程
  • 导入权重和偏置的感知机
  • 计网5.3.4 TCP可靠传输、流量控制
  • Rust编程学习 - 如何使用 `macro_rules!` 的声明宏用于通用元编程
  • GEO排名监测那个平台工具好用?GEO搜索营销什么工具好
  • 在 此芯P1 的开发板 瑞莎O6 平台上启用 NVIDIA RTX 显卡与 CUDA 驱动全流程指南
  • tcp large-receive-offload