关于POI合并单元格后加边框问题

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


Java代码
/**
* 合并单元格加边框 水平
* @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);
}
}

/**
* 合并单元格加边框 水平
* @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); 这样可以设置

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


Java代码
/**
* 合并单元格加边框 垂直
* @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);
}
}

/**
* 合并单元格加边框 垂直
* @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样式(里面有设置边框)

下面是调用代码:
Java代码


Java代码
//第二行 制表时间
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);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值