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

vue實現(xiàn)導出word文檔的示例代碼

 更新時間:2024年01月23日 08:18:24   作者:Grant丶  
這篇文章主要為大家詳細介紹了如何使用vue實現(xiàn)導出word文檔(包括圖片),文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下

vue 導出word文檔(包括圖片)

1.打開終端,安裝依賴

-- 安裝 docxtemplater
npm install docxtemplater pizzip  --save

-- 安裝 jszip-utils
npm install jszip-utils --save 

-- 安裝 jszip
npm install jszip --save

-- 安裝 FileSaver
npm install file-saver --save

-- 安裝 angular-expressions
npm install angular-expressions --save

-- 安裝 image-size
npm install image-size --save

2.創(chuàng)建exportFile.js文件(導出word方法)

文件存放目錄自定義

import PizZip from 'pizzip'
import docxtemplater from 'docxtemplater'
import JSZipUtils from 'jszip-utils'
import { saveAs } from 'file-saver'
 
/**
 * 將base64格式的數據轉為ArrayBuffer
 * @param {Object} dataURL base64格式的數據
 */
function base64DataURLToArrayBuffer (dataURL) {
  const base64Regex = /^data:image\/(png|jpg|jpeg|svg|svg\+xml);base64,/;
  if (!base64Regex.test(dataURL)) {
    return false;
  }
  const stringBase64 = dataURL.replace(base64Regex, "");
  let binaryString;
  if (typeof window !== "undefined") {
    binaryString = window.atob(stringBase64);
  } else {
    binaryString = new Buffer(stringBase64, "base64").toString("binary");
  }
  const len = binaryString.length;
  const bytes = new Uint8Array(len);
  for (let i = 0; i < len; i++) {
    const ascii = binaryString.charCodeAt(i);
    bytes[i] = ascii;
  }
  return bytes.buffer;
}
 
/**
 * 導出word,支持圖片
 * @param {Object} tempDocxPath 模板文件路徑
 * @param {Object} wordData 導出數據
 * @param {Object} fileName 導出文件名
 * @param {Object} imgSize 自定義圖片尺寸
 */
export const exportWord = (tempDocxPath, wordData, fileName, imgSize) => {
  // 這里要引入處理圖片的插件
  var ImageModule = require('docxtemplater-image-module-free');
 
  const expressions = require("angular-expressions");
 
  // 讀取并獲得模板文件的二進制內容
  JSZipUtils.getBinaryContent(tempDocxPath, function (error, content) {
 
    if (error) {
      throw error;
    }
 
    expressions.filters.size = function (input, width, height) {
      return {
        data: input,
        size: [width, height],
      };
    };
 
    // function angularParser (tag) {
    //   const expr = expressions.compile(tag.replace(/'/g, "'"));
    //   return {
    //     get (scope) {
    //       return expr(scope);
    //     },
    //   };
    // }
 
    // 圖片處理
    let opts = {}
 
    opts = {
      // 圖像是否居中
      centered: false
    };
 
    opts.getImage = (chartId) => {
      // console.log(chartId);//base64數據
      // 將base64的數據轉為ArrayBuffer
      return base64DataURLToArrayBuffer(chartId);
    }
 
    opts.getSize = function (img, tagValue, tagName) {
      // console.log(img);//ArrayBuffer數據
      // console.log(tagValue);//base64數據
      // console.log(tagName);//wordData對象的圖像屬性名
      // 自定義指定圖像大小
      if (imgSize.hasOwnProperty(tagName)){
        return imgSize[tagName];
      } else {
        return [600, 350];
      }
    }
 
    // 創(chuàng)建一個PizZip實例,內容為模板的內容
    let zip = new PizZip(content);
    // 創(chuàng)建并加載docxtemplater實例對象
    let doc = new docxtemplater();
    doc.attachModule(new ImageModule(opts));
    doc.loadZip(zip);
 
    doc.setData(wordData);
 
    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, fileName);
  });
}

3.組件調用

注意:引用文件的路徑是你創(chuàng)建文件的路徑

<template>
  <a-form-model
    ref="ruleForm"
    :model="form"
    :rules="rules"
    :label-col="labelCol"
    :wrapper-col="wrapperCol"
  >
    <a-form-model-item label="名稱" prop="name">
      <a-input-number v-model="form.name" style="width:100%;"/>
    </a-form-model-item>
    <a-form-model-item label="日期" prop="date">
      <a-input v-model="form.date" />
    </a-form-model-item>
    <a-form-model-item label="文件">
      <a-input v-model="form.imgUrl" read-only/>
      <a-upload name="file" :showUploadList="false" :customRequest="customRequest">
        <a-button type="primary" icon="upload">導入圖片</a-button>
      </a-upload>
    </a-form-model-item>
    <a-form-model-item label="操作">
      <a-button type="primary" icon="export" @click="exportWordFile">導出word文檔</a-button>
    </a-form-model-item>
  </a-form-model>
