问题如下:
所有单元格的样式都是带边框的,但是合并之后,图中的红框标识区域样式并没有
样式源码如下:
public HSSFCellStyle getBorderCellStyle(HSSFWorkbook hWorkbook) {
HSSFCellStyle cellStyle = hWorkbook.createCellStyle();
//加边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
return cellStyle;
}
解决思路:
合并之后根据合并的单元格坐标再重新设置样式即可
源码:
添加表头之后,重新设置样式,更新行数和列数
private void setCellStyle(int rowIndex, int columnIndex, int toRowIndex, int toColumnIndex, HSSFCellStyle cellStyle) {
for (int i = rowIndex; i <= toRowIndex; i++) {
for (int j = columnIndex; j < (toColumnIndex + columnIndex); j++) {
HSSFRow row = hSheet.getRow(i);
HSSFCell cell = null;
if (null != row) {
cell = row.getCell(j);
if (null == cell) {
cell = row.createCell(j);
cell.setCellStyle(cellStyle);
}
}
}
}
}
结果: