java poi doc 转html_用poi实现doc转html

废话,不多说直接上代码。

一、doc转html,并且带文件夹

/ * word转html

* html转图片

* @param tagPath 转换html文件之后,所带的图片附件文件夹

* @param sourceFileName 源文件

* @param outPath 输出文件xx.html

* @return

* @throws Exception

*/

public static String docToHtml(String tagPath,

String sourceFileName, String outPath) throws Exception {

File file = new File(tagPath);

if(!file.exists()) {

file.mkdirs();

}

HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(sourceFileName));

org.w3c.dom.Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(document);

//保存图片,并返回图片的相对路径

wordToHtmlConverter.setPicturesManager((content, pictureType, name, width, height) -> {

try (FileOutputStream out = new FileOutputStream(tagPath + name)) {

out.write(content);

} catch (Exception e) {

e.printStackTrace();

}

return "image/" + name;

});

wordToHtmlConverter.processDocument(wordDocument);

org.w3c.dom.Document htmlDocument = wordToHtmlConverter.getDocument();

DOMSource domSource = new DOMSource(htmlDocument);

StreamResult streamResult = new StreamResult(new File(outPath));

TransformerFactory tf = TransformerFactory.newInstance();

Transformer serializer = tf.newTransformer();

serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");

serializer.setOutputProperty(OutputKeys.INDENT, "yes");

serializer.setOutputProperty(OutputKeys.METHOD, "html");

serializer.transform(domSource, streamResult);

return outPath;

}

调用:

/**doc

* 转html

*/

String tagPath = "D:\\red_ant_file\\20180915\\image\\";

String sourcePath = "D:\\red_ant_file\\20180915\\RedAnt的实验作业.doc";

String outPath = "D:\\red_ant_file\\20180915\\123.html";

try {

AllServiceIsHere.docToHtml(tagPath, sourcePath, outPath);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

演示:

b513ed74002fb517712ce188527cfa09.png

走你:

1e80d5eb0321c0d6d0ea8c48546e8200.png

7599b4efff8969fd1933e8b63b1beb09.png

f100b6f761bfd9e5e45cbda2830ff0f1.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值