vue导出word纯前端的实现方式

 更新时间:2022年04月19日 09:57:38   作者:Yàο耀耀  
这篇文章主要介绍了vue导出word纯前端的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

vue导出word纯前端实现

最近项目有个需求导出word,纯前端实现,查了查资料,用docxtemplater简直不要太简单。

直接把官网例子拿过来就可以了。!!!

官网地址

首先,新建一个docx文件,把模板先写好。

注意!!如果数据结构中存在数组。 用{#xxx}{/xxx} 包裹。

数据结构示例:

 wordData: {
          name: '导出word',
          nameList: [{
              name: "张三",
              age: 16,
              hobby: ['吃饭', '睡觉', '打豆豆']
            },
            {
              name: "李四",
              age: 19,
              hobby: ['抽烟', '喝酒', '打麻将']
            },
          ]
        },

模板写好之后放入项目中。

然后导入需要的包,

npm i docxtemplater pizzip  file-saver  --save

然后在需要的模块内引入

  import 'docxtemplater/build/docxtemplater.js'
  import 'pizzip/dist/pizzip.js'
  import 'pizzip/dist/pizzip-utils.js'
  import 'file-saver'
  methods: {
      // 导出word
      loadFile(url, callback) {
        PizZipUtils.getBinaryContent(url, callback);
      },
      generate() {
        var that = this;
        this.loadFile("../../static/word.docx", function (error, content) {
          if (error) {
            throw error
          };
          var zip = new PizZip(content);
          var doc = new window.docxtemplater().loadZip(zip)
          doc.setData({
            ...that.wordData
          });
          try {
            // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
            doc.render()
          } catch (error) {
            var e = {
              message: error.message,
              name: error.name,
              stack: error.stack,
              properties: error.properties,
            }
            console.log(JSON.stringify({
              error: e
            }));
            // The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
            throw error;
          }
          var out = doc.getZip().generate({
            type: "blob",
            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
          }) //Output the document using Data-URI
          saveAs(out, "output.docx")
        })
      },
    }

到此就完事了。

完整代码,安装完包之后直接运行就好。记得放入word模板!!!

<template>
  <div>
    <button @click="generate">导出</button>
  </div>
</template>
<script>
  import 'docxtemplater/build/docxtemplater.js'
  import 'pizzip/dist/pizzip.js'
  import 'pizzip/dist/pizzip-utils.js'
  import 'file-saver'
  export default {
    data() {
      return {
        wordData: {
          name: '导出word',
          nameList: [{
              name: "张三",
              age: 16,
              hobby: ['吃饭', '睡觉', '打豆豆']
            },
            {
              name: "李四",
              age: 19,
              hobby: ['抽烟', '喝酒', '打麻将']
            },
          ]
        },
      }
    },
    methods: {
      // 导出word
      loadFile(url, callback) {
        PizZipUtils.getBinaryContent(url, callback);
      },
      generate() {
        var that = this;
        this.loadFile("../../static/word.docx", function (error, content) {
          if (error) {
            throw error
          };
          var zip = new PizZip(content);
          var doc = new window.docxtemplater().loadZip(zip)
          doc.setData({
            ...that.wordData
          });
          try {
            // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
            doc.render()
          } catch (error) {
            var e = {
              message: error.message,
              name: error.name,
              stack: error.stack,
              properties: error.properties,
            }
            console.log(JSON.stringify({
              error: e
            }));
            // The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
            throw error;
          }
          var out = doc.getZip().generate({
            type: "blob",
            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
          }) //Output the document using Data-URI
          saveAs(out, "output.docx")
        })
      },
    }
  }
</script>
<style scoped>
</style>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

相关文章

  • vue缓存的keepalive页面刷新数据的方法

    vue缓存的keepalive页面刷新数据的方法

    这篇文章主要介绍了vue缓存的keepalive页面刷新数据的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-04-04
  • vue货币过滤器的实现方法

    vue货币过滤器的实现方法

    这篇文章主要为大家详细介绍了vue货币过滤器的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • Console高级用法总结

    Console高级用法总结

    Console 对象提供了浏览器控制台调试的接口。在不同宿主环境上它的工作方式可能不一样,但通常都会提供一套共性的功能,本文主要总结了Console的一些高级用法,感兴趣的小伙伴可以参考一下
    2023-04-04
  • vue cli实现项目登陆页面流程详解

    vue cli实现项目登陆页面流程详解

    CLI是一个全局安装的npm包,提供了终端里的vue命令。它可以通过vue create快速搭建一个新项目,或者直接通过vue serve构建新想法的原型。你也可以通过vue ui通过一套图形化界面管理你的所有项目
    2022-10-10
  • Vue3如何处理重复渲染代码的问题

    Vue3如何处理重复渲染代码的问题

    这篇文章主要来和大家一起探讨一下在Vue3项目中如何处理重复渲染代码的问题,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • 基于Vue3实现列表虚拟滚动效果

    基于Vue3实现列表虚拟滚动效果

    这篇文章主要为大家介绍了如何利用Vue3实现列表虚拟滚动效果,文中的示例代码讲解详细,对我们学习或工作有一定价值,需要的可以参考一下
    2022-04-04
  • vue系列之requireJs中引入vue-router的方法

    vue系列之requireJs中引入vue-router的方法

    这篇文章主要介绍了vue系列之requireJs中引入vue-router的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Vue 实现登录界面验证码功能

    Vue 实现登录界面验证码功能

    本文通过实例代码给大家介绍了Vue 实现登录界面 验证码功能,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01
  • 解决vue3.0运行项目warning Insert `·` prettier/prettier问题

    解决vue3.0运行项目warning Insert `·` prettier/pret

    这篇文章主要介绍了解决vue3.0运行项目warning Insert `·` prettier/prettier问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • vue2实现provide inject传递响应式

    vue2实现provide inject传递响应式

    在看element-ui的源码的时候,注意到源码里面有很多地方使用provide和inject的属性,本文主要介绍了vue2实现provide inject传递响应式,分享给大家,感兴趣的可以了解一下
    2021-05-05

最新评论