POI获取Excel单元格值

/**
     * 获取单元格值信息
     *
     * @param cell
     * @return
     * @Description:
     */
    public static String getCellValue(Cell cell)
    {
        DecimalFormat df = new DecimalFormat("#");
        String cellValue = "";
        if (cell == null)
        {
            return cellValue;
        }
        // 判断数据的类型
        switch (cell.getCellType())
        {
            case Cell.CELL_TYPE_NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(cell))
                {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
                }
                return df.format(cell.getNumericCellValue());
            case Cell.CELL_TYPE_STRING: // 字符串
                cellValue = String.valueOf(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN: // Boolean
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA: // 公式
                cellValue = String.valueOf(cell.getCellFormula());
                break;
            case Cell.CELL_TYPE_BLANK: // 空值
                cellValue = "";
                break;
            case Cell.CELL_TYPE_ERROR: // 故障
                cellValue = "非法字符";
                break;
            default:
                cellValue = "未知类型";
                break;
        }
        return cellValue.trim();
    }

猜你喜欢

转载自gaochunhu.iteye.com/blog/2401100