vue若依导出word文件,简单的实现

首先前端导包,注意exportDocx的导包位置要修改成你自己的

html 复制代码
import {exportDocx} from '@/utils/docUtil/docutil.js'; 
import {addDays} from 'date-fns';
import {listGongyi} from "@/api/system/detail";

然后新建一个测试按钮

html 复制代码
 <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="generateWordDoc"
        >导出
        </el-button>
      </el-col>

接下来是js文件

js 复制代码
    generateWordDoc(row) {
      const ids = row.id || this.ids;
      //查找后台数据库封装数据
      find(ids).then(response => {
        console.log(response.data)
        const data = {
          form: response.data,
          list: response.data.aaZzcgPurchases
        }
		//模板文件位置在public文件夹里
        exportDocx('/caigoudan.docx', data, `caigoudan.docx`)
      });
    }

然后将下面这个docutil.js文件复制到项目中,我是复制在utils/docUtil中

js 复制代码
import docxtemplater from 'docxtemplater';
import PizZip from 'pizzip';
import JSZipUtils from 'jszip-utils';
import {saveAs} from 'file-saver';

/**
 导出docx,导出word
 @param { String } tempDocxPath 模板文件路径
 @param { Object } data 文件中传入的数据
 @param { String } fileName 导出文件名称
 */
export const exportDocx = (tempDocxPath, data, fileName) => {
  // 读取并获得模板文件的二进制内容

  JSZipUtils.getBinaryContent(tempDocxPath, (error, content) => {


    // input.docx是模板。我们在导出的时候,会根据此模板来导出对应的数据
    // 抛出异常
    if (error) {
      throw error
    }
    // 创建一个JSZip实例,内容为模板的内容
    const zip = new PizZip(content)
    // 创建并加载docxtemplater实例对象
    const doc = new docxtemplater().loadZip(zip)
    // 设置模板变量的值
    doc.setData({
      ...data.form,
      list: data.list
    })

    try {
      // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
      doc.render()
    } catch (error) {
      const e = {
        message: error.message,
        name: error.name,
        stack: error.stack,
        properties: error.properties
      }
      console.log({
        error: e
      })
      // The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
      throw error
    }
    const out = doc.getZip().generate({
      type: 'blob',
      mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    }) // Output the document using Data-URI
    saveAs(out, fileName)
  })
}

最后新建一个word模板文件,保存的格式要是docx才行哦

例如:

只要数据是集合,就得是{#list} 开头{/list}结尾

如果还有其他的集合,则自己修改data的值,记得配合修改:(自己研究一下立马就懂了)

另外自己的模板如果不弄页眉页脚的话,第二页是不显示页眉页脚的,所以要注意

注意,此模板文件必须放在项目中的public文件夹下 !!!

相关推荐
fruge2 分钟前
Canvas/SVG 冷门用法:实现动态背景与简易数据可视化
前端·信息可视化
一 乐7 分钟前
旅游|内蒙古景点旅游|基于Springboot+Vue的内蒙古景点旅游管理系统设计与实现(源码+数据库+文档)
开发语言·前端·数据库·vue.js·spring boot·后端·旅游
驯狼小羊羔18 分钟前
学习随笔-require和import
前端·学习
excel25 分钟前
🚀 从 GPT-5 流式输出看现代前端的流式请求机制(Koa 实现版)
前端
凸头29 分钟前
Spring Boot接收前端参数的注解总结
前端·spring boot·后端
爱吃甜品的糯米团子1 小时前
JavaScript 正则表达式:选择、分组与引用深度解析
前端·javascript·正则表达式
excel1 小时前
Vue SSR 编译器源码深析:ssrTransformShow 的实现原理与设计哲学
前端
excel1 小时前
深入解析 Vue 3 SSR 编译管线:ssrCodegenTransform 源码全解
前端
excel1 小时前
深入解析 Vue SSR 编译器的核心函数:compile
前端
IT_陈寒1 小时前
Vue 3性能优化实战:7个关键技巧让我的应用加载速度提升50%
前端·人工智能·后端