zoukankan      html  css  js  c++  java
  • 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);
    ...
    昔日我曾苍老,如今风华正茂(ง •̀_•́)ง
  • 相关阅读:
    ESP8266 STA TCP 客户端配置并连接通信
    Modbus CRC16 校验计算函数
    自写简易版从机Modbus
    STM32CubeIDE查看内存使用情况
    WPF 样式Style
    WPF选项卡页面分离之Page调用Window类
    WPF 多个选项卡TabControl 页面分离
    STM32Cube IDE 汉字字体变小解决办法
    浮点数double相等性比较
    Ling应用
  • 原文地址:https://www.cnblogs.com/lgqrlchinese/p/13080051.html
Copyright © 2011-2022 走看看