vue使用docxtemplater导出word

安装

    // 安装 docxtemplater
    npm install docxtemplater pizzip  --save
    // 安装 jszip-utils
    npm install jszip-utils --save 
    // 安装 jszip
    npm install jszip --save
    // 安装 FileSaver
    npm install file-saver --save
    // 引入处理图片的插件1
    npm install docxtemplater-image-module-free --save
    // 引入处理图片的插件2
    npm install angular-expressions --save    

  

准备word模板放至项目public文件夹下

常用语法

  • 单个变量使用

  {name}

  • 数组对象循环

  {#list}  {name} {age} {/list}

  • 图片(base64/url)

  {%imgurl}

 封装导出方法

    import docxtemplater from 'docxtemplater';
    import PizZip from 'pizzip';
    import JSZipUtils from 'jszip-utils';
    import { saveAs } from 'file-saver';
    import ImageModule from 'docxtemplater-image-module-free';


    //将对象中值为null和undefined的进行替换
    //参数1 需要处理的对象 参数2 替换后的值
    const replaceNull = (someObj, replaceValue = "***") => {
        const replacer = (key, value) =>
        String(value) === "null" || String(value) === "undefined" ?         replaceValue : value;
        return JSON.parse(JSON.stringify(someObj, replacer));
    }

    /**
     4. 导出docx
     5. @param { String } tempDocxPath 模板文件路径
     6. @param { Object } data 文件中传入的数据
     7. @param { String } fileName 导出文件名称
    */
    export const exportWord = (tempDocxPath, data, fileName) => {
        //过滤空值
           data = replaceNull(data, '')
        function base64DataURLToArrayBuffer(dataURL) {
            const base64Regex =     /^data:image\/(png|jpg|jpeg|svg|svg\+xml);base64,/;
            if (!base64Regex.test(dataURL)) {
                return false;
            }
        console.log(dataURL);
        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;
    }
    // 读取并获得模板文件的二进制内容
    JSZipUtils.getBinaryContent(tempDocxPath, (error, content) => {
        if (error) {
            throw error;
        }
        const imageOptions = {
            getImage(tag) {
                return base64DataURLToArrayBuffer(tag);
            },
            getSize() {
                return [100, 100];
            },
        };
        let zip = new PizZip(content);
        let doc = new docxtemplater();
        doc.loadZip(zip);
        doc.attachModule(new ImageModule(imageOptions));
        doc.setData(data);
        
          try {
              doc.render();
          } catch (error) {
              let e = {
                  message: error.message,
                  name: error.name,
                  stack: error.stack,
                  properties: error.properties,
              };
              console.log({
                  error: e
              });
              throw error;
          }
          let out = doc.getZip().generate({
              type: "blob",
              mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
          }); //Output the document using Data-URI
          saveAs(out, fileName);
      });
  };        

封装方法使用

    import { exportWord } from '/@/utils/exportFile';

    const handleExport = async (data: any) => {
            exportWord('/wordTemplate/tzd.docx', data, '通知单.docx');
    }

 

posted @ 2024-04-09 16:08    阅读(271)  评论(0编辑  收藏  举报