Java poi3.17 如何区分获取日期类型的单元格的值

1、网上看了好多方案,似乎都没有效果。
2、解决方法:(补充:后来发现这种方法,还是不太行。)
最后采取了这种方式来校验,如果不是Date类型的话,就会转失败!
try{
							DateFormat inputFormat = new SimpleDateFormat("M/d/yy");
							DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
							Date date = inputFormat.parse(colTxt);
							colTxt = outputFormat.format(date);
						}catch (Exception e){
							System.out.println("格式异常:"+e.getMessage());
						}
如果是日期类型的单元格,index 是等于3的。
但是我偶尔到的情况,不是等于日期类型,也会等于3,这时获取
Date date = cell.getDateCellValue(); 值会报错,
解决方法,就是用try/catch包起来,继续使用dataForMatter.formatCellValue(cell); 获取的值就可以了

colTxt = dataForMatter.formatCellValue(cell);
							CellStyle cellStyle = cell.getCellStyle();
							if(cellStyle != null){
								short index = cell.getCellStyle().getIndex();
								if( index == 3){
									try{
										Date date = cell.getDateCellValue();
										if(date != null){
											colTxt = DateUtil.format(date,DateUtil.DEFAULT_FORMAT_DATE);
										}
									}catch (Exception e){
										e.printStackTrace();
									}
								}
							}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号