合并单元格的方法:
HSSFSheet.getNumMergedRegions()//获取sheet中合并单元格的总数
HSSFSheet.getMergedRegion(i)//获取POI中自定义顺序的第i个合并单元格,这里所说的自定义顺序和你在excel中看到的合并单元格的顺序完全不一样
获取第i列的合并单元格代码:
int sheetmergerCount = hs.getNumMergedRegions();
int firstrow = 0;
int lastrow = 0;
int firstcolumn = 0;
int lastcolumn = 0;
HSSFCell hc = null;
for (int sheetmergerIndex = 0; sheetmergerIndex < sheetmergerCount; sheetmergerIndex++) {
ca = hs.getMergedRegion(sheetmergerIndex);
hc = hs.getRow(ca.getFirstRow()).getCell(ca.getFirstColumn());
firstrow = ca.getFirstRow();
lastrow = ca.getLastRow();
firstcolumn = ca.getFirstColumn();
lastcolumn = ca.getLastColumn();
if(firstcolumn<=i&&i<=lastcolumn){
list.add(hc);
}
}
判断Excel中是否含有合并单元格
//判断表中是否含有合并单元格
public boolean hasMerged() {
return sheet.getNumMergedRegions() > 0 ? true : false;
}
// 判断指定区域内是否含有合并单元格
public boolean hasMerged(Region region) {
for (int row = region.getRowFrom(); row < region.getRowTo(); row++) {
for (short col = region.getColumnFrom(); col < region.getColumnTo(); col++){
for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
Region r = sheet.getMergedRegionAt(i);
if (r.contains(row, col)) {
return true;
}
}
}
}
return false;
}