word與html互轉(zhuǎn)(2) -- html轉(zhuǎn)word

使用忠告

使用該方式進(jìn)行xhtml到word的轉(zhuǎn)換, 簡(jiǎn)單轉(zhuǎn)換是可以, 但是可能并沒(méi)有想象中那么滿意, 轉(zhuǎn)換出來(lái)的word格式并不完美, 比如目錄和標(biāo)題都會(huì)丟失, 標(biāo)題顯示看起來(lái)一樣, 但是是用正文加粗和加大字號(hào)來(lái)顯示的. 畢竟word是一種文檔格式, 而html是一種標(biāo)記性語(yǔ)言, 要想實(shí)現(xiàn)完美兼容和轉(zhuǎn)換很難

加上word與html互轉(zhuǎn)(1) -- word轉(zhuǎn)html, 雖然word與html互轉(zhuǎn)都有實(shí)現(xiàn)手段, 但是考慮到轉(zhuǎn)換的格式復(fù)雜度和后期的維護(hù)成本, 我們最后放棄去實(shí)現(xiàn)這個(gè)成本高但是對(duì)項(xiàng)目影響不大的功能

實(shí)現(xiàn)

實(shí)現(xiàn)方式

使用poi+xdocreport來(lái)實(shí)現(xiàn)
poi:都熟悉, 這邊不作介紹
Docx4j:是github上的一個(gè)開(kāi)源項(xiàng)目, 使用起來(lái)很簡(jiǎn)單, 可以很輕松的將xhtml轉(zhuǎn)為docx, 他的具體介紹可以去他的項(xiàng)目地址查看--項(xiàng)目地址

引入相關(guān)程序包

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.14</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.14</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.14</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.14</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.3</version>
</dependency>
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-ImportXHTML</artifactId>
    <version>3.0.0</version>
</dependency>

html轉(zhuǎn)docx

public static void main(String[] args) throws Exception {
        String inputfilepath = "C:\\Users\\Administrator\\Desktop\\test.html";
        String baseURL = "C:\\Users\\Administrator\\Desktop";

        String stringFromFile = FileUtils.readFileToString(new File(inputfilepath), "UTF-8");

        String unescaped = stringFromFile;
        if (stringFromFile.contains("&lt;/") ) {
            unescaped = StringEscapeUtils.unescapeHtml(stringFromFile);
        }
        // 設(shè)置字體映射
        RFonts rfonts = Context.getWmlObjectFactory().createRFonts();
        rfonts.setAscii("Century Gothic");
        XHTMLImporterImpl.addFontMapping("Century Gothic", rfonts);

        // 創(chuàng)建一個(gè)空的docx對(duì)象
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
        XHTMLImporter importer = new XHTMLImporterImpl(wordMLPackage);
        importer.setTableFormatting(FormattingOption.IGNORE_CLASS);
        importer.setParagraphFormatting(FormattingOption.IGNORE_CLASS);

        NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
        wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
        ndp.unmarshalDefaultNumbering();

        // 轉(zhuǎn)換XHTML,并將其添加到我們制作的空docx中
        XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);

        XHTMLImporter.setHyperlinkStyle("Hyperlink");
        wordMLPackage.getMainDocumentPart().getContent().addAll(
                XHTMLImporter.convert(unescaped, baseURL));
        wordMLPackage.save(new java.io.File("C:\\Users\\Administrator\\Desktop\\test.docx"));
}

html轉(zhuǎn)doc

public void test() throws IOException {
    //這邊我為了測(cè)試, 使用的是自己拼接出html
    String html = getHtml();
    byte b[] = html.getBytes("utf-8");

    // 根據(jù)數(shù)組
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    POIFSFileSystem poifs = new POIFSFileSystem();
    DirectoryEntry directory = poifs.getRoot();
    directory.createDocument("WordDocument", bais);
    poifs.writeFilesystem(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.doc"));
}

public String getHtml() {
    StringBuilder html = new StringBuilder();
    html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
    html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
    html.append("<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>");
    html.append("</head><body>");
    html.append("<ol style='list-style-type: decimal;' class=' list-paddingleft-2'>");
    html.append("<li><p><strong>111</strong></p></li>");
    html.append("<li><p><em>2222</em></p></li>");
    html.append("<li><p><span style='text-decoration: underline;'>33333</span></p></li>");
    html.append("<li><p><span style='text-decoration: line-through; border: medium none;'>444444</span></p></li>");
    html.append("<li><img src='https://csdnimg.cn/pubfooter/images/csdn_cs_qr.png'></img></li>");
    html.append("</ol>");
    html.append("<p><br/><span style='text-decoration: line-through; border: medium none;'></span></p>");
    html.append("<table><tbody>");
    html.append("<tr class='firstRow'>");
    html.append("<td style='word-break: break-all;' width='402' valign='top'><span style='color: #ff0000'>table1<br/></span></td>");
    html.append("<td style='word-break: break-all;' width='402' valign='top'><p><span style='color: #ff0000'>table2</span></p></td>");
    html.append("</tr>");
    html.append("<tr>");
    html.append("<td style='word-break: break-all;' class='selectTdClass' width='402' valign='top'><strong>table3<br/></strong></td>");
    html.append("<td style='word-break: break-all;' class='selectTdClass' width='402' valign='top'><p><strong>table4</strong></p></td>");
    html.append("</tr>");
    html.append("</tbody></table>");
    html.append("<p><br/></p>");   
    html.append("</body></html>");
    return html.toString();
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 使用忠告 使用該方式進(jìn)行xhtml到word的轉(zhuǎn)換, 簡(jiǎn)單轉(zhuǎn)換是可以, 但是可能并沒(méi)有想象中那么滿意, 轉(zhuǎn)換出來(lái)的...
    睡不醒的猿閱讀 11,255評(píng)論 0 7
  • 最近在做一個(gè)Web項(xiàng)目,需要用戶把Word上傳然后展示到頁(yè)面,但是Word在線瀏覽的功能不及PDF,因此需要將Wo...
    GISerliang閱讀 77,384評(píng)論 20 26
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,817評(píng)論 1 45
  • 埋葬! 不喜憂傷哭泣 不喜看到別人憂傷哭泣 如果忍不住哭泣 就把你自己埋葬 埋葬別人很容易! 埋葬自己很難! 一次...
    縱情嬉戲天地間閱讀 381評(píng)論 0 0
  • 韓劇《請(qǐng)回答1988》里有那么一個(gè)情節(jié)。女主角的奶奶去世了,她很傷心一直哭??奘撬磉_(dá)對(duì)奶奶感情的一種方式。 但是...
    愛(ài)之星閱讀 1,096評(píng)論 0 0

友情鏈接更多精彩內(nèi)容