Example #1
0
 private static void setCellText(
     XWPFDocument xDocument, XWPFTableCell cell, String text, String bgcolor, int width) {
   CTTcPr cellPr = cell.getCTTc().addNewTcPr();
   cellPr.addNewTcW().setW(BigInteger.valueOf(width));
   cell.setColor(bgcolor);
   cell.setVerticalAlignment(XWPFVertAlign.CENTER);
   cell.setText(text);
 }
 /**
  * * 写一个表格
  *
  * @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);
 }
Example #3
0
 private static void spanCellsAcrossRow(XWPFTable table, int rowNum, int colNum, int span)
     throws Exception {
   try {
     XWPFTableCell cell = table.getRow(rowNum).getCell(colNum);
     if (cell.getCTTc().getTcPr() != null) {
       cell.getCTTc().getTcPr().addNewGridSpan();
       if (cell.getCTTc().getTcPr().getGridSpan() == null) {
         cell.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf((long) span));
       }
     }
   } catch (Exception e) {
     throw e;
   }
 }
Example #4
0
 /**
  * 判断是不是迭代输出
  *
  * @author Zerrion
  * @date 2013-11-18
  * @return
  * @throws Exception
  */
 private Object checkThisTableIsNeedIterator(XWPFTableCell cell, Map<String, Object> map)
     throws Exception {
   String text = cell.getText().trim();
   // 判断是不是迭代输出
   if (text.startsWith("{{") && text.endsWith("}}") && text.indexOf("in ") != -1) {
     return PoiPublicUtil.getRealValue(text.replace("in ", "").trim(), map);
   }
   return null;
 }
Example #5
0
 public static TableWidth getTableWidth(XWPFTableCell cell) {
   float width = 0;
   boolean percentUnit = false;
   CTTcPr tblPr = cell.getCTTc().getTcPr();
   if (tblPr.isSetTcW()) {
     CTTblWidth tblWidth = tblPr.getTcW();
     return getTableWidth(tblWidth);
   }
   return new TableWidth(width, percentUnit);
 }
Example #6
0
 public static CTTblWidth getWidth(XWPFTableCell cell) {
   return cell.getCTTc().getTcPr().getTcW();
 }
Example #7
0
  public static CTDecimalNumber getGridSpan(XWPFTableCell cell) {
    if (cell.getCTTc().getTcPr() != null) return cell.getCTTc().getTcPr().getGridSpan();

    return null;
  }
Example #8
0
 private void parseThisRow(List<XWPFTableCell> cells, Map<String, Object> map) throws Exception {
   for (XWPFTableCell cell : cells) {
     parseAllParagraphic(cell.getParagraphs(), map);
   }
 }