博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用poi实现doc转html
阅读量:7027 次
发布时间:2019-06-28

本文共 2290 字,大约阅读时间需要 7 分钟。

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

一、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

走你:

用poi实现doc转html

用poi实现doc转html

用poi实现doc转html

转载于:https://blog.51cto.com/13479739/2175483

你可能感兴趣的文章
Codeforces Round #353 (Div. 2)
查看>>
20145234黄斐《Java程序设计》第六周学习总结
查看>>
js数据类型?
查看>>
使用BTRACE定位系统中慢的问题
查看>>
使用autoit,可以节省您很多时间
查看>>
关闭mysql validate-password插件
查看>>
Linux之 网卡发包、接包 error 、droped 情况
查看>>
zabbix之 自动发现磁盘io util 监控
查看>>
Zookeeper的RPC框架
查看>>
开发PL/SQL子程序——过程
查看>>
管理表空间和数据文件——建立表空间——建立本地管理表空间
查看>>
使用Spring Cloud Feign
查看>>
Ambari Agent 架构
查看>>
NumPy基础知识:数组和矢量计算
查看>>
NOJ-1162-简单编码
查看>>
jsp页面执行原理及获取上下文根方式
查看>>
牛客小白月赛6 E 对弈 思维
查看>>
linux RAC 安装失败完全卸载
查看>>
自己封装了一个EF的上下文类.,分享一下,顺便求大神指点
查看>>
Http的Get和Post--扫盲篇
查看>>