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

alwaysmove / 2024-08-08 / 原文

 

1、网上看了好多方案,似乎都没有效果。

2、解决方法:

如果是日期类型的单元格,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();
									}
								}
							}