前端生成word文档

核心工具:docxtemplater:一个可以通过js生成word,excel和ppt的工具,工具分三个版本,其中使用了免费版本

其他依赖:PizZip, JSZipUtils

 

安装:

   1 yarn add docxtemplater pizzip jszip-utils -S 

 

  如果需要支持image,需要安装image模块

   1 yarn add docxtemplater-image-module-free -S 

  如果需要将文件保存在本地可以使用file-saver

   1 yarn add file-saver -S 

 

使用:

  如果需要使用Image和save(最后一行),将注释打开就行

 1 import Docxtemplater from "docxtemplater";
 2 import PizZip from "pizzip";
 3 import JSZipUtils from "jszip-utils";
 4 // import ImageModule from "docxtemplater-image-module-free";
 5 // import { saveAs } from "file-saver";
 6 
 7 export default function exportWordAndImage(report) {
 8   const templateChose = "./template.docx"; // 模板推荐放在public文件夹下
 9   JSZipUtils.getBinaryContent(templateChose, (error, content) => {
10     if (error) {
11       console.error(error);
12       return;
13     }
14     // const opts = {};
15     // opts.centered = false;
16     // opts.getImage = (tagValue) => new Promise((resolve, reject) => {
17     //   JSZipUtils.getBinaryContent(tagValue, (geterror, getcontent) => {
18     //     if (geterror) {
19     //       return reject(geterror);
20     //     }
21     //     return resolve(getcontent);
22     //   });
23     // });
24     // 图片有关代码,没有图片的可以删除
25     // opts.getSize = (img, tagValue, tagName) => {
26     // return [200, 200] // 图片大小 (固定的)如果你的图片大小要固定,请使用这行代码,将下面return new Promise的那部分注销掉
27     // 非固定(这里是设置宽度最大为300px的图片)
28     //   const wid = 300;
29     //   return new Promise((resolve, reject) => {
30     //     const image = new Image();
31     //     image.src = tagValue;
32     //     let imgWidth;
33     //     let imgHeight;
34     //     let scale;
35     //     image.onload = () => {
36     //       imgWidth = image.width;
37     //       imgHeight = image.height;
38     //       scale = 0;
39     //       if (imgWidth > wid) {
40     //         scale = wid / imgWidth;
41     //         imgWidth = wid;
42     //         imgHeight *= scale;
43     //       }
44     //       resolve([imgWidth, imgHeight]);
45     //     };
46     //     image.onerror = (e) => {
47     //       console.log("img, tagName, tagValue : ", img, tagName, tagValue);
48     //       reject(e);
49     //     };
50     //   });
51     // };
52     // const imageModule = new ImageModule(opts);
53 
54     const zip = new PizZip(content);
55     const doc = new Docxtemplater()
56       .loadZip(zip)
57       .setOptions({ linebreaks: true }) // 换行确认,如果你有的文本中有换行符的话,可以选择它导入到word起不起作用
58       // .attachModule(imageModule)
59       .compile();
60     doc
61       .resolveData({
62         // 这是你导入的数据,这个数据体中的属性或对象一定要和word模板中的插值一样
63         ...report,
64       })
65       .then(() => {
66         console.log("Export...");
67         doc.render();
68         const out = doc.getZip().generate({
69           type: "blob",
70           mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
71         });
72 
73         return out; // 直接返回生成的blob对象
74         // 输出文档,可以自定义自己的文档名
75         // saveAs(out, "Temporary, you need to sync as.docx");
76       });
77   });
78 }

 

使用的时候直接调用

exportWordAndImage() // 参数是需要替换的数据

模版语法(免费版):

  变量替换:{变量}

    如: {name}   // 此处会使用name值进行替换

  条件:{#users.length > 3}

      // 当users的length大于3时渲染这里

     {/}

  循环渲染:{#products}

      {name}  //这里会渲染products[index].name 字段

     {/products}

  其他:参考官网demo

 

参考:https://blog.csdn.net/Grantr/article/details/124482299

 

posted on 2022-08-11 10:45  YJJMH  阅读(1391)  评论(0)    收藏  举报

导航