vue中向docx模板填充数据下载

1. 安装对应依赖
npm install docxtemplater pizzip --save-dev
npm install jszip-utils --save
npm install jszip --save
npm install file-saver --save
2. 导入依赖包
import docxtemplater from 'docxtemplater'
import PizZip from 'pizzip'
import JSZipUtils from 'jszip-utils'
import {saveAs} from 'file-saver'
3. 定义函数方法封装
import Docxtemplater from 'docxtemplater'
import PizZip from 'pizzip'
import JSZipUtils from 'jszip-utils'
import { saveAs } from 'file-saver'

/**
 4. 导出docx
 5. @param { String } tempDocxPath 模板文件路径
 6. @param { Object } data 文件中传入的数据
 7. @param { String } fileName 导出文件名称
*/
export const exportDocx = (tempDocxPath, data, fileName) => {
  // 读取并获得模板文件的二进制内容
  JSZipUtils.getBinaryContent(tempDocxPath, (error, content) => {
    if (error) {
      throw error
    }
    const zip = new PizZip(content)
    const doc = new Docxtemplater().loadZip(zip)
    doc.setData(data)
    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)
  })
}

4. 使用
4.1 准备temp.docx模板

在这里插入图片描述
vue-cli3以后一般放在public文件夹中
在这里插入图片描述

4.2 在页面中使用
1.导入封装的js
import { exportDocx } from '@/utils/docx.js'

2.调用的方法
 <el-button
                    type="text"
                    size="small"
                    class="el-icon-printer"
                    style="color:#93B762"
                    @click="print(scope.row)"
                  />
  // 打印
    print(row) {
      exportDocx('/temp.docx', row, '测试.docx')
    },

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue界面中设计一个表单,让用户输入相关数据,可以使用Vue的表单组件。具体步骤如下: 1. 在Vue组件的template模板中,使用Vue的表单组件,例如`<input>`、`<textarea>`、`<select>`等,让用户输入表单数据。 ```html <template> <form> <label for="title">标题</label> <input type="text" id="title" v-model="title" /> <label for="content">内容</label> <textarea id="content" v-model="content"></textarea> <label for="type">类型</label> <select id="type" v-model="type"> <option value="1">类型1</option> <option value="2">类型2</option> <option value="3">类型3</option> </select> <button type="button" @click="exportWord()">导出Word</button> </form> </template> ``` 2. 在Vue组件的`data`选项中,定义表单数据的初始值。 ```js export default { data() { return { title: '', content: '', type: '1' }; } }; ``` 3. 在Vue组件中,定义一个导出Word文档的方法`exportWord()`。该方法通过调用后台接口,传递表单数据,调用Word模板,将数据填入模板中,最后将填好数据的Word文档输出到前端。 ```js export default { methods: { exportWord() { // 通过axios调用后台接口,传递表单数据 axios.post('/api/exportWord', { title: this.title, content: this.content, type: this.type }, { responseType: 'blob' // 指定响应类型为二进制流 }).then(res => { // 将响应的二进制流文件转成Blob对象 const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }); // 创建一个a标签,设置下载链接和文件名,模拟点击下载 const a = document.createElement('a'); const fileName = `导出的Word文档-${new Date().toLocaleDateString()}.docx`; a.href = URL.createObjectURL(blob); a.download = fileName; a.click(); }).catch(error => { console.log(error); }); } } }; ``` 4. 在后台接口中,接收前端传递的表单数据,根据数据填充Word模板,最后将生成的Word文档以二进制流的形式返回给前端。 这里涉及到Word模板填充,可以使用第三方库docxtemplater来实现。具体步骤可以参考docxtemplater的官方文档。 总体的代码示例可以参考下面的代码: ```html <template> <form> <label for="title">标题</label> <input type="text" id="title" v-model="title" /> <label for="content">内容</label> <textarea id="content" v-model="content"></textarea> <label for="type">类型</label> <select id="type" v-model="type"> <option value="1">类型1</option> <option value="2">类型2</option> <option value="3">类型3</option> </select> <button type="button" @click="exportWord()">导出Word</button> </form> </template> <script> import axios from 'axios'; export default { data() { return { title: '', content: '', type: '1' }; }, methods: { exportWord() { axios.post('/api/exportWord', { title: this.title, content: this.content, type: this.type }, { responseType: 'blob' }).then(res => { const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }); const a = document.createElement('a'); const fileName = `导出的Word文档-${new Date().toLocaleDateString()}.docx`; a.href = URL.createObjectURL(blob); a.download = fileName; a.click(); }).catch(error => { console.log(error); }); } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值