POI读取EXCEL日期的问题。

qdcomn 2009-01-11 06:52:52
现在有份EXCEL表格,让很多人填写,在出生年月一列中,有人输入1970年2月,有人输入1970年2月3日,有人输入1970.02,有人输入1970.2,有人输入1970.02.03,有人输入1970-02,有人输入1970-2-3,还有人输入1970.2.3;
我在读取时,进行如下判断

switch(cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
Birthday = String.valueOf(cell.getDateCellValue().toLocaleString);
break;
case HSSFCell.CELL_TYPE_STRING:
Birthday = cell.getStringCellValue().trim();
break;
default:
Birthday = "";
break;
}

这样读取出的出生年月,如果填写的是1970年2月此种格式的,可以经过处理得到;但是如果填写的是1970.02这种格式的,读出的结果就不对了。

请教,有没有什么好的方法来解决??
...全文
4138 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhanghuanduan 2012-08-02
  • 打赏
  • 举报
回复
我把读取出来的数据都封装成数组了,怎么办,无法获取cell啊,本来xls中的时间是yyyy-MM-dd格式,可是读取出来后被解析成yy/MM/dd格式了,怎么办啊!
AslenG 2012-07-02
  • 打赏
  • 举报
回复
看了半天没一个靠谱:还"统一用
cell.getStringCellValue().trim()取字符","cell.getRichStringCellValue()",编译都通不过。
不说了大家看看这篇博客:http://blog.csdn.net/rock_tapestry/article/details/6666526
  • 打赏
  • 举报
回复
学习了。
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");

Date dt = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
String strdate = dateformat.format(dt);
}
break;
这个方法可以不?
呵呵,我还是自己测试测试去吧,谢谢各位。。。
jason6822 2012-05-30
  • 打赏
  • 举报
回复
感谢楼上的友友 学习了······
fq1798 2011-08-23
  • 打赏
  • 举报
回复
我X 这头像太吓人了
li_jinjian2005 2010-04-27
  • 打赏
  • 举报
回复
很多时候,我觉得就应该这样控制一下格式,没有必要吧问题搞的这么滴负责...
li_jinjian2005 2010-04-27
  • 打赏
  • 举报
回复
要想读取到日期,就应该使用 excle里面的有效性设置

//生成日期类有效性
DVConstraint constraint = DVConstraint.createDateConstraint(DVConstraint.OperatorType.BETWEEN, "1970-1-1",
"2100-1-1", "yyy-mm-dd");
//创建一个区域
CellRangeAddressList regions = new CellRangeAddressList(0, 0, 0, 9);
//有效性--区域 组合
HSSFDataValidation data_validation = new HSSFDataValidation(regions, constraint);

A1-A10的单元格就指定了必须为日期,用户只要能填入去,后台就可以读取


Date date = DateUtil.getJavaDate(cell.getNumericCellValue());
cyber_bss 2009-01-12
  • 打赏
  • 举报
回复
我可以肯定地告诉楼主,poi不能处理这种情况,而且这个也不该poi负责处理,
可以先使用getRichStringCellValue得到全部字符串,然后使用自己的方法处理此字符串

自己的方法:
①把"年","月","日","-"等等统统替换为".",这样日期就统一为以"."分割了
②使用SimpleDateFormat对替换好的字符串进行变换.转换失败就

try{
按"yyyy.MM.dd"转换;
返回转换好的Date;
}catch (Exception e){
//doNothing;
}
try{
按"yyyy.MM"转换;
返回转换好的Date;
}catch(Exception e){
//doNothing;
}
......
走到最后则说明是无法识别的格式,返回空串;(或者抛出异常==)
KOOK_OKKO 2009-01-12
  • 打赏
  • 举报
回复
改了下方法 支持 "1998.8"的情况


/****
* 日期String串转Date
* @param str
* @return
*/

public static Date parseDate(String str){
Date d = new Date();
String oper = str;
if(oper.indexOf('.')!=-1){
int count = 0;
while(oper.indexOf('.')!=-1){
count++;
oper = oper.substring(oper.indexOf('.')+1);

}
if(count ==1){

SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
try {
d = sdf.parse(str);
System.out.println(sdf.format(d));

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


if(count==2){

SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
try {
d = sdf.parse(str);
System.out.println(sdf.format(d));

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}
if(str.indexOf('-')!=-1){

int count = 0;
while(oper.indexOf('-')!=-1){
count++;
oper = oper.substring(oper.indexOf('-')+1);

}
if(count ==1){

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
try {
d = sdf.parse(str);
System.out.println(sdf.format(d));

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


if(count==2){

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
d = sdf.parse(str);
System.out.println(sdf.format(d));

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

return d;
}


KOOK_OKKO 2009-01-12
  • 打赏
  • 举报
回复
统一用
cell.getStringCellValue().trim()取字符


然后再调用以下方法转换格式



/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "1990.8.8";
//String str2 = "1990-9-4";
parseDate(str);
}

/****
* 日期String串转Date
* @param str
* @return
*/

public static Date parseDate(String str){
Date d = new Date();
if(str.indexOf('.')!=-1){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
try {
d = sdf.parse(str);
System.out.println(sdf.format(d));

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if(str.indexOf('-')!=-1){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
d = sdf.parse(str);
System.out.println(sdf.format(d));

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

return d;
}

qdcomn 2009-01-12
  • 打赏
  • 举报
回复
poi不能把读取的单元格强制按照string类型来读,对吧?

那可以根据poi提供的几种类型来读取,出生日期,可能就有case HSSFCell.CELL_TYPE_NUMERIC:和case HSSFCell.CELL_TYPE_STRING:两种类型,关键是HSSFCell.CELL_TYPE_NUMERIC的情况时,因为有可能是2008年2月3日,也可能是2008.3,这样一个是日期类型,一个是numeric类型,我不知道怎么处理了。
qdcomn 2009-01-12
  • 打赏
  • 举报
回复
getRichStringCellValue???
这是是什么意思,我使用的poi-2.5.1-final-20040804.jar,里边没有此方法呢??
L502650 2009-01-12
  • 打赏
  • 举报
回复
楼上正解.
無名VF 2009-01-11
  • 打赏
  • 举报
回复
没遇到过 友情Up

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