</template>
<script>
import {exportWord} from '@/assets/js/exportFile.js'
export default {
  name: 'ExportFile',
  data () {
    return {
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      form: {},
      rules: {
        name: [
          { required: true, message: '請輸入名稱!', trigger: 'blur' },
        ],
        date:[
          { required: true, message: '請輸入日期!', trigger: 'blur' },
        ],
      },
    };
  },
  created (){},
  methods: {
    customRequest (data){
      //圖片必須轉成base64格式
      var reader = new FileReader();
      reader.readAsDataURL(data.file);
      reader.onload = () => {
        // console.log("file 轉 base64結果:" + reader.result);
        this.form.imgUrl = reader.result; //imgUrl必須與模板文件里的參數名一致
      };
      reader.onerror = function (error) {
        console.log("Error: ", error);
      };
    },
    exportWordFile (){
      let imgSize = {
        imgUrl:[65, 65], //控制導出的word圖片大小
      };
      exportWord("./static/test.docx", this.form, "demo.docx", imgSize);
      //參數1:模板文檔 
      //參數2:字段參數
      //參數3:輸出文檔
      //參數4:圖片大小
    }
  },
};
</script>

4.創(chuàng)建test.docx文檔模板

注:使用vue-cli2的時候,放在static目錄下;使用vue-cli3的時候,放在public目錄下。

模板參數名需與字段一致,普通字段:{字段名},圖片字段:{%字段名}

文檔內容:

5.導出demo.docx

結果展示:

以上就是vue實現(xiàn)導出word文檔的示例代碼的詳細內容,更多關于vue導出word的資料請關注腳本之家其它相關文章!

相關文章

  • Vue輸入框狀態(tài)切換&自動獲取輸入框焦點的實現(xiàn)方法

    Vue輸入框狀態(tài)切換&自動獲取輸入框焦點的實現(xiàn)方法

    這篇文章主要介紹了Vue輸入框狀態(tài)切換&自動獲取輸入框焦點的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • 解決vue使用vant輪播組件swipe + flex時文字抖動問題

    解決vue使用vant輪播組件swipe + flex時文字抖動問題

    這篇文章主要介紹了解決vue使用vant輪播組件swipe + flex時文字抖動問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-01-01
  • 如何處理vue router 路由傳參刷新頁面參數丟失

    如何處理vue router 路由傳參刷新頁面參數丟失

    這篇文章主要介紹了如何處理vue router 路由傳參刷新頁面參數丟失,對vue感興趣的同學,可以參考下
    2021-05-05
  • Vue組件之高德地圖地址選擇功能的實例代碼

    Vue組件之高德地圖地址選擇功能的實例代碼

    這篇文章主要介紹了Vue組件之 高德地圖地址選擇功能的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • 如何使用Vue3設計實現(xiàn)一個Model組件淺析

    如何使用Vue3設計實現(xiàn)一個Model組件淺析

    v-model在Vue里面是一個語法糖,數據的雙向綁定,本質上還是通過 自定義標簽的attribute傳遞和接受,下面這篇文章主要給大家介紹了關于如何使用Vue3設計實現(xiàn)一個Model組件的相關資料,需要的朋友可以參考下
    2022-08-08
  • 在Vue3項目中使用MQTT獲取數據的方法示例

    在Vue3項目中使用MQTT獲取數據的方法示例

    這篇文章主要介紹了在Vue3項目中使用MQTT.js庫實現(xiàn)數據獲取的步驟,包括安裝庫、創(chuàng)建MQTT連接、發(fā)送和接收消息、配置安全選項等,并提供了一個完整的示例代碼和常見問題解決方法,需要的朋友可以參考下
    2025-11-11
  • 關于element el-input的autofocus失效的問題及解決

    關于element el-input的autofocus失效的問題及解決

    這篇文章主要介紹了關于element el-input的autofocus失效的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • vue-calendar-component日歷組件報錯Clock is not defined解決

    vue-calendar-component日歷組件報錯Clock is not defi

    這篇文章主要為大家介紹了vue-calendar-component日歷組件報錯Clock is not defined解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • Vue實現(xiàn)自定義組件改變組件背景色(示例代碼)

    Vue實現(xiàn)自定義組件改變組件背景色(示例代碼)

    要實現(xiàn) Vue 自定義組件改變組件背景色,你可以通過 props 將背景色作為組件的一個屬性傳遞給組件,在組件內部監(jiān)聽這個屬性的變化,并將其應用到組件的樣式中,下面通過示例代碼介紹Vue如何實現(xiàn)自定義組件改變組件背景色,感興趣的朋友一起看看吧
    2024-03-03
  • 詳解vue-cli3多頁應用改造

    詳解vue-cli3多頁應用改造

    這篇文章主要介紹了詳解vue-cli3多頁應用改造,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06

最新評論

大英县| 铁岭市| 江陵县| 班玛县| 恩施市| 济宁市| 九龙城区| 汉寿县| 常熟市| 铜山县| 格尔木市| 绩溪县| 崇阳县| 贡嘎县| 什邡市| 吉林省| 东乌| 盐城市| 崇左市| 喜德县| 丰都县| 荣昌县| 聂拉木县| 吉木乃县| 武平县| 保康县| 封开县| 富民县| 武汉市| 利辛县| 东城区| 双柏县| 内黄县| 舒兰市| 镶黄旗| 全南县| 江口县| 金昌市| 崇明县| 留坝县| 峡江县|