10、docx4j实现动态表格(模板式)单元格合并(含多列并列合并)
docx4jadmin 发布于:2018-10-15 14:54:51
阅读:loading
基于模板的动态单元格合并效果,以前在深入了解Jxls2.X的时候,写过一版在Excel中实现的多列并列合并处理效果,于是直接拷贝过来,对于Word里的表格来讲仍然合适,关于这个合并在Excel里面的效果时可转至Jxls2.X的相关文章查看,本篇文章重点在于在一个含有表格的模板中实现表格数据的动态循环,并且根据预置的要合并1、2、3列号,代码的实现即按照数据去进行合并展示,模板参考如下:
package cn.chendd.docx4j.examples;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBException;
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;
import cn.chendd.util.CellMergerUtil;
import cn.chendd.util.vo.CellRangeAddress;
public class TableTemplateMergeTest {
public static void main(String[] args) throws Exception {
String templatePath = System.getProperty("user.dir") + "/template/模板式表格_动态单元格合并_template.docx";
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(new java.io.File(templatePath));
//找到模板中的表格对象
ClassFinder find = new ClassFinder(Tbl.class);
new TraversalUtil(wordPackage.getMainDocumentPart().getContent(), find);
Tbl table = (Tbl) find.results.get(0);
//填充数据--并合并单元格
fillTableDataAndMerge(table);
String outPath = System.getProperty("user.dir") + "/template/out/模板式表格--动态单元格合并.docx";
wordPackage.save(new File(outPath));
}
//根据表格模板找到表格,并填充数据
private static void fillTableDataAndMerge(Tbl table) throws JAXBException {
List<Map<String , Object>> dataList = getDataList();
Tr dynamicTr = (Tr) table.getContent().get(2);//第三行约定为模板
String dynamicTrXml = XmlUtils.marshaltoString(dynamicTr);//获取模板行的xml数据
for (Map<String, Object> dataMap : dataList) {
Tr newTr = (Tr) XmlUtils.unmarshallFromTemplate(dynamicTrXml, dataMap);//填充模板行数据
table.getContent().add(newTr);
}
//删除模板行的占位行
table.getContent().remove(2);
//合并单元格,工具类,多列并列合并,但每次合并只是在同一个列中实现的并列合并
int mergerRows[] = {0 , 1 , 2};
Map<Integer, List<CellRangeAddress>> rangeMap = CellMergerUtil.getRanglesByData(dataList, mergerRows, 2);
CellMergerUtil.mergerColumns(table, rangeMap);
}
private static List<Map<String, Object>> getDataList() {
List<Map<String , Object>> dataList = new ArrayList<Map<String,Object>>();
Map<String , Object> m1 = new LinkedHashMap<String, Object>();
m1.put("companyName", "公司1");m1.put("deptName", "学术部");m1.put("realName", "渣渣1");m1.put("sex", "男");m1.put("remark", "备注1");
Map<String , Object> m2 = new LinkedHashMap<String, Object>();
m2.put("companyName", "公司1");m2.put("deptName", "学术部");m2.put("realName", "渣渣2");m2.put("sex", "男");m2.put("remark", "备注2");
Map<String , Object> m3 = new LinkedHashMap<String, Object>();
m3.put("companyName", "公司1");m3.put("deptName", "行政部");m3.put("realName", "渣渣3");m3.put("sex", "男");m3.put("remark", "备注3");
Map<String , Object> m4 = new LinkedHashMap<String, Object>();
m4.put("companyName", "公司2");m4.put("deptName", "行政部");m4.put("realName", "渣渣1");m4.put("sex", "男");m4.put("remark", "备注4");
Map<String , Object> m5 = new LinkedHashMap<String, Object>();
m5.put("companyName", "公司2");m5.put("deptName", "行政部");m5.put("realName", "渣渣1");m5.put("sex", "女");m5.put("remark", "备注5");
Map<String , Object> m6 = new LinkedHashMap<String, Object>();
m6.put("companyName", "公司3");m6.put("deptName", "行政部");m6.put("realName", "渣渣N");m6.put("sex", "女");m6.put("remark", "备注6");
dataList.add(m1);
dataList.add(m2);
dataList.add(m3);
dataList.add(m4);
dataList.add(m5);
dataList.add(m6);
return dataList;
}
}
点赞