简体   繁体   English

Java Excel API JXL-读取格式化为时间的单元格

[英]Java Excel API JXL - reading cell formated as time

I'm using Java Excel API to read data from Excels sheets in my Android project. 我正在使用Java Excel API从我的Android项目中的Excels表格中读取数据。 I have some columns formated as time like this: 我有一些按时间格式格式化的列: 时间格式

At first I tried read this cell using that code: 首先,我尝试使用该代码读取此单元格:

String cellContent = cell.getContents();

But as result I got content without hours eg :59:59. 但是结果是我没有几个小时就得到内容,例如:59:59。 After that I tried get this time as seconds. 之后,我尝试将时间设为秒。 I noticed that when cell data is get as DateCell, Date contents object has negative timestamp. 我注意到当单元格数据作为DateCell获得时,Date内容对象的时间戳记为负。 I did some trick and I found value of timestamp for 0:00:00. 我做了一些技巧,发现timestamp的值为0:00:00。 I set variable with this value as positive (2209161600000L). 我将此变量设置为正(2209161600000L)。 Now I just add this variable to negative timestamp from cell and devide result by 1000 (milliseconds in second). 现在,我将这个变量添加到来自单元格的负时间戳中,并将结果分配1000(毫秒)。 Everything is fine until 23:59:59. 直到23:59:59一切都很好。 Values above have extra 24 h (24:00:00 is reading as 48:00:00). 上面的值有额外的24小时(24:00:00读为48:00:00)。 I saw that Excel keeps this values in different ways. 我看到Excel以不同的方式保留此值。

23:59:59时间单元值24:00:00时间单元值

I can just subtract this 24h but it isnt elegant solution. 我可以减去这24小时,但这不是一个优雅的解决方案。 I want to get content as #:##:## string (which I prefer) or as seconds. 我想以#:##:##字符串(我更喜欢)或以秒为单位来获取内容。 Is there any good solution for this issue? 这个问题有什么好的解决方案吗?

Java Excel API is not developed since 2010 so I change library to Apache POI. 自2010年以来未开发Java Excel API,因此我将库更改为Apache POI。 Time cell is returned as fraction of 24h. 时间单元以24小时的分数返回。

double fractionOfDay = cell.getNumericCellValue();
long timeInSeconds = Math.round(numericValue * SECONDS_IN_DAY);

Now everything works fine. 现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM