問題如下:
所有單元格的樣式都是帶邊框的,但是合併之後,圖中的紅框標識區域樣式並沒有
樣式源碼如下:
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);
}
}
}
}
}
結果: