我想使用 Angular 操作 .docx 文件。 我编写了一个程序并实现了 docxtemplater,但在 .docx 中它写的是 undefined。
这是file.docx的最终输出 我的名字未定义 我的地址未定义 我的电子邮件地址未定义
预计出炉 我的名字是开发者 我的地址是印度 我的电子邮件地址是 [电子邮件受保护]
模板:- docx
My name is {data.name}
my address is {data.address}
My email address is {data.email}
我的代码:
为了使用点语法,{data.name},...,您需要启用角度解析器:
你可以这样做:
import Docxtemplater from "docxtemplater";
import PizZip from "pizzip";
import expressionParser from "docxtemplater/expressions.js";
expressionParser.filters.upper = function (input) {
// Make sure that if your input is undefined, your
// output will be undefined as well and will not
// throw an error
if (!input) return input;
return input.toUpperCase();
};
const zip = new PizZip(content);
const doc = new Docxtemplater(zip, {
paragraphLoop: true,
linebreaks: true,
parser: expressionParser
});
doc.render({
first_name: "John",
last_name: "Doe",
phone: "0652455478",
description: "New Website",
});
请参阅此处有关角度解析器的文档: