溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

用poi實(shí)現(xiàn)doc轉(zhuǎn)html

發(fā)布時(shí)間:2020-07-24 06:48:17 來源:網(wǎng)絡(luò) 閱讀:10151 作者:Red_Ant_hoyl 欄目:開發(fā)技術(shù)

廢話,不多說直接上代碼。

一、doc轉(zhuǎn)html,并且?guī)募A

    / * word轉(zhuǎn)html
     * html轉(zhuǎn)圖片
     * @param tagPath   轉(zhuǎn)換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);
        //保存圖片,并返回圖片的相對(duì)路徑
        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;
}

調(diào)用:

/**doc
             * 轉(zhuǎn)html
             */
            String tagPath = "D:\\red_ant_file\\20180915\\image\\";
            String sourcePath = "D:\\red_ant_file\\20180915\\RedAnt的實(shí)驗(yàn)作業(yè).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實(shí)現(xiàn)doc轉(zhuǎn)html

走你:

用poi實(shí)現(xiàn)doc轉(zhuǎn)html

用poi實(shí)現(xiàn)doc轉(zhuǎn)html

用poi實(shí)現(xiàn)doc轉(zhuǎn)html

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI