poi xwpf 转换成 html,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.(XWPFStylesDocument.java:190)
at org.apache.poi.xwpf.converter.xhtml.internal.styles.CSSStylesDocument.(CSSStylesDocument.java:100)
at org.apache.poi.xwpf.converter.xhtml.internal.XHTMLMapper.createStylesDocument(XHTMLMapper.java:147)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.(XWPFDocumentVisitor.java:159)
at org.apache.poi.xwpf.converter.xhtml.internal.XHTMLMapper.(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);
}
}
請問這個是該怎么解決?
總結
以上是生活随笔為你收集整理的poi xwpf 转换成 html,Apache POI创建的docx文档无法转化成html的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html页面右侧滚动,HTML5页面点击
- 下一篇: html 画动画效果,html5 can