使用忠告
使用該方式進(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("</") ) {
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();
}