Erik Lundqvist wrote:Hi,
I need to create tables with merged cells. Is this something that can easily be done with docx4j? If so does anyone have a nice example of the best way to do this? Also general examples of working with tables would be welcome. The expected output here is a '.docx' file.
Regards,
Erik
For horizontal merge, you can create a cell with the code below and insert into a table row
- Code: Select all
public static Tc createTableCellGspan(WordprocessingMLPackage wordMLPackage,
P p, int gridspan) {
org.docx4j.wml.Tc tc = factory.createTc();
org.docx4j.wml.TcPr tcpr = factory.createTcPr();
tc.setTcPr(tcpr);
CTVerticalJc valign = factory.createCTVerticalJc();
valign.setVal(STVerticalJc.TOP);
tcpr.setVAlign(valign);
org.docx4j.wml.TcPrInner.GridSpan gspan = factory.createTcPrInnerGridSpan();
gspan.setVal(new BigInteger("" + gridspan));
tcpr.setGridSpan(gspan);
tc.getEGBlockLevelElts().add(p);
return tc;
}
For vertical merge, you can use the code below for your cell.
- Code: Select all
public static Tc createTableCellVMerge(WordprocessingMLPackage wordMLPackage,
P p, String vMerge) {
org.docx4j.wml.Tc tc = factory.createTc();
org.docx4j.wml.TcPr tcpr = factory.createTcPr();
tc.setTcPr(tcpr);
CTVerticalJc valign = factory.createCTVerticalJc();
valign.setVal(STVerticalJc.TOP);
tcpr.setVAlign(valign);
org.docx4j.wml.TcPrInner.VMerge vm = factory.createTcPrInnerVMerge();
vm.setVal(vMerge);
tcpr.setVMerge(vm);
tc.getEGBlockLevelElts().add(p);
return tc;
}
Hope this helps!