使用docx4j编程式地创建复杂的Word(.docx)文档

来源:互联网 发布:python 高斯分布函数 编辑:程序博客网 时间:2024/05/01 17:45

原文链接:Create complex Word (.docx) documents programatically with docx4j

原文作者:jos.dirksen

发表日期:2012年2月7日


两个月前,我需要用一些表格和段落创建动态的Word文档。过去我使用过POI做这些事情,但我发现它非常难用并且在我创建更加复杂的文档时它总不能很好地工作。所以在一番四处搜索之后,对于这个项目我决定使用docx4j。

根据官方网站的说法,Docx4j是一个:

"docx4j is a Java library for creating and manipulating Microsoft Open XML (Word docx, Powerpoint pptx, and Excel xlsx) files.

It is similar to Microsoft's OpenXML SDK, but for Java. "

在这篇文章中,我会向你展示几个可以用于生成word文档内容的示例,更具体地说,我们会看一下下面的两个例子:

  • 加载一个用于添加内容并保存为新文档的word文档模版
  • 向这个模版文档添加段落
  • 向这个模版文档添加表格
一般的方式是首先创建一个包含你最终文档布局和主要样式的Word文档。你需要在这个文档中添加用于搜索并替换为真实内容的占位符(简单的字符串)。

例如,一个非常基本的模版看起像这样:


在这篇文章中会向你展示如何填充这个模版最终得到这个:



加载一个用于添加内容的模版word文档并保存为一个新文档


首先,我们创建一个可用作模版的简单的word文档。对于此只需打开Word,创建新文档然后保存为template.docx,这就是我们将要用于添加内容的word文档。我们需要做的第一件事是使用docx4j将这个文档加载进来,你可以使用下面的几行代码做这件事:

[java] view plaincopyprint?
  1. private WordprocessingMLPackage getTemplate(String name) throws Docx4JException, FileNotFoundException {  
  2.     WordprocessingMLPackage template = WordprocessingMLPackage.load(new FileInputStream(new File(name)));  
  3.     return template;  
  4. }  

这样会返回一个表示完整的空白(在此时)文档Java对象。现在我们可以使用Docx4J API添加、删除以及更新这个word文档的内容,Docx4J有一些你可以用于遍历该文档的工具类。我自己写了几个助手方法使查找指定占位符并用真实内容进行替换的操作变地很简单。让我们来看一下其中的一个,这个计算是几个JAXB计算的包装器,允许你针对一个特定的类来搜索指定元素以及它所有的孩子,例如,你可以用它获取文档中所有的表格、表格中所有的行以及其它类似的操作。

[java] view plaincopyprint?
  1. private static List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {  
  2.     List<Object> result = new ArrayList<Object>();  
  3.     if (obj instanceof JAXBElement) obj = ((JAXBElement<?>) obj).getValue();  
  4.   
  5.     if (obj.getClass().equals(toSearch))  
  6.         result.add(obj);  
  7.     else if (obj instanceof ContentAccessor) {  
  8.         List<?> children = ((ContentAccessor) obj).getContent();  
  9.         for (Object child : children) {  
  10.             result.addAll(getAllElementFromObject(child, toSearch));  
  11.         }  
  12.   
  13.     }  
  14.     return result;  
  15. }  

没什么复杂的,但真的很有帮助。让我们看一下怎样使用这个方法。在这个例子中我们只是使用不同的值来替换简单的文本占位符,例如你动态设置一个文档的标题。首先,在前面创建的模版文档中添加一个自定义占位符,我使用SJ_EX1作为占位符,我们将要用name参数来替换这个值。在docx4j中基本的文本元素用org.docx4j.wml.Text类来表示,替换这个简单的占位符我们需要做的就是调用这个方法:

[java] view plaincopyprint?
  1. private void replacePlaceholder(WordprocessingMLPackage template, String name, String placeholder ) {  
  2.     List<Object> texts = getAllElementFromObject(template.getMainDocumentPart(), Text.class);  
  3.   
  4.     for (Object text : texts) {  
  5.         Text textElement = (Text) text;  
  6.         if (textElement.getValue().equals(placeholder)) {  
  7.             textElement.setValue(name);  
  8.         }  
  9.     }  
  10. }  

这会在文档中查找所有的Text元素,并且与占位符匹配的Text都将被我们指定的值替换,现在我们需要做的仅是将这个文档写回一个文件中。

