public static CTShortHexNumber getTblLook(XWPFTable table) { CTTblPr tblPr = getTblPr(table); if (tblPr != null) { return tblPr.getTblLook(); } return null; }
public static CTTblCellMar getTblCellMar(XWPFTable table) { CTTblPr tblPr = getTblPr(table); if (tblPr != null) { return tblPr.getTblCellMar(); } return null; }
public static CTTblBorders getTblBorders(XWPFTable table) { CTTblPr tblPr = getTblPr(table); if (tblPr != null) { return tblPr.getTblBorders(); } return null; }
/** * Returns table width of teh XWPF table. * * @param table * @return */ public static TableWidth getTableWidth(XWPFTable table) { float width = 0; boolean percentUnit = false; CTTblPr tblPr = table.getCTTbl().getTblPr(); if (tblPr.isSetTblW()) { CTTblWidth tblWidth = tblPr.getTblW(); return getTableWidth(tblWidth); } return new TableWidth(width, percentUnit); }
/** * * 写一个表格 * * @throws Exception */ public void testWriteTable() throws Exception { XWPFDocument doc = new XWPFDocument(); // 创建一个5行5列的表格 XWPFTable table = doc.createTable(5, 5); // 这里增加的列原本初始化创建的那5行在通过getTableCells()方法获取时获取不到,但通过row新增的就可以。 // table.addNewCol(); //给表格增加一列,变成6列 table.createRow(); // 给表格新增一行,变成6行 List<XWPFTableRow> rows = table.getRows(); // 表格属性 CTTblPr tablePr = table.getCTTbl().addNewTblPr(); // 表格宽度 CTTblWidth width = tablePr.addNewTblW(); width.setW(BigInteger.valueOf(8000)); XWPFTableRow row; List<XWPFTableCell> cells; XWPFTableCell cell; int rowSize = rows.size(); int cellSize; for (int i = 0; i < rowSize; i++) { row = rows.get(i); // 新增单元格 row.addNewTableCell(); // 设置行的高度 row.setHeight(500); // 行属性 // CTTrPr rowPr = row.getCtRow().addNewTrPr(); // 这种方式是可以获取到新增的cell的。 // List<CTTc> list = row.getCtRow().getTcList(); cells = row.getTableCells(); cellSize = cells.size(); for (int j = 0; j < cellSize; j++) { cell = cells.get(j); if ((i + j) % 2 == 0) { // 设置单元格的颜色 cell.setColor("ff0000"); // 红色 } else { cell.setColor("0000ff"); // 蓝色 } // 单元格属性 CTTcPr cellPr = cell.getCTTc().addNewTcPr(); cellPr.addNewVAlign().setVal(STVerticalJc.CENTER); if (j == 3) { // 设置宽度 cellPr.addNewTcW().setW(BigInteger.valueOf(3000)); } cell.setText(i + ", " + j); } } // 文件不存在时会自动创建 OutputStream os = new FileOutputStream(SystemUtil.getSystemPath("testw111withtable.docx")); // 写入文件 doc.write(os); this.close(os); }
/** * 生成word,表格 * * @param data * @throws Exception */ public static void productWordForm( Map<String, String> tableinfo, Map<String, LinkedHashMap<String, LinkedHashMap<String, String>>> data, Parameters parameters) throws Exception { XWPFDocument xDocument = new XWPFDocument(); Iterator<String> tableNameIter = data.keySet().iterator(); while (tableNameIter.hasNext()) { String table_name = tableNameIter.next(); XWPFParagraph xp = xDocument.createParagraph(); XWPFRun r1 = xp.createRun(); r1.setText(table_name + " " + tableinfo.get(table_name)); r1.setFontSize(18); r1.setTextPosition(10); XWPFParagraph p = xDocument.createParagraph(); p.setAlignment(ParagraphAlignment.CENTER); p.setWordWrap(true); LinkedHashMap<String, LinkedHashMap<String, String>> columns = data.get(table_name); int rows = columns.size(); XWPFTable xTable = xDocument.createTable(rows + 1, 7); // 表格属性 CTTblPr tablePr = xTable.getCTTbl().addNewTblPr(); // 表格宽度 CTTblWidth width = tablePr.addNewTblW(); width.setW(BigInteger.valueOf(8600)); int i = 0; xTable.getRow(i).setHeight(380); setCellText(xDocument, xTable.getRow(i).getCell(0), "代码", "CCCCCC", getCellWidth(0)); setCellText(xDocument, xTable.getRow(i).getCell(1), "注释", "CCCCCC", getCellWidth(1)); setCellText(xDocument, xTable.getRow(i).getCell(2), "类型", "CCCCCC", getCellWidth(2)); setCellText(xDocument, xTable.getRow(i).getCell(3), "默认值", "CCCCCC", getCellWidth(3)); setCellText(xDocument, xTable.getRow(i).getCell(4), "标识", "CCCCCC", getCellWidth(4)); setCellText(xDocument, xTable.getRow(i).getCell(5), "主键", "CCCCCC", getCellWidth(5)); setCellText(xDocument, xTable.getRow(i).getCell(6), "空值", "CCCCCC", getCellWidth(6)); i = i + 1; // 下一行 int j = 0; // 列column索引 Map<String, LinkedHashMap<String, String>> keyColumnMap = keyColumns(columns); for (Iterator<String> columnNameIter = keyColumnMap.keySet().iterator(); columnNameIter.hasNext(); ) { String column_name = columnNameIter.next(); LinkedHashMap<String, String> columnsAtt = keyColumnMap.get(column_name); int cwidth = getCellWidth(j); setCellText(xDocument, xTable.getRow(i).getCell(j), column_name, "FFFFFF", cwidth); ++j; Iterator<String> columnTypeIter = columnsAtt.keySet().iterator(); while (columnTypeIter.hasNext()) { String colum_type = columnTypeIter.next(); cwidth = getCellWidth(j); setCellText( xDocument, xTable.getRow(i).getCell(j), columnsAtt.get(colum_type), "FFFFFF", cwidth); j++; } ++i; // 下一行 j = 0; // 恢复第一列 } Iterator<String> cloumnsNameIter = columns.keySet().iterator(); while (cloumnsNameIter.hasNext()) { xTable.getRow(i).setHeight(380); String colum_name = cloumnsNameIter.next(); LinkedHashMap<String, String> columnsAtt = columns.get(colum_name); int cwidth = getCellWidth(j); if (xTable.getRow(i) == null) continue; setCellText(xDocument, xTable.getRow(i).getCell(j), colum_name, "FFFFFF", cwidth); j++; Iterator<String> columnTypeIter = columnsAtt.keySet().iterator(); while (columnTypeIter.hasNext()) { String colum_type = columnTypeIter.next(); cwidth = getCellWidth(j); setCellText( xDocument, xTable.getRow(i).getCell(j), columnsAtt.get(colum_type), "FFFFFF", cwidth); j++; } j = 0; // 恢复第一列 ++i; // 下一行 } XWPFTableRow row = xTable.insertNewTableRow(0); row.setHeight(380); row.addNewTableCell(); CTTcPr cellPr = row.getCell(0).getCTTc().addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(1600)); row.getCell(0).setColor("CCCCCC"); row.getCell(0).setText("中文名称"); row.addNewTableCell(); cellPr = row.getCell(0).getCTTc().addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(3000)); row.getCell(1).setColor("FFFFFF"); row.getCell(1).setText(tableinfo.get(table_name)); row.addNewTableCell(); cellPr = row.getCell(0).getCTTc().addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(1200)); row.getCell(2).setColor("CCCCCC"); row.getCell(2).setText("英文名称"); row.addNewTableCell(); CTTc cttc = row.getCell(3).getCTTc(); CTTcPr ctPr = cttc.addNewTcPr(); cellPr = row.getCell(0).getCTTc().addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(2800)); ctPr.addNewGridSpan().setVal(BigInteger.valueOf(4)); ctPr.addNewHMerge().setVal(STMerge.CONTINUE); cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER); cttc.getPList().get(0).addNewR().addNewT().setStringValue(table_name); row = xTable.insertNewTableRow(1); row.setHeight(380); row.addNewTableCell(); cellPr = row.getCell(0).getCTTc().addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(1600)); row.getCell(0).setColor("CCCCCC"); row.getCell(0).setText("功能描述"); row.addNewTableCell(); cellPr = row.getCell(0).getCTTc().addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(7000)); cttc = row.getCell(1).getCTTc(); ctPr = cttc.addNewTcPr(); ctPr.addNewGridSpan().setVal(BigInteger.valueOf(6)); ctPr.addNewHMerge().setVal(STMerge.CONTINUE); cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.LEFT); cttc.getPList().get(0).addNewR().addNewT().setStringValue(""); } FileOutputStream fos = new FileOutputStream(parameters.getPath() + parameters.getDatabase() + "_doc.docx"); xDocument.write(fos); fos.close(); }