温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

用poi实现doc转html

发布时间:2020-07-24 06:48:17 来源:网络 阅读:10134 作者:Red_Ant_hoyl 栏目:开发技术

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI