by ghossio » Tue Sep 06, 2011 3:20 am
I'm trying to create a table with docxj4. i created the table but without borders. I'm looking for but dont find anything. Any help will be grateful. Thanks for the attention and a salute. This is the code
- Code: Select all
public static Tbl createTable(int rows, int cols, int cellWidthTwips) {
log.info("Creando la tabla |||||||||||");
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 (Exception 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();
tr.getEGContentCellContent().add(tc);
TcPr tcPr = Context.getWmlObjectFactory().createTcPr();
tc.setTcPr(tcPr);
TblWidth cellWidth = Context.getWmlObjectFactory().createTblWidth();
tcPr.setTcW(cellWidth);
cellWidth.setType("dxa");
cellWidth.setW(BigInteger.valueOf(cellWidthTwips));
org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
org.docx4j.wml.P p = factory.createP();
org.docx4j.wml.Text t = factory.createText();
t.setValue("hi: " + i + ", " + j);
org.docx4j.wml.R run = factory.createR();
run.getRunContent().add(t);
p.getParagraphContent().add(run);
tc.getEGBlockLevelElts().add(p);
}
}
log.info("tabla creada|||||||||||");
return tbl;
}