Original Address: https: //chendd.cn/information/viewInformation/other/257.a
In addition to programmatically create a table in the articles mentioned before, template-based implementation list table is also very popular or useful, the previous article I said I only realized about template-based analytical methods caused jxel template, so the examples of articles in order to achieve a static implementation tabular data and dynamic data circular list, and the style is adjusted based on the template type table, reference template shots are as follows:
The code is implemented as:
package cn.chendd.docx4j.examples;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.docx4j.Docx4J;
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.finders.ClassFinder;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.Tr;
public class TableTemplateTest {
public static void main(String[] args) throws Exception {
. = The templatePath the System String getProperty ( "user.dir") + "/ Template / template type table _template.docx";
. = String outPath the System getProperty ( "user.dir") + "/ Template / OUT / template type table .docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(newjava.io.File(templatePath));
HashMap<String, String> mappings = new HashMap<String, String>();
// constructor table acyclic data grid
mappings.put("name", "马参军");
mappings.put("sex", "男");
mappings.put ( "skill", "San rumor: there are more than three people into a tiger thing");
// data structure of a circular list
ClassFinder find = new ClassFinder(Tbl.class);
new TraversalUtil(wordMLPackage.getMainDocumentPart().getContent(), find);
Tbl table = (Tbl) find.results.get(1);
Tr dynamicTr = (Tr) table.getContent () get (1);. // convention for second line template
DynamicTrXml = XmlUtils String. MarshaltoString (dynamicTr); // Get the template xml data row
List<Map<String , Object>> dataList = getDataList();
for (Map<String, Object> dataMap : dataList) {
NewTr = Tr (Tr) XmlUtils. UnmarshallFromTemplate (dynamicTrXml, Datamap); // row to populate the template data
table.getContent().add(newTr);
}
// delete the placeholder line template row
table.getContent().remove(1);
. WordMLPackage.getMainDocumentPart () variableReplace (mappings); // set the global variable substitution
Docx4J.save(wordMLPackage, new File(outPath));
}
// loop configuration data
private static List<Map<String , Object>> getDataList() {
List<Map<String , Object>> dataList = new ArrayList<Map<String , Object>>();
Map<String , Object> m1 = new HashMap<String , Object>();
m1.put("item.number", "1");m1.put("item.name", "关银萍");
m1.put ( "item.sex", "woman"); m1.put ( "item.skill", "Come on, broadsword");
dataList.add(m1);
Map<String , Object> m2 = new HashMap<String , Object>();
m2.put("item.number", "2");m2.put("item.name", "马云禄");
m2.put ( "item.sex", "woman"); m2.put ( "item.skill", "hell, square piece Mopai hand, see the placeholder placeholder wrap style effects, occupying accounts bit style effects seen wrap ");
dataList.add(m2);
Map<String , Object> m3 = new HashMap<String , Object>();
m3.put("item.number", "3");m3.put("item.name", "张星彩");
m3.put ( "item.sex", "woman"); m3.put ( "item.skill", "Zhangyingzaishou, wipe wipe");
dataList.add(m3);
return dataList;
}
}
Generated files:
Sample generates file download