`
chenyunhong
  • 浏览: 136362 次
  • 性别: Icon_minigender_1
  • 来自: 真的不知道
社区版块
存档分类
最新评论

关于POI合并单元格后加边框问题,请大家拍砖

    博客分类:
  • Java
阅读更多

最近由于项目需要用到了POI来生成Excel表格,遇到单元格合并以后怎么加边框,老是解决不了,特意上来请教一下
在网上找了一堆,都不行。
自己写了一个笨方法,可以实现列合并加边框,但是行合并就不行了,方法如下:

/**
	 * 合并单元格加边框  水平
	 * @param sheet
	 * @param region
	 * @param cs
	 */
	public static void setCellBorder(int start, int end, HSSFRow row,    HSSFCellStyle style) {
    	for(int i=start;i<=end;i++){   
		    HSSFCell cell = row.createCell(i);   
		    cell.setCellValue("");   
		    cell.setCellStyle(style);   
		}
	}

 参数说明:start和并的第二列,end为合并的最后一列,row就为当前行,style样式(里面有设置边框)

例如从0-10列合并: ExcelUtil.setCellBorder(2,10,row,style);   这样可以设置

 

关于合并行设置也写了一个,但是不行,方法如下:

/**
	 * 合并单元格加边框  垂直
	 * @param sheet
	 * @param region
	 * @param cs
	 */
	public static void setCellBorder(int start, int end, int col, HSSFSheet sheet, HSSFCellStyle style) {
    	for(int i=start;i<=end;i++){   
    		HSSFRow row  = ExcelUtil.createRow(sheet, start, (short)0);
		    HSSFCell cell = row.createCell(col);   
		    cell.setCellValue("");   
		    cell.setCellStyle(style);   
		}
	}

  参数说明:start和并的第二行,end为合并的最后一行,col为哪一列,sheet就是当前表格对象,style样式(里面有设置边框)

 

  下面是调用代码:

//第二行 制表时间
		HSSFRow rowCreateTime = ExcelUtil.createRow(sheet, 1, (short)400);
		String time = new SimpleDateFormat("yyyy年MM月dd日").format(new Date());
		
		ExcelUtil.createCell(sheet, rowCreateTime, 0, styleTime, 
				new Region(1,(short)0,1,(short)27), "制表时间:"+time,null);
		//合并后单元格设置边框
		ExcelUtil.setCellBorder(1, 27, rowCreateTime, styleTime);
		
		//第三行 目录
		HSSFRow rowMenu  = ExcelUtil.createRow(sheet, 2, (short)450);
		HSSFRow rowMenu1 = ExcelUtil.createRow(sheet, 3, (short)450);
		//单位
		ExcelUtil.createCell(sheet, rowMenu, 0, style,
				new Region(2,(short)0,3,(short)0),"单位",4000);
		ExcelUtil.setCellBorder(3, 3, 0, sheet, style);
		
		//设备型号
		ExcelUtil.createCell(sheet, rowMenu, 1, style,
				new Region(2,(short)1,3,(short)1),"设备型号",2500);
		ExcelUtil.setCellBorder(3, 3, 1, sheet, style);
		
		//全年1-12月维修情况
		ExcelUtil.createCell(sheet, rowMenu, 2, style,
				new Region(2,(short)2,2,(short)13),"全年1-12月维修情况",null);
		//合计
		ExcelUtil.createCell(sheet, rowMenu, 14, style,
				new Region(2,(short)14,3,(short)14),"合计",1200);
		ExcelUtil.setCellBorder(3, 3, 14, sheet, style);
		
		//全年1-12月维护情况
		ExcelUtil.createCell(sheet, rowMenu, 15, style,
				new Region(2,(short)15,2,(short)26),"全年1-12月维修情况",null);
		//合计
		ExcelUtil.createCell(sheet, rowMenu, 27, style,
				new Region(2,(short)27,3,(short)27),"合计",1200);
		ExcelUtil.setCellBorder(3, 3, 27, sheet, style);
 

 

请高手指教一下,有什么好的方法么?

 

  • 大小: 111.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics