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;
    }

}

你可能感兴趣的:(excel,java,开发语言)