word与html互转(2) -- html转word

使用忠告

使用该方式进行xhtml到word的转换, 简单转换是可以, 但是可能并没有想象中那么满意, 转换出来的word格式并不完美, 比如目录和标题都会丢失, 标题显示看起来一样, 但是是用正文加粗和加大字号来显示的. 毕竟word是一种文档格式, 而html是一种标记性语言, 要想实现完美兼容和转换很难

加上word与html互转(1) -- word转html, 虽然word与html互转都有实现手段, 但是考虑到转换的格式复杂度和后期的维护成本, 我们最后放弃去实现这个成本高但是对项目影响不大的功能

实现

实现方式

使用poi+xdocreport来实现
poi:都熟悉, 这边不作介绍
Docx4j:是github上的一个开源项目, 使用起来很简单, 可以很轻松的将xhtml转为docx, 他的具体介绍可以去他的项目地址查看--项目地址

引入相关程序包

<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转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);
        }
        // 设置字体映射
        RFonts rfonts = Context.getWmlObjectFactory().createRFonts();
        rfonts.setAscii("Century Gothic");
        XHTMLImporterImpl.addFontMapping("Century Gothic", rfonts);

        // 创建一个空的docx对象
        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();

        // 转换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转doc

public void test() throws IOException {
    //这边我为了测试, 使用的是自己拼接出html
    String html = getHtml();
    byte b[] = html.getBytes("utf-8");

    // 根据数组
    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();
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 使用忠告 使用该方式进行xhtml到word的转换, 简单转换是可以, 但是可能并没有想象中那么满意, 转换出来的...
    睡不醒的猿阅读 11,075评论 0 7
  • 最近在做一个Web项目,需要用户把Word上传然后展示到页面,但是Word在线浏览的功能不及PDF,因此需要将Wo...
    GISerliang阅读 77,115评论 20 26
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,879评论 1 45
  • 埋葬! 不喜忧伤哭泣 不喜看到别人忧伤哭泣 如果忍不住哭泣 就把你自己埋葬 埋葬别人很容易! 埋葬自己很难! 一次...
    纵情嬉戏天地间阅读 359评论 0 0
  • 韩剧《请回答1988》里有那么一个情节。女主角的奶奶去世了,她很伤心一直哭。哭是她表达对奶奶感情的一种方式。 但是...
    爱之星阅读 968评论 0 0