Apache POI创建的docx文档无法转化成html
当我通过word软件创建文档的时候
FileInputStream fileInputStream = new FileInputStream(sourceFileName);
//当你确定该文件可信任时使用该方法,避免大文件被误认为是zip炸弹,实际上不推荐上传大文件,会导致资源消耗过快
// ZipSecureFile.setMinInflateRatio(-1.0d);
XWPFDocument document = new XWPFDocument(fileInputStream);
System.out.println(document.getStyles());
XHTMLOptions options = XHTMLOptions.create().indent(4);
options.setExtractor(new FileImageExtractor(new File(imgPath)));
options.URIResolver(new BasicURIResolver(imgPath));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XHTMLConverter.getInstance().convert(document,baos,options);
baos.close();
content = baos.toString();
上面这段代码运行是正常的
但是当我通过apache poi创建docx文档的时候,却会提示
Exception in thread "main" org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.IllegalStateException: Expecting one Styles document part, but found 0
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:45)
at WordToHtml.WordToHtml.docToHtml(WordToHtml.java:112)
at WordToHtml.WordToHtml.main(WordToHtml.java:42)
Caused by: java.lang.IllegalStateException: Expecting one Styles document part, but found 0
at org.apache.poi.xwpf.usermodel.XWPFDocument.getStyle(XWPFDocument.java:461)
at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.<init>(XWPFStylesDocument.java:190)
at org.apache.poi.xwpf.converter.xhtml.internal.styles.CSSStylesDocument.<init>(CSSStylesDocument.java:100)
at org.apache.poi.xwpf.converter.xhtml.internal.XHTMLMapper.createStylesDocument(XHTMLMapper.java:147)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.<init>(XWPFDocumentVisitor.java:159)
at org.apache.poi.xwpf.converter.xhtml.internal.XHTMLMapper.<init>(XHTMLMapper.java:137)
at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.convert(XHTMLConverter.java:72)
... 5 more
我创建文档的代码如下:
package WordToHtml;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.File;
import java.io.FileOutputStream;
public class CreateDocx {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
FileOutputStream out = new FileOutputStream(new File("C:\\demo\\test.docx"));
XWPFParagraph firstParagraph = document.createParagraph();
firstParagraph.setAlignment(ParagraphAlignment.LEFT);
for (int i = 0;i<=50;i++) {
XWPFRun run = firstParagraph.createRun();
run.setText("Java POI 生成word文件。"+i);
run.addBreak();
run.setColor("696969");
}
firstParagraph.setStyle("");
//把word文档写到输出流
document.write(out);
}
}
请问这个是该怎么解决?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
相同的代码,可以正常运行,poi版本
4.1.1
可以试下Spire.Doc for Java控件,我用来创建过Word文档,效果不错,而且在创建时还支持设置段落的字体格式、对齐方式、段首缩进以及段落间距等。相关代码可参考他们官方教程。