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

vue實(shí)現(xiàn)導(dǎo)出word文檔的示例代碼

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

vue 導(dǎo)出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文件(導(dǎo)出word方法)

文件存放目錄自定義

import PizZip from 'pizzip'
import docxtemplater from 'docxtemplater'
import JSZipUtils from 'jszip-utils'
import { saveAs } from 'file-saver'
 
/**
 * 將base64格式的數(shù)據(jù)轉(zhuǎn)為ArrayBuffer
 * @param {Object} dataURL base64格式的數(shù)據(jù)
 */
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;
}
 
/**
 * 導(dǎo)出word,支持圖片
 * @param {Object} tempDocxPath 模板文件路徑
 * @param {Object} wordData 導(dǎo)出數(shù)據(jù)
 * @param {Object} fileName 導(dǎo)出文件名
 * @param {Object} imgSize 自定義圖片尺寸
 */
export const exportWord = (tempDocxPath, wordData, fileName, imgSize) => {
  // 這里要引入處理圖片的插件
  var ImageModule = require('docxtemplater-image-module-free');
 
  const expressions = require("angular-expressions");
 
  // 讀取并獲得模板文件的二進(jìn)制內(nèi)容
  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數(shù)據(jù)
      // 將base64的數(shù)據(jù)轉(zhuǎn)為ArrayBuffer
      return base64DataURLToArrayBuffer(chartId);
    }
 
    opts.getSize = function (img, tagValue, tagName) {
      // console.log(img);//ArrayBuffer數(shù)據(jù)
      // console.log(tagValue);//base64數(shù)據(jù)
      // console.log(tagName);//wordData對(duì)象的圖像屬性名
      // 自定義指定圖像大小
      if (imgSize.hasOwnProperty(tagName)){
        return imgSize[tagName];
      } else {
        return [600, 350];
      }
    }
 
    // 創(chuàng)建一個(gè)PizZip實(shí)例,內(nèi)容為模板的內(nèi)容
    let zip = new PizZip(content);
    // 創(chuàng)建并加載docxtemplater實(shí)例對(duì)象
    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;
    }
 
    // 生成一個(gè)代表docxtemplater對(duì)象的zip文件(不是一個(gè)真實(shí)的文件,而是在內(nèi)存中的表示)
    let out = doc.getZip().generate({
      type: "blob",
      mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    });
    // 將目標(biāo)文件對(duì)象保存為目標(biāo)類型的文件,并命名
    saveAs(out, fileName);
  });
}

3.組件調(diào)用

注意:引用文件的路徑是你創(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">導(dǎo)入圖片</a-button>
      </a-upload>
    </a-form-model-item>
    <a-form-model-item label="操作">
      <a-button type="primary" icon="export" @click="exportWordFile">導(dǎo)出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: '請(qǐng)輸入名稱!', trigger: 'blur' },
        ],
        date:[
          { required: true, message: '請(qǐng)輸入日期!', trigger: 'blur' },
        ],
      },
    };
  },
  created (){},
  methods: {
    customRequest (data){
      //圖片必須轉(zhuǎn)成base64格式
      var reader = new FileReader();
      reader.readAsDataURL(data.file);
      reader.onload = () => {
        // console.log("file 轉(zhuǎn) base64結(jié)果:" + reader.result);
        this.form.imgUrl = reader.result; //imgUrl必須與模板文件里的參數(shù)名一致
      };
      reader.onerror = function (error) {
        console.log("Error: ", error);
      };
    },
    exportWordFile (){
      let imgSize = {
        imgUrl:[65, 65], //控制導(dǎo)出的word圖片大小
      };
      exportWord("./static/test.docx", this.form, "demo.docx", imgSize);
      //參數(shù)1:模板文檔 
      //參數(shù)2:字段參數(shù)
      //參數(shù)3:輸出文檔
      //參數(shù)4:圖片大小
    }
  },
};
</script>

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

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

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

文檔內(nèi)容:

5.導(dǎo)出demo.docx

結(jié)果展示:

以上就是vue實(shí)現(xiàn)導(dǎo)出word文檔的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于vue導(dǎo)出word的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

汪清县| 扶沟县| 江安县| 大洼县| 沁水县| 泽库县| 鹿泉市| 元阳县| 昌黎县| 祥云县| 临潭县| 大兴区| 勐海县| 彭泽县| 虎林市| 错那县| 桑日县| 毕节市| 通海县| 剑川县| 辽阳市| 洪雅县| 呼伦贝尔市| 阿瓦提县| 平谷区| 醴陵市| 宁化县| 靖远县| 山阳县| 靖江市| 米脂县| 凌云县| 青浦区| 海丰县| 拜城县| 鹤壁市| 永清县| 五常市| 平湖市| 宜城市| 建宁县|