poi 3.14下报错:
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)
at com.landy.tools.Test.docxToHtml(Test.java:34)
at com.landy.tools.Test.main(Test.java:44)
Caused by: java.lang.NullPointerException
at org.apache.poi.xwpf.converter.core.styles.run.RunUnderlineValueProvider.getValue(RunUnderlineValueProvider.java:40)
at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.getUnderline(XWPFStylesDocument.java:687)
at org.apache.poi.xwpf.converter.xhtml.internal.styles.CSSStylesDocument.createCSSStyle(CSSStylesDocument.java:296)
at org.apache.poi.xwpf.converter.xhtml.internal.styles.CSSStylesDocument.createCSSStyle(CSSStylesDocument.java:249)
at org.apache.poi.xwpf.converter.xhtml.internal.XHTMLMapper.visitRun(XHTMLMapper.java:248)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitRuns(XWPFDocumentVisitor.java:616)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitParagraphBody(XWPFDocumentVisitor.java:441)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitParagraph(XWPFDocumentVisitor.java:332)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:247)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:199)
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:73)
... 5 more
poi 3.9 报错:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/xwpf/usermodel/IRunBody
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:72)
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)
at com.landy.tools.Test.docxToHtml(Test.java:34)
at com.landy.tools.Test.main(Test.java:44)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.xwpf.usermodel.IRunBody
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 6 more
原因是一样的:
通过调式定位,poi 类的源码不严谨造成;
package org.apache.poi.xwpf.converter.core.styles.run;
import org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
public class RunUnderlineValueProvider extends AbstractRunValueProvider<UnderlinePatterns>{
public static final RunUnderlineValueProvider INSTANCE = new RunUnderlineValueProvider();
@Override
public UnderlinePatterns getValue( CTRPr rPr, XWPFStylesDocument stylesDocument){
return ( rPr != null && rPr.isSetU() ) ? UnderlinePatterns.valueOf( rPr.getU().getVal().intValue() ): null;
}
}
public class RunUnderlineValueProvider extends AbstractRunValueProvider<UnderlinePatterns>{
public static final RunUnderlineValueProvider INSTANCE = new RunUnderlineValueProvider();
@Override
public UnderlinePatterns getValue( CTRPr rPr, XWPFStylesDocument stylesDocument){
// 注意返回时rPr.getU()!=null && rPr.getU().getVal()!=null 判空
return ( rPr != null && rPr.isSetU() && rPr.getU()!=null && rPr.getU().getVal()!=null) ? UnderlinePatterns.valueOf( rPr.getU().getVal().intValue() ): null;
}
}
这样就会覆盖掉源码(优先调用)
最终解决的办法是修改jar包版本,一定要对应上。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.core</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
<version>1.0.6</version>
</dependency>