表格内容可以简单地用TblFactory生成一个,例如:
package me.test.docx4j;
import java.io.File;
import org.docx4j.jaxb.Context;
import org.docx4j.model.table.TblFactory;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Body;
import org.docx4j.wml.Document;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.Tc;
import org.docx4j.wml.Text;
import org.docx4j.wml.Tr;
import org.junit.Test;
public class SimpleTable {
@Test
public void doTest() throws Docx4JException {
WordprocessingMLPackage pkg = WordprocessingMLPackage.createPackage();
MainDocumentPart main = pkg.getMainDocumentPart();
Document doc = main.getContents();
Body body = doc.getBody();
int writableWidthTwips = pkg.getDocumentModel().getSections()
.get(0).getPageDimensions().getWritableWidthTwips();
Tbl table = TblFactory.createTable(3, 3, writableWidthTwips/3);
Tr r1 = (Tr)table.getContent().get(0);
this.setText((Tc)r1.getContent().get(0), "A");
Tr r2 = (Tr)table.getContent().get(1);
this.setText((Tc)r2.getContent().get(1), "B");
Tr r3 = (Tr)table.getContent().get(2);
this.setText((Tc)r3.getContent().get(2), "C");
body.getContent().add(table);
pkg.save(new File(System.getProperty("user.dir"), "Simple Table.docx"));
}
protected ObjectFactory factory = Context.getWmlObjectFactory();;
protected Tc setText(Tc tc, String text) {
P p = (P)tc.getContent().get(0);
R r = factory.createR();
Text t = factory.createText();
t.setValue(text);
r.getContent().add(t);
p.getContent().add(r);
return tc;
}
}
注意:所有的文字都要放在段落里才能加到单元格里去, 否则生成文件识别不了。
表格有很多的格式可以设置,例如下面这个例子,就不用TblFactory。
package me.test.docx4j;
import java.io.File;
import java.math.BigInteger;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Body;
import org.docx4j.wml.CTBorder;
import org.docx4j.wml.CTTblLayoutType;
import org.docx4j.wml.CTTblPrBase.TblStyle;
import org.docx4j.wml.Document;
import org.docx4j.wml.Jc;
import org.docx4j.wml.JcEnumeration;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.docx4j.wml.STBorder;
import org.docx4j.wml.STTblLayoutType;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.TblBorders;
import org.docx4j.wml.TblPr;
import org.docx4j.wml.TblWidth;
import org.docx4j.wml.Tc;
import org.docx4j.wml.TcPr;
import org.docx4j.wml.TcPrInner.GridSpan;
import org.docx4j.wml.TcPrInner.VMerge;
import org.docx4j.wml.Text;
import org.docx4j.wml.Tr;
import org.junit.Test;
public class FullTableSample {
protected ObjectFactory factory = Context.getWmlObjectFactory();
public static final class VMergeEnum {
public static final VMerge RESTART;
public static final VMerge CONTINUE;
public static final VMerge DEFAULT;
static {
RESTART = create("restart");
CONTINUE = create("continue");
DEFAULT = new VMerge();
}
private static final VMerge create(String val) {
VMerge vm = new VMerge();
vm.setVal(val);
return vm;
}
}
@Test
public void doTest() throws Docx4JException {
WordprocessingMLPackage pkg = WordprocessingMLPackage.createPackage();
MainDocumentPart main = pkg.getMainDocumentPart();
Document doc = main.getContents();
Body body = doc.getBody();
//创建表格
Tbl table = factory.createTbl();
//第1行
Tr one = factory.createTr();
//第1列纵向合并
Tc caption = this.createTc("Caption");
TcPr captcpr = factory.createTcPr();
captcpr.setVMerge(VMergeEnum.RESTART);
caption.setTcPr(captcpr);
one.getContent().add(caption);
//第2、3、4列
one.getContent().add(this.createTc("The quick brown fox jumps over the lazy dog"));
one.getContent().add(this.createTc("X"));
one.getContent().add(this.createTc("Y"));
table.getContent().add(one);
//第2行
Tr two = factory.createTr();
//第1列纵向合并,Hidden不显示,不过在word里面拆分开以后可以看到。
Tc hid = this.createTc("Hidden");
TcPr hidtcpr = factory.createTcPr();
hidtcpr.setVMerge(VMergeEnum.CONTINUE);
hid.setTcPr(hidtcpr);
two.getContent().add(hid);
//第2、3列横向合并
Tc tc = this.createTc("The quick brown fox jumps over the lazy dog");
TcPr rowspan = factory.createTcPr();
GridSpan gs = new GridSpan();
gs.setVal(BigInteger.valueOf(2));
tc.setTcPr(rowspan);
rowspan.setGridSpan(gs);
two.getContent().add(tc);
//第4列
two.getContent().add(this.createTc("Z"));
table.getContent().add(two);
table.setTblPr(this.createTblPr());
body.getContent().add(table);
pkg.save(new File(System.getProperty("user.dir"), "Full Table Sample.docx"));
}
protected Tc createTc(String text) {
Tc tc = factory.createTc();
P p = factory.createP();
R r = factory.createR();
Text t = factory.createText();
t.setValue(text);
r.getContent().add(t);
p.getContent().add(r);
tc.getContent().add(p);
return tc;
}
protected TblPr createTblPr() {
TblPr tblpr = factory.createTblPr();
Jc jc = factory.createJc();
jc.setVal(JcEnumeration.CENTER);
tblpr.setJc(jc);
//表格样式:使用的是最普通的细线样式。
TblStyle s = new TblStyle();
s.setVal("TableGrid");
tblpr.setTblStyle(s);
//表格样式:设置边框样式
//此处表格宽度单位使用默认单位,为1/8个点,最小为2,最大为96
TblBorders bdrs = factory.createTblBorders();
CTBorder out = new CTBorder();
out.setSpace(BigInteger.ZERO);
out.setSz(BigInteger.valueOf(16));
out.setColor("000000");
out.setVal(STBorder.SINGLE);
bdrs.setLeft(out);
bdrs.setRight(out);
bdrs.setTop(out);
bdrs.setBottom(out);
CTBorder inh = new CTBorder();
inh.setSpace(BigInteger.ZERO);
inh.setSz(BigInteger.valueOf(8));
inh.setColor("000000");
inh.setVal(STBorder.SINGLE);
bdrs.setInsideH(inh);
CTBorder inv = new CTBorder();
inv.setSpace(BigInteger.ZERO);
inv.setSz(BigInteger.valueOf(8));
inv.setColor("333333");
inv.setVal(STBorder.DASHED);
bdrs.setInsideV(inv);
tblpr.setTblBorders(bdrs);
//表格宽度:使用的是pct单位,也就是50分之一个百分点。2500就是50%。
TblWidth w = new TblWidth();
w.setType("pct");
w.setW(BigInteger.valueOf(2500));
tblpr.setTblW(w);
//如果下面三条被注释掉,列宽会变为默认的自动调整。
CTTblLayoutType layout = new CTTblLayoutType();
layout.setType(STTblLayoutType.FIXED /*STTblLayoutType.AUTOFIT*/);
tblpr.setTblLayout(layout);
return tblpr;
}
}
实际上TblFactory也是差不多实现的,只是一开始给设置好了一些内容而已。