zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    hdu 1199 Color the Ball 离散线段树
    poj 2623 Sequence Median 堆的灵活运用
    hdu 2251 Dungeon Master bfs
    HDU 1166 敌兵布阵 线段树
    UVALive 4426 Blast the Enemy! 计算几何求重心
    UVALive 4425 Another Brick in the Wall 暴力
    UVALive 4423 String LD 暴力
    UVALive 4872 Underground Cables 最小生成树
    UVALive 4870 Roller Coaster 01背包
    UVALive 4869 Profits DP
  • 原文地址:https://www.cnblogs.com/ryelqy/p/10104112.html
Copyright © 2011-2022 走看看