poi导出word表格的问题



poi导出word表格的问题,不能直接用,但是很有参考价值,对poi操作word 表格的一般操作能够实现


poi导出word表格jar包

poi-3.9-20121203.jar

poi-ooxml-3.9-20121203.jar

poi-ooxml-schemas-3.9-20121203.jar

poi-scratchpad-3.9.jar

xbean.jar


 List CONDITIONlist = new ArrayList();

CONDITIONlist .add("fdsajfdsfjdsfjldjsfds");

CONDITIONlist .add("sjfkljdslkfdsfdsafdsf");

 XWPFTable table = doc.createTable(16, 4);

//设置表格宽度
   CTTbl ttbl = table.getCTTbl();  
       CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();  
       CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();  
       CTJc cTJc=tblPr.addNewJc();  
       cTJc.setVal(STJc.Enum.forString("center"));  
       tblWidth.setW(new BigInteger("8000"));  
       tblWidth.setType(STTblWidth.DXA); 


       
       List rows = table.getRows(); 
     XWPFTableRow row;  
     List cells;  
     XWPFTableCell cell;  
     int rowSize = rows.size();  
     int cellSize;  
     for (int i=0; i     row = rows.get(i);
        //设置行的高度  
        row.setHeight(500);  //设置行高
        cells = row.getTableCells();  //获得单元格
     cellSize = cells.size();  

//分列设置单元格宽度或者说列宽
        for (int j=0; j           cell = cells.get(j); 
          CTTcPr cellPr = cell.getCTTc().addNewTcPr(); 
     CTTblWidth cellw = cellPr.addNewTcW();
     cellw.setType(STTblWidth.DXA);
     if (j == 3||j==1) {  
               //设置宽度  
               cellPr.addNewTcW().setW(BigInteger.valueOf(2700));  
           }else cellPr.addNewTcW().setW(BigInteger.valueOf(1300));
        }

        }
     
//   setTableGridCol(table,a);
   // 设置上下左右四个方向的距离,可以将表格撑大
   /*table.setCellMargins(20, 20, 20, 20);
   table.setWidth(500);
   table.setRowBandSize(50);
   table.setColBandSize(30);*/
   List tableCells = table.getRow(0).getTableCells();
   tableCells.get(0).setText("事项编码");
   tableCells.get(1).setText(""fdsa);
  
   List tableCellsq = table.getRow(1).getTableCells();
   tableCellsq.get(0).setText("实施主体");
   tableCellsq.get(1).setText("fdsfdsa");
   tableCellsq.get(2).setText("承办机构");
   tableCellsq.get(3).setText("fdsafdsafdsa");
   
   List tableCellsq3 = table.getRow(2).getTableCells();
   tableCellsq3.get(0).setText("内部事项编码");
   tableCellsq3.get(1).setText("fdsafdsa");
   tableCellsq3.get(2).setText("事项名称");
   tableCellsq3.get(3).setText("fdsafdsa");
   
   List tableCellsq4 = table.getRow(3).getTableCells();
   tableCellsq4.get(0).setText("服务对象");
   tableCellsq4.get(1).setText"(SUB_TYPE");
   tableCellsq4.get(2).setText("服务主题");
   tableCellsq4.get(3).setText("TITLE_NAME");
   tableCells.get(2).setText("服务分类");
   tableCells.get(3).setText("SUBTYPE");
   
   List tableCellsq5 = table.getRow(4).getTableCells();
   tableCellsq5.get(0).setText("法定期限");
   tableCellsq5.get(1).setText("String.valueOf(LAW_TIME)");
   tableCellsq5.get(2).setText("承诺期限");
   tableCellsq5.get(3).setText("String.valueOf(AGREE_TIME)");
   
   List tableCellsq6 = table.getRow(5).getTableCells();
   tableCellsq6.get(0).setText("是否收费");
   tableCellsq6.get(1).setText(IS_CHARGE);
   tableCellsq6.get(2).setText("办件类型");
   tableCellsq6.get(3).setText(ASSORT);
   
   List tableCellsq7 = table.getRow(6).getTableCells();
   tableCellsq7.get(0).setText("是否可网上办理");
   tableCellsq7.get(1).setText("IS_ONLINE");
   tableCellsq7.get(2).setText("时都进驻大厅");
   tableCellsq7.get(3).setText("IS_IN_HALL");
   
   List tableCellsq8 = table.getRow(7).getTableCells();
   tableCellsq8.get(0).setText("备注信息");
   tableCellsq8.get(1).setText("REMARK");
   mergeCellsHorizontal( table, 7, 1,3);
   
   List tableCellsq9 = table.getRow(8).getTableCells();
   tableCellsq9.get(0).setText("受理条件");
   //tableCellsq9.get(1).setText("CONDITION");

//单元格内换行
   XWPFParagraph para =tableCellsq9.get(1).getParagraphs().get(0);
   para.setAlignment(ParagraphAlignment.LEFT);
   for(String text : CONDITIONlist){
       XWPFRun run = para.createRun();
       run.setText(text);
       run.addBreak();
   }

    //合并单元格
   mergeCellsHorizontal( table, 8, 1,3);


/**
* @Description: 跨列合并
*/
public void mergeCellsHorizontal(XWPFTable table, int row, int fromCell,
int toCell) {
for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
if (cellIndex == fromCell) {
// The first merged cell is set with RESTART merge value
getCellCTTcPr(cell).addNewHMerge().setVal(STMerge.RESTART);
//getCellCTTcPr(cell).addNewHMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one,are set with CONTINUE
getCellCTTcPr(cell).addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}


 
   


你可能感兴趣的:(poi导出word表格的问题)