poi处理excel的合并单元格写的工具类,支持xlsx和xls

Posted lgqrlchinese

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poi处理excel的合并单元格写的工具类,支持xlsx和xls相关的知识,希望对你有一定的参考价值。

1.判断当前单元格是不是合并单元格

 1 ...
 2     private boolean isMergedRegion(Sheet sheet, int row, int column) {
 3         //获取合并单元格的数量
 4         int sheetMergeCount = sheet.getNumMergedRegions();
 5         //循环合并的单元格
 6         for (int i = 0; i < sheetMergeCount; i++) {
 7             //获取合并单元格对象
 8             CellRangeAddress range = sheet.getMergedRegion(i);
 9             //获取第i个合并单元格的边界
10             int firstColumn = range.getFirstColumn();
11             int lastColumn = range.getLastColumn();
12             int firstRow = range.getFirstRow();
13             int lastRow = range.getLastRow();
14             //通过判断边界的大小范围来判断是不是合并单元格
15             if (row >= firstRow && row <= lastRow) {
16                 if (column >= firstColumn && column <= lastColumn) {
17                     return true;
18                 }
19             }
20         }
21         return false;
22     }
23 ...

2.获取单元格的边界内容

 1 ...
 2     public Map<String, Integer> getMergedRegionRange(Sheet sheet, int row, int column) {
 3         //获取合并单元格的数量
 4         int sheetMergeCount = sheet.getNumMergedRegions();
 5         //循环获取的合并单元格
 6         for (int i = 0; i < sheetMergeCount; i++) {
 7             CellRangeAddress ca = sheet.getMergedRegion(i);
 8             //获取合并单元格的边界
 9             int firstColumn = ca.getFirstColumn();
10             int lastColumn = ca.getLastColumn();
11             int firstRow = ca.getFirstRow();
12             int lastRow = ca.getLastRow();
13             //如果这个是合并单元格,将内容放在map里面
14             if (row >= firstRow && row <= lastRow) {
15                 if (column >= firstColumn && column <= lastColumn) {
16                     Row fRow = sheet.getRow(firstRow);
17                     Map<String, Integer> cellRangeMap = new HashMap<>();
18                     cellRangeMap.put("firstRow", firstRow);
19                     cellRangeMap.put("lastRow", lastRow);
20                     cellRangeMap.put("firstColumn", firstColumn);
21                     cellRangeMap.put("lastColumn", lastColumn);
22                     return cellRangeMap;
23                 }
24             }
25         }
26         return null;
27     }
28 ...

3.获取单元格的值

 1 ...
 2     public String getMergedRegionValue(Sheet sheet, int row, int column) {
 3         //获取合并单元格的数量
 4         int sheetMergeCount = sheet.getNumMergedRegions();
 5         //循环获取的合并单元格
 6         for (int i = 0; i < sheetMergeCount; i++) {
 7             CellRangeAddress ca = sheet.getMergedRegion(i);
 8             int firstColumn = ca.getFirstColumn();
 9             int lastColumn = ca.getLastColumn();
10             int firstRow = ca.getFirstRow();
11             int lastRow = ca.getLastRow();
12             if (row >= firstRow && row <= lastRow) {
13                 if (column >= firstColumn && column <= lastColumn) {
14                     Row fRow = sheet.getRow(firstRow);
15                     Cell fCell = fRow.getCell(firstColumn);
16                     return getCellValue(fCell);
17                 }
18             }
19         }
20         return null;
21     }
22 ...

4.设置合并单元格

...
int firstRow = Integer.parseInt(((HashMap) topRangeMap).get("firstRow").toString());
int lastRow = Integer.parseInt(((HashMap) topRangeMap).get("lastRow").toString());
int firstColumn = Integer.parseInt(((HashMap) topRangeMap).get("firstColumn").toString());
int lastColumn = Integer.parseInt(((HashMap) topRangeMap).get("lastColumn").toString());
        CellRangeAddress cra = new CellRangeAddress(firstRow, lastRow, firstColumn, lastColumn);
        sheet.addMergedRegion(cra);
...

 

以上是关于poi处理excel的合并单元格写的工具类,支持xlsx和xls的主要内容,如果未能解决你的问题,请参考以下文章

poi处理excel基本操作时写的工具类

poi Java生成excel合并单元格后字体居中

原创POI操作Excel导入导出工具类ExcelUtil

java 利用poi 实现excel合并单元格后出现边框有的消失的解决方法

我也遇到了java poi合并excel单元格后 边框显示异常 ,异常部分都是合并的单元格?

java导出复杂excel表格