日期:2014-05-18  浏览次数:20578 次

jxl的问题!
9:18:00     这样一个数据(估计是时间类型的)   .
用了自定义格式   所以看到的效果是这样的       9时18分

把单元格的格式换成常规后变成0.3875
换成时间就是正确的显示9:18:00

用Cell.getContents()方法返回的是0.3875
可是我要的是9:18:00啊.

各位高手帮帮忙啊!     怎么可以获得时间类型的?


------解决方案--------------------
你用DateCell试试

java.util.Date getDate()
Gets the date contained in this cell
java.text.DateFormat getDateFormat()
Gets the DateFormat used to format the cell.
boolean isTime()
Indicates whether the date value contained in this cell refers to a date, or merely a time

------解决方案--------------------
不推荐用Date对象和getDate()方法,最好是用Calendar对象和其方法.
------解决方案--------------------
不行哦. 现在这个cell是Number类型的.
和日期类型的DateCell不匹配啊!强制转换就出错了.
------解决方案--------------------
刚试了一下,如果单元格的类型是“时间”的话
sheet.getcell(,).getContents()返回的是你要的格式啊
------解决方案--------------------
如果单元格是number型,就给你个方法来把数值转换为你要的格式吧,很简单的

NumberCell cell = (NumberCell)sheet.getCell(?,?);
String timeStr = numberToTime(cell.getValue());

public String numberToTime(double value){
StringBuffer sb = new StringBuffer();
long seconds = (long)(value * 86400);
sb.append(seconds/3600);
sb.append( ": ");
sb.append((seconds%3600)/60);
System.out.println((int)(value*60)%60);
sb.append( ": ");
sb.append(seconds%60);
return sb.toString();
}
------解决方案--------------------
把所有的单元格统统设为文本格式,就可以处理了.也很方面,干吗非得给自己找麻烦.
------解决方案--------------------
Excel里有很多计算公式啊,计算公式是要看单元格属性的,所以不是所有的情况都可以写成文本,如果全是文本,用csv就行了,何苦写xls呢,

刚才的代码里面那句System.out.println是调试的,忘了删了 :P