poi导入读取时间格式问题

万能处理方案

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

yyyy-MM-dd-----14

yyyy年m月d日--- 31

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

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

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

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

 

 

[java] view plain copy
 
  1. //1、判断是否是数值格式    
  2. if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){    
  3.     short format = cell.getCellStyle().getDataFormat();    
  4.     SimpleDateFormat sdf = null;    
  5.     if(format == 14 || format == 31 || format == 57 || format == 58){    
  6.         //日期    
  7.         sdf = new SimpleDateFormat("yyyy-MM-dd");    
  8.     }else if (format == 20 || format == 32) {    
  9.         //时间    
  10.         sdf = new SimpleDateFormat("HH:mm");    
  11.     }    
  12.     double value = cell.getNumericCellValue();    
  13.     Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);    
  14.     result = sdf.format(date);    
  15. }    
[java] view plain copy
 
  1. 以上是借鉴他人博客,下面是我的解决方法  
[java] view plain copy
 
  1. //判断是否是数值格式   
  2.                                            if(row.getCell(4).getCellType() == HSSFCell.CELL_TYPE_NUMERIC){  
  3.                                                short format = row.getCell(4).getCellStyle().getDataFormat();  
  4.                                                //判断日期个格式是否是 2017/01/01 这样  
  5.                                                 /* 
  6.                                                  * 14 yyyy-MM-dd / 2017/01/01 
  7.                                                  * 31 yyyy年m月d日 
  8.                                                  * */  
  9.                                                if(format == 14 || format == 31){  
  10.                                                    Date date = HSSFDateUtil.getJavaDate(row.getCell(4).getNumericCellValue());       
  11.                                                        
  12.                                                }  

原文出处:

[1] ljx_1993, poi导入读取时间格式问题, http://write.blog.csdn.net/postedit

原文地址:https://www.cnblogs.com/ryelqy/p/10104112.html