docx-templates docx-templater 导出word报错Can’t find end of central directory : is this a zip file
最近接到一个需求需要做按word格式导出word 文档,我使用的node.js,网上搜了相关的库很少,推荐的docx-templates docx-templater 这两个库居多。
但是在使用过程中发现一直报Can’t find end of central directory : is this a zip file 这个错误。
前前后后参考几篇博客,我的一直报这个错误。
直接搜索这个错误,相关回答很少,有网友说是路径问题。于是我一个下午都在研究路径。发现还是不行
接下来敲黑板。。。。。
不是路径问题是.doc文件无法识别。。
经过多次文件测试确定就是doc文件问题,因为我一开始用的需要用的模板测试报错报错报错
悄悄告诉你可以把先建立.docx文件该.doc也可以识别。
最后,我用的docx-templates这个库,模板复杂点,但是代码很简洁。
const { createReport } = require('docx-templates');
const fs = require('fs');
const template = fs.readFileSync('./template/test2.doc');
createReport({
template,
data: {
name: '测试的第一个工程',
surname: 'Appleseed',
},
}).then(res => {
fs.writeFileSync('./out/out6.doc', res)
});
简单几行代码可以实现导出了
这是我的模板
这个是导出的文件
菜鸡一个有什么说错的,欢迎大佬指正。