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

vue纯前端根据页面或者后台数据,读取本地文档模板,填充数据后并导出

最近接了个活,需要根据表格数据和用户给定的文档模板,生成报告,因为没得后端,所以做了一个纯前端的生成报告并下载,现实如下:

纯前端根据页面或者后台数据,读取本地文档模板,填充数据后并导出

1、技术 vue3 + element-plus 或者 vue2 + element-ui
2、安装依赖

npm install docxtemplater
npm install pizzip
npm install file-saver

3、模板文件,以doc为例,放在本地public
在这里插入图片描述
4、具体代码实现(复制可用)

<!-- 前端实现,获取本地文档模板,在页面上根据数据,导出一个有数据的word -->
<template><el-table :data="tableData" style="width: 100%"><el-table-column label="Date" width="180"><template #default="scope"><div style="display: flex; align-items: center"><el-icon><timer /></el-icon><span style="margin-left: 10px">{{ scope.row.date }}</span></div></template></el-table-column><el-table-column label="Name" width="180"><template #default="scope"><el-popover effect="light" trigger="hover" placement="top" width="auto"><template #default><div>name: {{ scope.row.name }}</div><div>address: {{ scope.row.address }}</div></template><template #reference><el-tag>{{ scope.row.name }}</el-tag></template></el-popover></template></el-table-column><el-table-column label="Operations"><template #default="scope"><el-button size="small" @click="generateReport(scope.row)">生成报告</el-button><el-button size="small" @click="handleEdit(scope.$index, scope.row)">Edit</el-button><el-button  size="small"  type="danger"  @click="handleDelete(scope.$index, scope.row)">Delete</el-button></template></el-table-column></el-table>
</template><script setup>
// import Docxtemplater from 'docxtemplater'
// import PizZip from 'pizzip'
import { saveAs } from 'file-saver'const tableData = [{date: '2016-05-03',name: 'Tom',address: 'No. 189, Grove St, Los Angeles',},{date: '2016-05-02',name: 'Tom',address: 'No. 189, Grove St, Los Angeles',},{date: '2016-05-04',name: 'Tom',address: 'No. 189, Grove St, Los Angeles',},{date: '2016-05-01',name: 'Tom',address: 'No. 189, Grove St, Los Angeles',},
]
const handleEdit = (index,row ) => {console.log(index, row)
}
const handleDelete = (index, row) => {console.log(index, row)
}const generateReport = async (row) => {console.log(row)try {// 1. 加载Word模板文件console.log(window.origin)const response = await fetch('/mb.docx')const arrayBuffer = await response.arrayBuffer()// 2. 初始化docxtemplater(老版本)// const zip = new PizZip(arrayBuffer)// const doc = new Docxtemplater().loadZip(zip)//新const PizZip = require('pizzip');const Docxtemplater = require('docxtemplater');const zip = new PizZip(arrayBuffer);const doc = new Docxtemplater(zip);// 3. 准备模板数据(根据你的Word模板结构调整)let templateData = {title: '数据分析报告',name:row.name,date:row.date,address:row.address,}// 4. 渲染模板doc.render(templateData)// 5. 生成并下载文件const out = doc.getZip().generate({ type: 'blob' })saveAs(out, `数据分析报告_${new Date().getTime()}.docx`)} catch (error) {console.error('生成报告失败:', error)}
}
</script>

5、点击生成报告结果如下
在这里插入图片描述

相关文章:

  • KDJ指标的运用
  • VIC-3D应用指南系列之:DIC数字图像相关技术与热成像(VIC-3D IR System助力热载荷测试)
  • 访问者模式:解耦数据结构与操作的优雅之道
  • 数字IC后端实现之Innovus中各种cell名字前缀物理含义
  • Vue3 Element Plus 表格默认显示一行
  • GitHub Actions 深度实践:零运维搭建 CI/CD 流水线
  • React 第六十一节 Router 中 createMemoryRouter的使用详解及案例注意事项
  • SpringBoot+vue前后端分离系统开发(期末)
  • ios 26发布:设计革新与智能整合
  • 26考研 | 王道 | 计算机组成原理 | 六、总线
  • 栈与队列:数据结构优劣全解析
  • react react-router-dom中获取自定义参数v6.4版本之后
  • 域名+nginx反向代理实现案例
  • frida对qt5(32位)实现简单HOOK
  • Oracle 中使用CONNECT BY、START WITH递归查询
  • Dockerfile - 自定义 Nginx 镜像构建
  • 面向对象-对象和属性描述符详解(一)
  • 使用HashMap或者List模拟数据库插入和查询数据
  • java集合(十) ---- LinkedList 类
  • 精准洞察位移变化,位移传感器开启测量新纪元
  • 做建筑钢材的b2b网站有哪些/小游戏推广接单平台
  • 大型网站开发方案/seo推广代理
  • php与动态网站建设/软文案例大全
  • 鲜花团购网站建设/宣传网站怎么做
  • 中国住房和城乡建设部网站首页/黄石市seo关键词优化怎么做
  • 学校室内设计效果图/优化疫情防控措施