public class TblFactory {
public static Tbl createTable(int rows, int cols, int cellWidthTwips) {
Tbl tbl = Context.getWmlObjectFactory().createTbl();
// w:tblPr
String strTblPr = "<w:tblPr " + Namespaces.W_NAMESPACE_DECLARATION + ">"
+ "<w:tblStyle w:val=\"TableGrid\"/>"
+ "<w:tblW w:w=\"0\" w:type=\"auto\"/>"
+ "<w:tblLook w:val=\"04A0\"/>"
+ "</w:tblPr>";
TblPr tblPr = null;
try {
tblPr = (TblPr)XmlUtils.unmarshalString(strTblPr);
} catch (JAXBException e) {
// Shouldn't happen
e.printStackTrace();
}
tbl.setTblPr(tblPr);
// <w:tblGrid><w:gridCol w:w="4788"/>
TblGrid tblGrid = Context.getWmlObjectFactory().createTblGrid();
tbl.setTblGrid(tblGrid);
// Add required <w:gridCol w:w="4788"/>
for (int i=1 ; i<=cols; i++) {
TblGridCol gridCol = Context.getWmlObjectFactory().createTblGridCol();
gridCol.setW(BigInteger.valueOf(cellWidthTwips));
tblGrid.getGridCol().add(gridCol);
}
// Now the rows
for (int j=1 ; j<=rows; j++) {
Tr tr = Context.getWmlObjectFactory().createTr();
tbl.getEGContentRowContent().add(tr);
// The cells
for (int i=1 ; i<=cols; i++) {
Tc tc = Context.getWmlObjectFactory().createTc();