1
我有一个excel文件,它有几个字符串数据类型列,一个数字列和一个日期列。我正在使用Apache POI来读取文件。 下面是我如何处理数据类型在Java中使用POI读取Excel
Cell cell = sheet.getRow(i).getCell(j);
if(cell!=null){
switch(cell.getCellType()){
case Cell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date cellDate = cell.getDateCellValue();
cellValue = df.format(cellDate);
break;
case Cell.CELL_TYPE_BLANK:
break;
default :
}
}
这正常工作与字符串和日期数据类型。但对于数字,它将该值转换为日期。我知道这是因为处理有问题。你可以请教如何适应数字和日期数据类型的处理?
谢谢, 山姆。
为什么不直接使用[DataFormatter](http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html)将单元格变成字符串,并让它处理你的复杂性? – Gagravarr