前端 Word 模板参入特定数据 并且下载
要将特定数据插入到现有的 Word 模板中并进行下载,可以使用 Pizzip 和 docxtemplater 库。这两个库结合起来可以让你方便地将数据插入到 .docx 模板中。以下是详细步骤:
- 安装所需库
npm install pizzip docxtemplater file-saver
- 准备 Word 模板
创建一个 .docx 文件作为模板,例如 template.docx。在模板中,可以使用类似 {name}、{age} 这样的占位符表示需要替换的数据。 - 编写代码将数据插入模板并下载
import PizZip from "pizzip";
import Docxtemplater from "docxtemplater";
import { saveAs } from "file-saver";// 加载文件的方法
const loadFile = async (url) => {const response = await fetch(url);const arrayBuffer = await response.arrayBuffer();return arrayBuffer;
};// 将数据插入到模板并生成文件
const generateDocument = async () => {try {// 1. 加载模板文件const content = await loadFile("/path/to/template.docx"); // 替换成模板文件的路径// 2. 创建PizZip实例const zip = new PizZip(content);// 3. 创建docxtemplater实例const doc = new Docxtemplater(zip, {paragraphLoop: true,linebreaks: true,});// 4. 设置要插入的数据doc.setData({name: "张三",age: 28,job: "前端开发工程师",});// 5. 替换模板中的占位符doc.render();// 6. 生成文档并下载const out = doc.getZip().generate({type: "blob",mimeType:"application/vnd.openxmlformats-officedocument.wordprocessingml.document",});saveAs(out, "filled_template.docx");} catch (error) {console.error("生成文档出错: ", error);}
};// 调用生成文档的函数
generateDocument();
- 使用注意事项
将 template.docx 文件放置在可通过 URL 访问的位置(例如项目的 public 文件夹)或者自己指定的路径。
在模板文件中使用 {} 包含占位符,例如:{name}、{age} 等。
这样,运行时程序会将数据插入到模板中的对应位置,并生成并下载填充后的 Word 文件。
参考来源