最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue導出word純前端的實現(xiàn)方式

 更新時間:2022年04月19日 09:57:38   作者:Yàο耀耀  
這篇文章主要介紹了vue導出word純前端的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

vue導出word純前端實現(xiàn)

最近項目有個需求導出word,純前端實現(xiàn),查了查資料,用docxtemplater簡直不要太簡單。

直接把官網(wǎng)例子拿過來就可以了。?。?!

官網(wǎng)地址

首先,新建一個docx文件,把模板先寫好。

注意??!如果數(shù)據(jù)結構中存在數(shù)組。 用{#xxx}{/xxx} 包裹。

數(shù)據(jù)結構示例:

 wordData: {
          name: '導出word',
          nameList: [{
              name: "張三",
              age: 16,
              hobby: ['吃飯', '睡覺', '打豆豆']
            },
            {
              name: "李四",
              age: 19,
              hobby: ['抽煙', '喝酒', '打麻將']
            },
          ]
        },

模板寫好之后放入項目中。

然后導入需要的包,

npm i docxtemplater pizzip  file-saver  --save

然后在需要的模塊內(nèi)引入

  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模板?。。?/p>

<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>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關文章

  • vue緩存的keepalive頁面刷新數(shù)據(jù)的方法

    vue緩存的keepalive頁面刷新數(shù)據(jù)的方法

    這篇文章主要介紹了vue緩存的keepalive頁面刷新數(shù)據(jù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • vue貨幣過濾器的實現(xiàn)方法

    vue貨幣過濾器的實現(xiàn)方法

    這篇文章主要為大家詳細介紹了vue貨幣過濾器的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Console高級用法總結

    Console高級用法總結

    Console 對象提供了瀏覽器控制臺調(diào)試的接口。在不同宿主環(huán)境上它的工作方式可能不一樣,但通常都會提供一套共性的功能,本文主要總結了Console的一些高級用法,感興趣的小伙伴可以參考一下
    2023-04-04
  • vue cli實現(xiàn)項目登陸頁面流程詳解

    vue cli實現(xiàn)項目登陸頁面流程詳解

    CLI是一個全局安裝的npm包,提供了終端里的vue命令。它可以通過vue create快速搭建一個新項目,或者直接通過vue serve構建新想法的原型。你也可以通過vue ui通過一套圖形化界面管理你的所有項目
    2022-10-10
  • Vue3如何處理重復渲染代碼的問題

    Vue3如何處理重復渲染代碼的問題

    這篇文章主要來和大家一起探討一下在Vue3項目中如何處理重復渲染代碼的問題,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-01-01
  • 基于Vue3實現(xiàn)列表虛擬滾動效果

    基于Vue3實現(xiàn)列表虛擬滾動效果

    這篇文章主要為大家介紹了如何利用Vue3實現(xiàn)列表虛擬滾動效果,文中的示例代碼講解詳細,對我們學習或工作有一定價值,需要的可以參考一下
    2022-04-04
  • vue系列之requireJs中引入vue-router的方法

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

    這篇文章主要介紹了vue系列之requireJs中引入vue-router的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • Vue 實現(xiàn)登錄界面驗證碼功能

    Vue 實現(xiàn)登錄界面驗證碼功能

    本文通過實例代碼給大家介紹了Vue 實現(xiàn)登錄界面 驗證碼功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    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實現(xiàn)provide inject傳遞響應式

    vue2實現(xiàn)provide inject傳遞響應式

    在看element-ui的源碼的時候,注意到源碼里面有很多地方使用provide和inject的屬性,本文主要介紹了vue2實現(xiàn)provide inject傳遞響應式,分享給大家,感興趣的可以了解一下
    2021-05-05

最新評論

汽车| 安陆市| 宜宾市| 金坛市| 进贤县| 石台县| 崇明县| 莱阳市| 青浦区| 阿坝| 海安县| 娄烦县| 马公市| 崇礼县| 沙田区| 台湾省| 平乐县| 茂名市| 通山县| 内黄县| 贺兰县| 日土县| 张家港市| 丰镇市| 华宁县| 通化县| 余庆县| 安国市| 孟津县| 延吉市| 镇康县| 大同县| 祁门县| 璧山县| 广南县| 霸州市| 马鞍山市| 资中县| 日喀则市| 莎车县| 邵东县|