求助,Apache API导出docx文件损坏

本文介绍了一个Java程序,尝试将HTML内容转换为Word文档(docx)。代码可以成功在WPS中打开,但在OfficeWord中出现错误,可能涉及编码或XML命名空间兼容性问题。
摘要由CSDN通过智能技术生成
    public static void main(String[] args) throws IOException {
        StringBuffer sb = new StringBuffer();
        sb.append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\" xmlns=\"http://www.w3.org/TR/REC-html40\" >");
        sb.append("<head></head>");
        sb.append("<body>");
        sb.append(text);
        sb.append("</body>");
        sb.append("</html>");
        //中文编码
        ByteArrayInputStream bais = null;
        FileOutputStream targetWord = null;
        try {
            byte[] bArr = sb.toString().getBytes("UTF-8");
            bais = new ByteArrayInputStream(bArr);
            POIFSFileSystem poifs = new POIFSFileSystem();
            DirectoryEntry directory = poifs.getRoot();
            DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
            //文件暂时放在本机下载文件夹
            targetWord = new FileOutputStream(new File(path + "测试word.docx"));
            poifs.writeFilesystem(targetWord);
            targetWord.flush();
            return;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bais) {
                bais.close();
            }
            if (null != targetWord) {
                targetWord.close();
            }
        }
    }

最终导出的docx文件在wps打开没问题,在office word打开就报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值