java - 使用 apache POI 将 doc 转换为 html 剪切最终的 html 文件
问题描述
我使用 apache POI 4.0.0 将 .doc 转换为 .html。
private static String ProcessingDoc(File doc, String imagedir) throws IOException, ParserConfigurationException, TransformerConfigurationException, TransformerFactoryConfigurationError {
FileInputStream in = new FileInputStream(doc);
HWPFDocument doc_file = new HWPFDocument(in);
Document html_file = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
WordToHtmlConverter converter = new WordToHtmlConverter(html_file);
converter.setPicturesManager(new PicturesManager() {
@Override
public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches,
float heightInches) {
File imgFile = new File(getParentDirectory(doc));
if(!imgFile.exists()){
imgFile.mkdirs();
}
try {
FileOutputStream out = new FileOutputStream(imagedir+"/" + suggestedName);
out.write(content);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return suggestedName;
}
});
converter.processDocument(doc_file);
StringWriter stringWriter = new StringWriter();
Transformer transformer;
transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
transformer.setOutputProperty( OutputKeys.METHOD, "html" );
try {
transformer.transform(
new DOMSource( converter.getDocument() ),
new StreamResult( stringWriter ) );
} catch (TransformerException e) {
e.printStackTrace();
}
return stringWriter.toString();
}
}
但是 POI 创建了一些不完整的 html 文件,在文件的不同位置被剪切。它看起来像:
<some text of html document>
<tr class="r1">
<td class="td49">
<p class="p17"></p>
</td><td class="td50">
<p class="p17"></p>
</td><td class="td51">
它的 html 文件的结尾。转换过程中没有错误。
为什么我没有错误,也没有完整的文件?
感谢您的回答!
解决方案
推荐阅读
- angular - 如何改进:两个总和给定一个整数数组,返回两个数字的索引......使用角度
- c++ - 为什么在 BST 插入程序中使用 void 不起作用
- c# - 为什么我的 azure webjob 没有记录到存储 blob?
- javascript - 有什么办法可以在一个表单中使用多个输入?
- python - 当用户单击 tkinter 中的错误消息框时聚焦空输入框
- angular - 使用 angular-oauth2-oidc 实现 OAuth2 代码流
- javascript - 无法将 mysql 返回的对象推送到数组中
- css - 获取要在语义 UI 中的导航栏下显示的 Flash 消息
- html - Bootstrap 4:垂直和水平对齐 div 中的文本(具有给定的 div 高度)
- r - R Diff函数:处理每组的第一个值