[java] view plaincopyprint?
  1. private void writeDocxToStream(WordprocessingMLPackage template, String target) throws IOException, Docx4JException {  
  2.     File f = new File(target);  
  3.     template.save(f);  
  4. }  

如你所见,并不困难。

按这种方式,我们也可以向word文档添加更加复杂的内容,确定如何添加特定内容最简单的方式就是查看word文档的XML源码,这会告诉你需要什么样的包装及Word如何编排XML。在下一个例子中我们会看一下怎样添加一个段落。


向模版文档添加段落


你可能想知道为什么我们需要添加段落?我们已经可以添加文本,难道段落不就是一大段的文本吗?好吧,既是也不是,一个段落确实看起来像是一大段文本,但你需要考虑的是换行符,如果你像前面一样添加一个Text元素并且在文本中添加换行符,它们并不会出现,当你想要换行符时,你就需要创建一个新的段落。然而,幸运的是这对于Docx4j来说也非常地容易。

做这个需要下面的几步:

  1. 从模版中找到要替换的段落
  2. 将输入文本拆分成单独的行
  3. 每一行基于模版中的段落创建一个新的段落
  4. 移除原来的段落
使用我们已经拥有的助手方法也并不困难。

[java] view plaincopyprint?
  1. private void replaceParagraph(String placeholder, String textToAdd, WordprocessingMLPackage template, ContentAccessor addTo) {  
  2.     // 1. get the paragraph   
  3.     List<Object> paragraphs = getAllElementFromObject(template.getMainDocumentPart(), P.class);  
  4.   
  5.     P toReplace = null;  
  6.     for (Object p : paragraphs) {  
  7.         List<Object> texts = getAllElementFromObject(p, Text.class);  
  8.         for (Object t : texts) {  
  9.             Text content = (Text) t;  
  10.             if (content.getValue().equals(placeholder)) {  
  11.                 toReplace = (P) p;  
  12.                 break;  
  13.             }  
  14.         }  
  15.     }  
  16.   
  17.     // we now have the paragraph that contains our placeholder: toReplace  
  18.     // 2. split into seperate lines   
  19.     String as[] = StringUtils.splitPreserveAllTokens(textToAdd, '\n');  
  20.   
  21.     for (int i = 0; i < as.length; i++) {  
  22.         String ptext = as[i];  
  23.   
  24.         // 3. copy the found paragraph to keep styling correct  
  25.         P copy = (P) XmlUtils.deepCopy(toReplace);  
  26.   
  27.         // replace the text elements from the copy  
  28.         List<?> texts = getAllElementFromObject(copy, Text.class);  
  29.         if (texts.size() > 0) {  
  30.             Text textToReplace = (Text) texts.get(0);  
  31.             textToReplace.setValue(ptext);  
  32.         }  
  33.   
  34.         // add the paragraph to the document  
  35.         addTo.getContent().add(copy);  
  36.     }  
  37.   
  38.     // 4. remove the original one   
  39.     ((ContentAccessor)toReplace.getParent()).getContent().remove(toReplace);  
  40.   
  41. }  

在这个方法中我们使用提供的文本替换了段落的内容,然后将新的段落指定为addTo方法的参数。

[java] view plaincopyprint?
  1. String placeholder = "SJ_EX1";  
  2. String toAdd = "jos\ndirksen";  
  3.   
  4. replaceParagraph(placeholder, toAdd, template, template.getMainDocumentPart());  

如果你用更多的内容针对模版文档运行这个例子,你会注意到这些段落出现在你文档的底部。原因是段落被添加回主文档,如果你希望段落被添加到文档的指定位置(你通常会希望如此),你可以将它们包到一个1X1无边框的表格中,这个表格被视为段落的父亲并且新的段落可以添加到那里。


在模版文档中添加表格


我准备展示的最后一个例子是如何向一个word模版添加表格,一个更适合的表述应该是,如何填充word模版中预定义的表格。就像我们对文本和段落所做的一样,将要替换占位符。为了本例要在你的word文档中添加一个简单的表格(你可以设置喜欢的样式),在表格中添加一个“仿制行”(原文:dummy row,傀儡行?假的行?不知道怎样翻译,意思就是模版行)作为内容的模版。在代码中我们将要查找到该行,复制它,并且在Java代码中使用新行替换内容,如下:

  1. 找到包含其中一个关键字的表格
  2. 复制用作行模版的行
  3. 针对每一条的数据,向表格添加基于行模版创建的一行
  4. 移除原来的模版行
