Apache POI 将 HTML/XHTML 转换为 DOC/DOCX

Apache POI convert HTML/XHTML to DOC/DOCX

提问人:Alan Vallv i Bonet External 提问时间:1/10/2023 最后编辑:Olaf KockAlan Vallv i Bonet External 更新时间:1/10/2023 访问量:399

问:

我需要将 HTML 转换为 doc 文件,HTML 中填充了自定义信息,图像和 CSS 会根据请求而变化。

我正在尝试为此使用 Apache POI,但我遇到了错误

`
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:72)
    at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:58)
    at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:38)
    at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)

我的代码是这样的:

  // Load the HTML file
            //Doc file
            String htmlFile = "pathToHtml/file.html";
            //String htmlFile = parseHTMLTemplate(disputeLetterDetails, template, fileExtension);
    
            //new File(htmlFile);
            //File file = new FileReader(htmlFile);
    
            Path path = Path.of(htmlFile);
    
            OutputStream in = new FileOutputStream(htmlFile, true);
    
    
            // Create a new XWPFDocument
            XWPFDocument document = new XWPFDocument();
            // Set up the XHTML options
            XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(new File("./images/")));
            options.setExtractor(new FileImageExtractor(new File("./images/")));
            // Convert the HTML to XWPFDocument
            XHTMLConverter.getInstance().convert(document, in, options);
            // Save the document to a .doc file
            FileOutputStream out = new FileOutputStream("pathToHtml/OUT_from_XHTML_TEST.docx");
            document.write(out);
            out.close();
    
    `

我想从具有相同样式的 HTML 文件中获取 docx 文件,但我收到此错误”

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:72)
    at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:58)
    at org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.doConvert(XHTMLConverter.java:38)
    at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)
`
java apache-poi xhtml 文档

评论

0赞 Axel Richter 1/11/2023
OutputStream in井。。。为什么?第一:这不是 Apache POI 项目的一部分。第二:它尝试XHTML转换为XHTML,而不是相反。第三:很旧,不会从当前的 Apache POI 版本中使用。当前版本是 。XHTMLConverterXWPFDocumentorg.apache.poi.xwpf.converter.xhtml.XHTMLConverterXWPFDocumentfr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter

答: 暂无答案