关于Apache POI将word文档转换为html出现Converter空指针的问题

大致异常如下

org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:77)
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:63)
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:38)
at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:61)
 

笔者引用的jar包情况及代码如下

jar包:

  <poi-version>3.17</poi-version>

             <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi-version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-scratchpad</artifactId>
                <version>${poi-version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi-version}</version>
            </dependency>

         <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.xhtml</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.core</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>xdocreport</artifactId>
            <version>2.0.1</version>
        </dependency>

代码:


import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * houjx3
 * 版本要对得上,就能解决问题
 */
public class DocUtils {
    public static String urlWord2007ToHtml(String urlString) throws IOException {
        FileInputStream in = new FileInputStream(urlString);
        XWPFDocument document = new XWPFDocument(in);
        // 也可以使用字符数组流获取解析的内容
//        XHTMLOptions xhtmlOptions = XHTMLOptions.create();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XHTMLConverter.getInstance().convert(document, baos, null);
        String content = baos.toString();
        baos.close();
        return content;
    }

    public static void main(String[] args) throws Exception {
        String url = "C:\\Users\\houjx3\\Desktop\\“test.docx";
        String str = urlWord2007ToHtml(url);
        System.out.println(str);
    }
}

问题解决:

      

将<poi-version>3.17</poi-version> 改为 <poi-version>4.0.1</poi-version>

将
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.core</artifactId>
            <version>2.0.1</version>
        </dependency>
改为
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.core</artifactId>
            <version>2.0.2</version>
        </dependency>

注意: 改完之后可能会有jar包冲突的情况, 需要解决完jar包冲突才能解析成功, 可以使用IDEA的插件 Maven Helper 来解决jar冲突(用起来方便).

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值