2021-01-17

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)

});

简单几行代码可以实现导出了

这是我的模板
在这里插入图片描述
这个是导出的文件
在这里插入图片描述
菜鸡一个有什么说错的,欢迎大佬指正。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值