Apache POI创建的docx文档无法转化成html

二手代码程序员
  • 22

当我通过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);

    }
}

请问这个是该怎么解决?

回复
阅读 2.4k
2 个回答

相同的代码,可以正常运行,poi版本4.1.1

可以试下Spire.Doc for Java控件,我用来创建过Word文档,效果不错,而且在创建时还支持设置段落的字体格式、对齐方式、段首缩进以及段落间距等。相关代码可参考他们官方教程

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