JAVA poi导入Excel处理多种日期格式

Excel本身支持很多格式日期,在导入Excel总是容易出问题,这里整理了常用的一些格式,可以有效转化为JAVA的Date类型,其中包括Excel的单元格设置里面的“日期”、“时间”和”自定义格式“,君自取

/**
     * 数据导入处理 获取单元格值
     *
     * @param row 获取的行
     * @param column 获取单元格列号
     * @return 单元格值
     */
    public String getCellValue(Row row, int column)
    {
        Object val = "";
        if (row == null)
        {
            return String.valueOf(val);
        }
        try
        {
            Cell cell = row.getCell(column);
            if (StringUtils.isNotNull(cell))
            {
                /**
                 * format 的值可能为以下这些 yyyyMMddHHmmss
                 * yyyy-MM-dd----- 14
                 * yyyy年m月d日----- 31
                 * yyyy年m月--------57
                 * m月d日  -----------58
                 * HH:mm-----------20
                 * h时mm分  --------- 32
                 */
                if (cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA)
                {
                    val = cell.getNumericCellValue();
                    // POI Excel 日期格式转换
                    String formatDate = "";
                    switch (cell.getCellStyle().getDataFormat()){
                        case 14:
                            formatDate = "yyyy-MM-dd";
                            break;
                        case 20:
                            formatDate = "HH:mm";
                            break;
                        case 21:
                            formatDate = "HH:mm:ss";
                            break;
                        case 31:
                            formatDate = "yyyy年MM月dd日";
                            break;
                        case 32:
                            formatDate = "HH时mm分";
                            break;
                        case 33:
                            formatDate = "HH时mm分mm秒";
                            break;
                        case 57:
                            formatDate = "yyyy年MM月";
                            break;
                        case 58:
                            formatDate = "MM月dd日";
                            break;
                        case 176:
                            formatDate = "yyyy-MM-dd";
                            break;
                    }
                    if(!"".equals(formatDate)){
                        val = new SimpleDateFormat(formatDate).format(DateUtil.getJavaDate((Double) val));
                        System.out.println(val);
                    }else{
                        if ((Double) val % 1 != 0)
                        {
                            val = new BigDecimal(val.toString());
                        }
                        else
                        {
                            val = new DecimalFormat("0").format(val);
                        }
                    }
                }
                else if (cell.getCellType() == CellType.STRING)
                {
                    val = cell.getStringCellValue();
                }
                else if (cell.getCellType() == CellType.BOOLEAN)
                {
                    val = cell.getBooleanCellValue();
                }
                else if (cell.getCellType() == CellType.ERROR)
                {
                    val = cell.getErrorCellValue();
                }
            }
        }
        catch (Exception e)
        {
            return String.valueOf(val);
        }
        return String.valueOf(val);
    }

猜你喜欢

转载自blog.csdn.net/zlxls/article/details/128917719