use of org.docx4j.wml.Tbl in project docx4j-template by vindell.
the class Docx4j_工具类_S3_Test method getAllTbl.
/**
* @Description:得到所有表格
*/
public List<Tbl> getAllTbl(WordprocessingMLPackage wordMLPackage) {
MainDocumentPart mainDocPart = wordMLPackage.getMainDocumentPart();
List<Object> objList = getAllElementFromObject(mainDocPart, Tbl.class);
if (objList == null) {
return null;
}
List<Tbl> tblList = new ArrayList<Tbl>();
for (Object obj : objList) {
if (obj instanceof Tbl) {
Tbl tbl = (Tbl) obj;
tblList.add(tbl);
}
}
return tblList;
}
use of org.docx4j.wml.Tbl in project docx4j-template by vindell.
the class Docx4j_工具类_S3_Test method createTable.
/**
* @Description:创建表格(默认水平居中,垂直居中)
*/
public Tbl createTable(int rowNum, int colsNum, int[] widthArr) throws Exception {
colsNum = Math.max(1, Math.min(colsNum, widthArr.length));
rowNum = Math.max(1, rowNum);
Tbl tbl = new Tbl();
StringBuffer tblSb = new StringBuffer();
tblSb.append("<w:tblPr ").append(Namespaces.W_NAMESPACE_DECLARATION).append(">");
tblSb.append("<w:tblStyle w:val=\"TableGrid\"/>");
tblSb.append("<w:tblW w:w=\"0\" w:type=\"auto\"/>");
// 上边框
tblSb.append("<w:tblBorders>");
tblSb.append("<w:top w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
// 左边框
tblSb.append("<w:left w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
// 下边框
tblSb.append("<w:bottom w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
// 右边框
tblSb.append("<w:right w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
tblSb.append("<w:insideH w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
tblSb.append("<w:insideV w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
tblSb.append("</w:tblBorders>");
tblSb.append("</w:tblPr>");
TblPr tblPr = null;
tblPr = (TblPr) XmlUtils.unmarshalString(tblSb.toString());
Jc jc = new Jc();
// 单元格居中对齐
jc.setVal(JcEnumeration.CENTER);
tblPr.setJc(jc);
tbl.setTblPr(tblPr);
// 设定各单元格宽度
TblGrid tblGrid = new TblGrid();
tbl.setTblGrid(tblGrid);
for (int i = 0; i < colsNum; i++) {
TblGridCol gridCol = new TblGridCol();
gridCol.setW(BigInteger.valueOf(widthArr[i]));
tblGrid.getGridCol().add(gridCol);
}
// 新增行
for (int j = 0; j < rowNum; j++) {
Tr tr = new Tr();
tbl.getContent().add(tr);
// 列
for (int i = 0; i < colsNum; i++) {
Tc tc = new Tc();
tr.getContent().add(tc);
TcPr tcPr = new TcPr();
TblWidth cellWidth = new TblWidth();
cellWidth.setType("dxa");
cellWidth.setW(BigInteger.valueOf(widthArr[i]));
tcPr.setTcW(cellWidth);
tc.setTcPr(tcPr);
// 垂直居中
setTcVAlign(tc, STVerticalJc.CENTER);
P p = new P();
PPr pPr = new PPr();
pPr.setJc(jc);
p.setPPr(pPr);
R run = new R();
p.getContent().add(run);
tc.getContent().add(p);
}
}
return tbl;
}
use of org.docx4j.wml.Tbl in project docx4j-template by vindell.
the class WMLPackageUtils method replaceTable.
public static void replaceTable(String[] placeholders, List<Map<String, String>> textToAdd, WordprocessingMLPackage template) throws Docx4JException, JAXBException {
List<Tbl> tables = getTargetElements(template.getMainDocumentPart(), Tbl.class);
// 1. find the table
Tbl tempTable = getTable(tables, placeholders[0]);
List<Tr> rows = getTargetElements(tempTable, Tr.class);
// first row is header, second row is content
if (rows.size() == 2) {
// this is our template row
Tr templateRow = (Tr) rows.get(1);
for (Map<String, String> replacements : textToAdd) {
// 2 and 3 are done in this method
addRowToTable(tempTable, templateRow, replacements);
}
// 4. remove the template row
tempTable.getContent().remove(templateRow);
}
}
use of org.docx4j.wml.Tbl in project docx4j-template by vindell.
the class DocxElementWmlRender method newTable.
public Tbl newTable(int row, int cell) {
// 创建表格对象
Tbl table = factory.createTbl();
for (int i = 0; i < row; i++) {
Tr tableRow = factory.createTr();
for (int j = 0; j < cell; j++) {
newCell(tableRow, "");
}
table.getContent().add(tableRow);
}
return table;
}
use of org.docx4j.wml.Tbl in project docx4j-template by vindell.
the class SettingColumnWidthForTable method main.
/**
* 创建一个带边框的表格并添加一行. 然后添加两个带内容的单元格并给定宽度.
*/
public static void main(String[] args) throws Docx4JException {
wordMLPackage = WordprocessingMLPackage.createPackage();
factory = Context.getWmlObjectFactory();
Tbl table = factory.createTbl();
addBorders(table);
Tr tr = factory.createTr();
addTableCellWithWidth(tr, "Field 1", 2500);
addTableCellWithWidth(tr, "Field 2", 0);
table.getContent().add(tr);
wordMLPackage.getMainDocumentPart().addObject(table);
wordMLPackage.save(new java.io.File("src/main/HelloWord133.docx"));
}
Aggregations