码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-06-09 20:38:19      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:gem   range   单元格   ring   stc   put   bsp   hash   ++   

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

标签:gem   range   单元格   ring   stc   put   bsp   hash   ++   

原文地址:https://www.cnblogs.com/lgqrlchinese/p/13080051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!