poi读取excel时判断单元格

 public static String toNumeric(HSSFSheet sheet, int i, int colIndex) {
        Cell cell = sheet.getRow(i).getCell(colIndex);
        String val = null;
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            val = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            Boolean bol = cell.getBooleanCellValue();
            val = bol.toString();
            break;
        case Cell.CELL_TYPE_NUMERIC:
            if (HSSFDateUtil.isCellDateFormatted(cell)) { //日期类型
                val = cell.toString();
            } else {
                DecimalFormat df = new DecimalFormat("0");
                val = df.format(cell.getNumericCellValue()); //数值类型的
            }
            break;
        case Cell.CELL_TYPE_BLANK:
            break;
        default:
            break;
        }
        return val;
    }

    public static String XssfToNumeric(XSSFSheet sheet, int i, int colIndex) {
        Cell cell = sheet.getRow(i).getCell(colIndex);
        String val = null;
        if (cell != null) {
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                val = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                Boolean bol = cell.getBooleanCellValue();
                val = bol.toString();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                DecimalFormat df = new DecimalFormat("0");
                val = df.format(cell.getNumericCellValue()); //数值类型的
                break;
            default:
                break;
            }
        }
        return val;
    }

 

你可能感兴趣的:(Poi)