跟我们针对段落时展示的方法一样,首先来看一下我们将要提供怎样的替换数据。对于本例,我提供了一个hashmap的集合,其中包含要被替换替换的占位符名称和替换它的值,同时也提供了可以在表格行中发现的替换符。

[java] view plaincopyprint?
  1. Map<String,String> repl1 = new HashMap<String, String>();  
  2. repl1.put("SJ_FUNCTION""function1");  
  3. repl1.put("SJ_DESC""desc1");  
  4. repl1.put("SJ_PERIOD""period1");  
  5.   
  6. Map<String,String> repl2 = new HashMap<String, String>();  
  7. repl2.put("SJ_FUNCTION""function2");  
  8. repl2.put("SJ_DESC""desc2");  
  9. repl2.put("SJ_PERIOD""period2");  
  10.   
  11. Map<String,String> repl3 = new HashMap<String, String>();  
  12. repl3.put("SJ_FUNCTION""function3");  
  13. repl3.put("SJ_DESC""desc3");  
  14. repl3.put("SJ_PERIOD""period3");  
  15.   
  16. replaceTable(new String[]{"SJ_FUNCTION","SJ_DESC","SJ_PERIOD"}, Arrays.asList(repl1,repl2,repl3), template);  

现在,replaceTable方法如下所示:

[java] view plaincopyprint?
  1. private void replaceTable(String[] placeholders, List<Map<String, String>> textToAdd,  
  2.             WordprocessingMLPackage template) throws Docx4JException, JAXBException {  
  3.     List<Object> tables = getAllElementFromObject(template.getMainDocumentPart(), Tbl.class);  
  4.   
  5.     // 1. find the table   
  6.     Tbl tempTable = getTemplateTable(tables, placeholders[0]);  
  7.     List<Object> rows = getAllElementFromObject(tempTable, Tr.class);  
  8.   
  9.     // first row is header, second row is content  
  10.     if (rows.size() == 2) {  
  11.         // this is our template row  
  12.         Tr templateRow = (Tr) rows.get(1);  
  13.   
  14.         for (Map<String, String> replacements : textToAdd) {  
  15.             // 2 and 3 are done in this method  
  16.             addRowToTable(tempTable, templateRow, replacements);  
  17.         }  
  18.   
  19.         // 4. remove the template row  
  20.         tempTable.getContent().remove(templateRow);  
  21.     }  
  22. }  

该方法找到表格,获取第一行并且遍历提供的map向表格添加新行,在将其返回之前删除模版行。这个方法用到了两个助手方法:addRowToTable 和 getTemplateTable。我们首先看一下后面的那个:

[java] view plaincopyprint?
  1. private Tbl getTemplateTable(List<Object> tables, String templateKey) throws Docx4JException, JAXBException {  
  2.     for (Iterator<Object> iterator = tables.iterator(); iterator.hasNext();) {  
  3.         Object tbl = iterator.next();  
  4.         List<?> textElements = getAllElementFromObject(tbl, Text.class);  
  5.         for (Object text : textElements) {  
  6.             Text textElement = (Text) text;  
  7.             if (textElement.getValue() != null && textElement.getValue().equals(templateKey))  
  8.                 return (Tbl) tbl;  
  9.         }  
  10.     }  
  11.     return null;  
  12. }  

这个方法只是查看表格是否含有我们的占位符,如果有则返回表格。addRowToTable方法也很简单:

[java] view plaincopyprint?
  1. private static void addRowToTable(Tbl reviewtable, Tr templateRow, Map<String, String> replacements) {  
  2.     Tr workingRow = (Tr) XmlUtils.deepCopy(templateRow);  
  3.     List<?> textElements = getAllElementFromObject(workingRow, Text.class);  
  4.     for (Object object : textElements) {  
  5.         Text text = (Text) object;  
  6.         String replacementValue = (String) replacements.get(text.getValue());  
  7.         if (replacementValue != null)  
  8.             text.setValue(replacementValue);  
  9.     }  
  10.   
  11.     reviewtable.getContent().add(workingRow);  
  12. }  

本方法复制模版并且使用给定的值替换模版行中的占位符,然后这个复制行被添加到表格,就这么简单。使用这块代码我们可以在保持表格样式和布局的同时填充word文档中任意的表格。

这篇文章就到这里,使用段落和表格你可以创建很多不同风格的文档,而且这与通常生成的文档风格能很好地匹配。相同的方式也适用于向word文档中添加其它类型的内容。

0 0