poi-处理excel的单元格日期数据

poi处理excel时,当excel没有明确指明是哪个类型的数据时,poi很可能处理单元格的日期数据时就有可能是一串数字。而使用java程序基本无法转换

以下为对poi处理日期情况一些方面的处理(不是很全,简单能用一些)

本文主要思路来源这里

    private List<Map<Integer,Object>> process(String sheetName){
        if( wb == null ) return null;
        XSSFSheet sheet = wb.getSheet(sheetName);
        if(sheet==null) return null;
        int lastRowNum = sheet.getLastRowNum()+1;
        if(lastRowNum<=0) return null;
        List<Map<Integer,Object>> result = new ArrayList<Map<Integer,Object>>();
        for (int i = 1; i < lastRowNum; i++) {
            Map<Integer,Object> rowMap = new LinkedHashMap<Integer,Object>();
            result.add(rowMap);
            XSSFRow row = sheet.getRow(i);
            if( row == null)continue;
            short lastCellNum =row.getLastCellNum();
            if(lastCellNum<=0) continue;
            for (int j = 0; j < lastCellNum; j++) {
                XSSFCell cell = row.getCell(j);
                Object value = null;
                if(cell!=null){
                    switch(cell.getCellType()) {
                    case XSSFCell.CELL_TYPE_BOOLEAN:
                        value = cell.getBooleanCellValue();
                        break;
                    case XSSFCell.CELL_TYPE_NUMERIC:
                        short format = cell.getCellStyle().getDataFormat();  
                        if (HSSFDateUtil.isCellDateFormatted(cell)) {
                            Date d = cell.getDateCellValue();
                            DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            value = formater.format(d);
                        }else if(format == 14 || format == 31 || format == 57 || format == 58){  
                            //日期  
                            DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");  
                            Date date = DateUtil.getJavaDate(cell.getNumericCellValue());  
                            value = formater.format(date);  
                        }else if (format == 20 || format == 32) {  
                            //时间  
                            DateFormat formater = new SimpleDateFormat("HH:mm");    
                            Date date = DateUtil.getJavaDate(cell.getNumericCellValue());  
                            value = formater.format(date);  
                        } else{
                            value = cell.getNumericCellValue();
                        }
                        //System.out.println(value+":"+cell.getCellStyle().getDataFormat());
                        break;
                    case XSSFCell.CELL_TYPE_STRING:
                        value = cell.getStringCellValue();
                        break;
                    }
                }
                value = rowMap.put(j,value);
            }
        }
        return result;
    }
}

 

 

Excel数据处理:

Excel存储日期、时间均以数值类型进行存储,读取时POI先判断是是否是数值类型,再进行判断转化

1、数值格式(CELL_TYPE_NUMERIC):

1.纯数值格式:getNumericCellValue() 直接获取数据

2.日期格式处理yyyy-MM-dd, d/m/yyyy h:mm, HH:mm 等不含文字的日期格式

1).判断是否是日期格式:

HSSFDateUtil.isCellDateFormatted(cell)

 

2).判断是日期或者时间

cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")

OR: cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("yyyy-MM-dd")

 

3.自定义日期格式处理yyyy年m月d日,h时mm分,yyyy年m月等含文字的日期格式

判断cell.getCellStyle().getDataFormat()值,解析数值格式

yyyy年m月d日----->31

m月d日---->58

h时mm分--->32

 

2、字符格式(CELL_TYPE_STRING):直接获取内容

 eg:

private String parseExcel(Cell cell) {
        String result = new String();
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_NUMERIC:// 数字类型
            if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
                SimpleDateFormat sdf = null;
                if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
                        .getBuiltinFormat("h:mm")) {
                    sdf = new SimpleDateFormat("HH:mm");
                } else {// 日期
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                }
                Date date = cell.getDateCellValue();
                result = sdf.format(date);
            } else if (cell.getCellStyle().getDataFormat() == 58) {
                // 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                double value = cell.getNumericCellValue();
                Date date = org.apache.poi.ss.usermodel.DateUtil
                        .getJavaDate(value);
                result = sdf.format(date);
            } else {
                double value = cell.getNumericCellValue();
                CellStyle style = cell.getCellStyle();
                DecimalFormat format = new DecimalFormat();
                String temp = style.getDataFormatString();
                // 单元格设置成常规
                if (temp.equals("General")) {
                    format.applyPattern("#");
                }
                result = format.format(value);
            }
            break;
        case HSSFCell.CELL_TYPE_STRING:// String类型
            result = cell.getRichStringCellValue().toString();
            break;
        case HSSFCell.CELL_TYPE_BLANK:
            result = "";
        default:
            result = "";
            break;
        }
        return result;
    }

 *万能处理方案

所有日期格式都可以通过getDataFormat()值来判断

yyyy-MM-dd----- 14

yyyy年m月d日--- 31

yyyy年m月------- 57

m月d日  ---------- 58

HH:mm----------- 20

h时mm分  ------- 32

 

    //1、判断是否是数值格式
    if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
        short format = cell.getCellStyle().getDataFormat();
        SimpleDateFormat sdf = null;
        if(format == 14 || format == 31 || format == 57 || format == 58){
            //日期
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        }else if (format == 20 || format == 32) {
            //时间
            sdf = new SimpleDateFormat("HH:mm");
        }
        double value = cell.getNumericCellValue();
        Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
        result = sdf.format(date);
    }

 

posted @ 2016-03-07 14:05  243573295  阅读(1970)  评论(0编辑  收藏  举报