|
@@ -901,25 +901,37 @@ public final class DocxProcessUtil {
|
|
|
|
|
|
/**
|
|
|
* html转word
|
|
|
- *
|
|
|
- * @param html
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
*/
|
|
|
public static String html2Docx(WordprocessingMLPackage wordMLPackage, String html) throws Exception {
|
|
|
+ if (StringUtils.isBlank(html)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
initTmpPackage(wordMLPackage);
|
|
|
RFonts rfonts = Context.getWmlObjectFactory().createRFonts();
|
|
|
rfonts.setAscii("eastAsia");
|
|
|
XHTMLImporterImpl.addFontMapping("eastAsia", rfonts);
|
|
|
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
|
|
|
XHTMLImporter.setHyperlinkStyle("Hyperlink");
|
|
|
- String wordMl = "";
|
|
|
- wordMLPackage.getMainDocumentPart().getContent().addAll(
|
|
|
- XHTMLImporter.convert(html, TEMP_FILE_IMP));
|
|
|
+
|
|
|
+ try {
|
|
|
+ List<Object> values = XHTMLImporter.convert(html, TEMP_FILE_IMP);
|
|
|
+ wordMLPackage.getMainDocumentPart().getContent().addAll(values);
|
|
|
+ } catch (Docx4JException e) {
|
|
|
+ //内容示例:
|
|
|
+ // "" 会报错
|
|
|
+ // "abc" 会报错
|
|
|
+ // "<xxx>abc<xxx>" 不报错
|
|
|
+ logger.warn(e.getMessage());
|
|
|
+ throw new IllegalArgumentException("内容需要定义在HTML标签中");
|
|
|
+ }
|
|
|
+
|
|
|
//转换完后就初始化image
|
|
|
initPkgImage(wordMLPackage);
|
|
|
+
|
|
|
// 获取word文档中所有段落
|
|
|
List<Object> pList = getAllElementFromObject(wordMLPackage.getMainDocumentPart(), P.class);
|
|
|
+
|
|
|
+ String wordMl = "";
|
|
|
for (Object p : pList) {
|
|
|
wordMl += formatPWordMl(XmlUtils.marshaltoString(p));
|
|
|
}
|
|
@@ -1009,15 +1021,15 @@ public final class DocxProcessUtil {
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
-// System.out.println(formatPWordMl("<p>A</p>"));
|
|
|
-// System.out.println(getOptionNum("A.123123123"));
|
|
|
-// System.out.println(getTextInHtml("<p>#include <stdio.h></p>"));
|
|
|
- StringBuilder str = new StringBuilder("");
|
|
|
- str.append("<p>11</p>");
|
|
|
- str.append("<p></p>");
|
|
|
- String html = str.toString();
|
|
|
- int index = html.lastIndexOf("<p>");
|
|
|
- String htm = html.substring(0, index);
|
|
|
- System.out.println(htm);
|
|
|
- }
|
|
|
-}
|
|
|
+ StringBuilder str = new StringBuilder();
|
|
|
+// str.append("abc");
|
|
|
+// str.append("<span>abc</span>");
|
|
|
+ str.append("<span></span>");
|
|
|
+
|
|
|
+ WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
|
|
|
+ String html = CommonUtils.formatHtml(str.toString());
|
|
|
+ String wordXml = DocxProcessUtil.html2Docx(wordMLPackage, html);
|
|
|
+ System.out.println(wordXml);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|