分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

用poi实现doc转html

发布时间:2023-09-06 02:14责任编辑:彭小芳关键词:暂无标签
废话,不多说直接上代码。

一、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(); ???????????}

演示:

走你:

用poi实现doc转html

原文地址:http://blog.51cto.com/13479739/2175483

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved