POI获取Excel单元格的值

    public String getCellValue(XSSFCell cell) {
        String value = "";
        // 以下是判断数据的类型
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_NUMERIC:
            value = cell.getNumericCellValue() + "";
            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                if (date != null) {
                    value = new SimpleDateFormat("yyyy-MM-dd").format(date);
                } else {
                    value = "";
                }
            } else {
                value = new DecimalFormat("0").format(cell.getNumericCellValue());
            }
            break;
        case HSSFCell.CELL_TYPE_STRING: // 字符串
            value = cell.getStringCellValue();
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
            value = cell.getBooleanCellValue() + "";
            break;
        case HSSFCell.CELL_TYPE_FORMULA: // 公式
            value = cell.getCellFormula() + "";
            break;
        case HSSFCell.CELL_TYPE_BLANK: // 空值
            value = "";
            break;
        case HSSFCell.CELL_TYPE_ERROR: // 故障
            value = "非法字符";
            break;
        default:
            value = "未知类型";
            break;
        }
        return value;
    }

你可能感兴趣的:(实战篇)