vue使用docxtemplater导出word

 更新时间:2025年04月04日 08:54:10   作者:脆  
docxtemplater 是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtemplater导出word吧

docxtemplater

docxtemplater 是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容(表格、html、图像)。

npm 是安装 docxtemplater 最简单的方法

npm install docxtemplater pizzip --save​
// 安装 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

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文件夹下

常用语法

1.单个变量使用

{name}

2.数组对象循环

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

3.图片(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');
    }

到此这篇关于vue使用docxtemplater导出word的文章就介绍到这了,更多相关vue docxtemplater导出word内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue中的 DOM与Diff详情

    Vue中的 DOM与Diff详情

    这篇文章主要介绍了Vue中的 DOM与Diff详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下,希望对你的学习有所帮助
    2022-08-08
  • vuejs 动态添加input框的实例讲解

    vuejs 动态添加input框的实例讲解

    今天小编就为大家分享一篇vuejs 动态添加input框的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • vue之字符串、数组之间的相互转换方式

    vue之字符串、数组之间的相互转换方式

    这篇文章主要介绍了vue之字符串、数组之间的相互转换方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • vue 单页应用和多页应用的优劣

    vue 单页应用和多页应用的优劣

    这篇文章主要介绍了vue 单页应用和多页应用的优劣,帮助大家更好的理解和使用vue,感兴趣的朋友可以了解下
    2020-10-10
  • Vue中的匿名插槽与具名插槽详解

    Vue中的匿名插槽与具名插槽详解

    这篇文章主要为大家介绍了Vue中的匿名插槽与具名插槽,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-12-12
  • Vue实现文件上传和下载功能

    Vue实现文件上传和下载功能

    这篇文章主要为大家详细介绍了Vue实现文件上传和下载功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • uniapp宽屏开发PC端方案及衍生问题解决办法

    uniapp宽屏开发PC端方案及衍生问题解决办法

    在uniapp中进行宽屏开发,主要是指在电脑端(PC端)使用宽屏显示效果进行应用的开发,这篇文章主要给大家介绍了关于uniapp宽屏开发PC端方案及衍生问题解决办法,需要的朋友可以参考下
    2024-03-03
  • Vue3项目实现前端导出Excel的示例代码

    Vue3项目实现前端导出Excel的示例代码

    这篇文章主要介绍了Vue3项目实现前端导出Excel的示例,在vue3的项目中导出Excel表格的功能,可以借助xlsx库来实现,下面是详细的操作步骤,需要的朋友可以参考下
    2025-01-01
  • vue3 typescript封装axios过程示例

    vue3 typescript封装axios过程示例

    这篇文章主要为大家介绍了vue3 typescript封装axios过程示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • element表格行列的动态合并示例详解

    element表格行列的动态合并示例详解

    这篇文章主要为大家介绍了element表格行列的动态合并示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07

最新评论