可以合并的,不过要逐个给每一个被合并的单元格加上边框,这样就ok

 

有木有其他办法  有木有 ?

 

 

poi 文档 FAQ有说明:



12. How do I add a border around a merged cell?


Add blank cells around where the cells normally would have been and set the borders individually for each cell. We will probably enhance HSSF in the future to make this process easier.

 

 

 

private static void setRegionBorder(int border, CellRangeAddress region, Sheet sheet,Workbook wb){
		CellStyle cs=wb.createCellStyle();
		cs.setBorderBottom((short) border);
		cs.setBorderTop((short) border);
		cs.setBorderLeft((short) border);
		cs.setBorderRight((short) border);
		setRegionStyle( cs, region, sheet);
		}
  
	private static void setRegionStyle(CellStyle cs, CellRangeAddress region, Sheet sheet){
	   
	    for(int i=region.getFirstRow();i<=region.getLastRow();i++){
	    	
	    	Row row=sheet.getRow(i);
	    	if(row==null) row=sheet.createRow(i);
	    	for(int j=region.getFirstColumn();j<=region.getLastColumn();j++){
	    		Cell cell=row.getCell(j);
	    		if( cell==null){
	    			cell=row.createCell(j);
	    			cell.setCellValue("");
	    		}
	    		 cell.setCellStyle(cs);
	    	}
	    }
	}

 

注意1:如果合并在前,则在后面不能直接用

  

Row row=sheet.createRow(0);
         而应该改为
       Row row=sheet.getRow(0);
       if(row==null){
            row2=sheet.createRow(2);
       }

          否则直接createRow会覆盖先前合并时定义的边框样式。

 

注意2:可以用poi自带的工具类来处理合并后的边框

         

 

private static void setRegionBorder(int border, CellRangeAddress region, Sheet sheet,Workbook wb){
		RegionUtil.setBorderBottom(border,region, sheet, wb);
		RegionUtil.setBorderLeft(border,region, sheet, wb);
		RegionUtil.setBorderRight(border,region, sheet, wb);
		RegionUtil.setBorderTop(border,region, sheet, wb);
	
	}