poi操作Word合并单元格

2 篇文章 0 订阅

对于合并单元格的介绍不怎么多,下面是之前做word导出的时候研究的,在stackoverflow查到了点资料。

记录下两个关键方法:

[java]  view plain  copy
  1. // word跨列合并单元格  
  2.     public  void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {    
  3.         for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {    
  4.             XWPFTableCell cell = table.getRow(row).getCell(cellIndex);    
  5.             if ( cellIndex == fromCell ) {    
  6.                 // The first merged cell is set with RESTART merge value    
  7.                 cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);    
  8.             } else {    
  9.                 // Cells which join (merge) the first one, are set with CONTINUE    
  10.                 cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);    
  11.             }    
  12.         }    
  13.     }    
  14.     // word跨行并单元格  
  15.     public void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {    
  16.         for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {    
  17.             XWPFTableCell cell = table.getRow(rowIndex).getCell(col);    
  18.             if ( rowIndex == fromRow ) {    
  19.                 // The first merged cell is set with RESTART merge value    
  20.                 cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);    
  21.             } else {    
  22.                 // Cells which join (merge) the first one, are set with CONTINUE    
  23.                 cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);    
  24.             }    
  25.         }    
  26.     }   

另外加上单元格字体设置的方法:

[html]  view plain  copy
  1. private void getParagraph(XWPFTableCell cell,String cellText){  
  2.         CTP ctp = CTP.Factory.newInstance();  
  3.         XWPFParagraph p = new XWPFParagraph(ctp, cell);  
  4.         p.setAlignment(ParagraphAlignment.CENTER);  
  5.         XWPFRun run = p.createRun();  
  6.         run.setText(cellText);  
  7.         CTRPr rpr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : run.getCTR().addNewRPr();  
  8.         CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();  
  9.         fonts.setAscii("仿宋");  
  10.         fonts.setEastAsia("仿宋");  
  11.         fonts.setHAnsi("仿宋");  
  12.         cell.setParagraph(p);  
  13.     }  
转载自: POI操作word合并单元格

另:(设值)

    private static void fillTable(XWPFTable table) {
        for (int rowIndex = 0; rowIndex < table.getNumberOfRows(); rowIndex++) {
            XWPFTableRow row = table.getRow(rowIndex);

            for (int colIndex = 0; colIndex < row.getTableCells().size(); colIndex++) {
                XWPFTableCell cell = row.getCell(colIndex);
                cell.addParagraph().createRun().setText(" cell " + rowIndex + colIndex + " ");
            }
        }
    }

相关参考:[简单]poi word2007表格单元格合并

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值