vue docxtemplater导出前端模板文件

效果:

 

一、安装

npm install docxtemplater
npm install pizzip
npm install file-saver
npm install jszip-utils
npm install docxtemplater-image-module-free

二、代码结构

三、代码

exportWord.js

import docxtemplater from 'docxtemplater'
import PizZip from 'pizzip'
import JSZipUtils from 'jszip-utils'
import { saveAs } from 'file-saver'

//word文件生成
export default {
  data() {
       return {}
  },
  methods: {
    exportWord(docxsrc, docxname, fullData){
        this.action(docxsrc, docxname, fullData);
    },
    //模板文件的位置 docxsrc
    //导出文件的名字 docxname
    //数据填充对象 fullData
    action(docxsrc, docxname, fullData) {
        // 读取并获得模板文件的二进制内容
        JSZipUtils.getBinaryContent(docxsrc, function(error, content) {
          if (error) {
            throw error;
          }
          let zip = new PizZip(content);
          let doc = new docxtemplater().loadZip(zip);
          doc.setOptions({nullGetter: function() { return ""; }});
          // 设置模板变量的值
          doc.setData(fullData);
          try {
            doc.render();
          } catch (error) {
            let e = {
              message: error.message,
              name: error.name,
              stack: error.stack,
              properties: error.properties
            };
            console.log(JSON.stringify({ error: e }));
            throw error;
          }
          // 生成一个代表docxtemplater对象的zip文件(不是一个真实的文件,而是在内存中的表示)
          let out = doc.getZip().generate({
            type: "blob",
            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
          });
          // 将目标文件对象保存为目标类型的文件,并命名
          saveAs(out, docxname+".docx");
        });
      }
  }
}

index.vue

<template>
  <dev>
    <button
        @click="downloadTemplate"
    >下载</button>
  </dev>
</template>
<script>
import exportWord from '@/components/comm/exportWord'
export default {
  name: "demo",
  props: {},
  components: {},
  mixins: [exportWord],
  data() {
    return {
    }
  },
  computed: {},
  methods: {
    async downloadTemplate(){
      let data = {};
      data.remark="备注";
      data.template=[{
        no:"1",
        name:"苹果",
        num:'10',
        time:'2022-12-10'
      },{
        no:"2",
        name:"梨子",
        num:'20',
        time:'2022-12-10'
      }];
      //模板文件的位置
      let docxsrc="/word/template.docx";
      //导出文件的名字
      let docxname = "模板";
      this.exportWord(docxsrc, docxname, data)
    }
  }
}
</script>
<style>
</style>

App.vue

 

<template>
  <views />
</template>

<script>
import views from './views/index.vue'

export default {
  name: 'App',
  components: {
    views
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值