Excel表格数据读取问题

处理复杂Excel表格数据读取问题

package com.jeesite.modules.ma.common.constant;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

public class ExcelUtils {

    public static Object getCellValue(Cell cell) {
        /**
         * switch (cellTypeCode) {
         *             case 0:
         *                 return "numeric";
         *             case 1:
         *                 return "text";
         *             case 2:
         *                 return "formula";
         *             case 3:
         *                 return "blank";
         *             case 4:
         *                 return "boolean";
         *             case 5:
         *                 return "error";
         *             default:
         *                 return "#unknown cell type (" + cellTypeCode + ")#";
         *         }
         */
        switch (cell.getCellType()) {
            case 0:
                //数字类型转变为字符串类型(一般用于电话号码读取)
                cell.setCellType(Cell.CELL_TYPE_STRING);
                return cell.getStringCellValue();
            case 1:
                return cell.getStringCellValue();
            case 2:
                return cell.getCellFormula();
            default:
                return "";
        }
    }

    /**
     * 判断指定行列是否为合并单元格
     *
     * @param sheet  Sheet对象
     * @param rownum 行号
     * @param colnum 列号
     * @return 合并单元格对象
     */
    // 获取合并单元格的行数和列数
    public static CellRangeAddress getMergedRegion(Sheet sheet, int rownum, int colnum) {
        int mergedCount = sheet.getNumMergedRegions();
        for (int i = 0; i < mergedCount; i++) {
            CellRangeAddress mergedRegion = sheet.getMergedRegion(i);
            int firstRow = mergedRegion.getFirstRow();
            int lastRow = mergedRegion.getLastRow();
            int firstCol = mergedRegion.getFirstColumn();
            int lastCol = mergedRegion.getLastColumn();
            if ((rownum >= firstRow && rownum <= lastRow) && (colnum >= firstCol && colnum <= lastCol)) {
                // 如果该单元格位于某个合并单元格中,返回该合并单元格的地址
                return mergedRegion;
            }
        }
        return null;
    }

    // 获取合并单元格的内容
    public static String getMergedRegionValue(Sheet sheet, CellRangeAddress mergedRegion) {
        int firstRow = mergedRegion.getFirstRow();
        int lastRow = mergedRegion.getLastRow();
        int firstCol = mergedRegion.getFirstColumn();
        int lastCol = mergedRegion.getLastColumn();
        StringBuilder sb = new StringBuilder();
        for (int i = firstRow; i <= lastRow; i++) {
            Row row = sheet.getRow(i);
            if (row != null) {
                for (int j = firstCol; j <= lastCol; j++) {
                    Cell cell = row.getCell(j);
                    if (cell != null) {
                        sb.append(getCellValue(cell)).append("\t");
                    } else {
                        sb.append("\t");
                    }
                }
            }
        }
        return sb.toString();
    }

    // 判断行是否为空行
    public static boolean isEmptyRow(Row row) {
        if (row == null || row.getLastCellNum() <= 0) {
            return true;
        }
        for (int i = 0; i < row.getLastCellNum(); i++) {
            Cell cell = row.getCell(i);
            if (cell != null && cell.getCellType() != 3) {
                return false;
            }
        }
        return true;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值