poi 日期处理

来源:互联网 发布:c语言疯狂讲义 v4.0 编辑:程序博客网 时间:2024/04/27 17:03
    @SuppressWarnings({ "static-access" })
    private String getValue(Cell hssfCell) {
        if (hssfCell == null) {
            return null;
        }
        if (hssfCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            // 返回布尔类型的值
            return String.valueOf(hssfCell.getBooleanCellValue());
        } else if (hssfCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
             if (HSSFDateUtil.isCellDateFormatted(hssfCell)) {  
                 SimpleDateFormat sdf = null;  
                 if (hssfCell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) {  
                     sdf = new SimpleDateFormat("HH:mm");  
                 } else {// 日期  
                     sdf = new SimpleDateFormat("yyyy-MM-dd");  
                 }  
                 Date date = hssfCell.getDateCellValue();  
                 return sdf.format(date);  
             } else if (hssfCell.getCellStyle().getDataFormat() == 58) {  
                 // 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)  
                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
                 double value = hssfCell.getNumericCellValue();  
                 Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);  
                 return sdf.format(date);  
             } else {  
                 double value = hssfCell.getNumericCellValue();  
                 CellStyle style = hssfCell.getCellStyle();  
                 DecimalFormat format = new DecimalFormat();  
                 String temp = style.getDataFormatString();  
                 // 单元格设置成常规  
                 if (temp.equals("General")) {  
                     format.applyPattern("#");  
                 }  
                 return format.format(value);  
             }  
        } else {
            // 返回字符串类型的值
            hssfCell.setCellType(hssfCell.CELL_TYPE_STRING);
            return hssfCell.toString();
        }
    }
0 0
原创粉丝点击