最近由于项目需要用到了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 查看图片附件
如果觉得《关于POI合并单元格后加边框问题 请大家拍砖》对你有帮助,请点赞、收藏,并留下你的观点哦!