系列文章目录
一、Java使用Apache POI导出excel
二、Apache POI 操作Excel常用方法
三、Apache poi 拆分单元格并赋值
四、使用easypoi模板方法导出excel
五、Apache poi给excel单元格添加下拉框或数据验证
一、效果展示
拆分前
拆分后
二、代码
for (int i = sheet.getNumMergedRegions() -1 ; i >= 0; i--) {
// 合并单元格位置
CellRangeAddress range = sheet.getMergedRegion(i);
// 拆分3行与4行的单元格
if (range.getFirstRow() >= 2 && range.getLastRow() <= 3) {
// 拆分单元格
sheet.removeMergedRegion(i);
int firstColumn = range.getFirstColumn();
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int LastColumn = range.getLastColumn();
Cell cfirst = sheet.getRow(firstRow).getCell(firstColumn);
// 填充被拆分单元格的值
for (int m = firstRow; m <= lastRow; m++) {
for (int n = firstColumn; n <= LastColumn; n++) {
Cell tc = sheet.getRow(m).getCell(n);
tc.setCellStyle(cfirst.getCellStyle());
tc.setCellValue(cfirst.getStringCellValue());
}
}
}
}