解析Excel中Cell的格式为[h]:mm:ss

vicvin 2011-04-12 04:37:37
现在我有一个Excel,有一列的格式为[h]:mm:ss,显示在Excel的格式就是6322:00:00的格式。这个6322:00:00对应的时间就是1900-9-15 13:00:00。
我现在用POI来取这个Cell里面的这个值,不是6322:00:00而是1900-9-15 13:00:00,我现在要转化成6322:00:00。
请问如何实现。
...全文
593 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxbccsu 2011-04-13
  • 打赏
  • 举报
回复
上面的格式[h]:mm:ss中的[h]是excel求出了一个总小时数,但实质还是个Date,只是在excel显示出了这种格式,POI只能得出这个Date;
你要在Java中求两个Date间相差的天数或小时数或秒数等,我是没有发现API中有现成的操作,都只能自己去实现;也没有提供像Excel这样定义一个这样的[h]格式,就能得出总小时数。
vicvin 2011-04-13
  • 打赏
  • 举报
回复
感谢各位的回答,怎么讲呢?咱们中国人什么时候做事严谨一点啊。麻烦都先把我的题目认真的读完再讲出来答案好不好啊。
做程序几年了,越来越反感这一行。今年六月份要转行了,看到各位的回答真的很无语。
以前做数据仓库时的回答更是无语,结贴了,贴出正确答案是在国外一个论坛上找到答案的。什么时候能像老外一样的话,在中国做软件还是有前途的。
不过不可能了,所以中国没有Google没有苹果没有微软。
答案是:
Dates and times in Excel are stored as floating point numbers. Dates are days since 1900 (a date of 1 is 01/01/1900), and times are fractions of a day (so 0.5 is 12 hours).

Depending on what format you need the value to be in before you put it in a database, then you might just want to take the numeric value and multiple by 24 to get the hours and fractional hours, eg:

double time = cell.getNumericCellValue() * 24;
int hours = (int)time;
int minutes = (time - hours) * 60;
Alternately, if you want a string, then the DataFormatter class will happily format a value of 1.5 as HH:MM to 36:00
fengyumanlou101 2011-04-13
  • 打赏
  • 举报
回复
我前几天刚好用到,里面有日期类型的,你参考一下:
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ReadDataFromExcel2 {
public static String outputFile = "e:\\test\\tels.xls";

public static String fileToBeRead = "D:\\uws资料\\预收未录入20110407\\预收已录入(青岛).xls";

public void readExcel() {
DecimalFormat df = new DecimalFormat("#");
try {

HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
fileToBeRead));

for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个sheet

short number6 = 1;
short number1 = 3;
short number2 = 7;
short number3 = 8;
short number4 = 9;
short number5 = 10;
//System.out.println("long==" +aSheet.getLastRowNum() );
for (int rowNumOfSheet = 1; rowNumOfSheet <= aSheet
.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet);


HSSFCell agentNo = aRow.getCell(number1);
HSSFCell appClientName = aRow.getCell(number2);
Date appBirthDate = aRow.getCell(number3).getDateCellValue();
HSSFCell insClientName = aRow.getCell(number4);
Date insBirthDate = aRow.getCell(number5).getDateCellValue();
HSSFCell regionCode = aRow.getCell(number6);

HSSFCell insBirthDatetes = aRow.getCell(number3);

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String appBirthDateStr = sdf.format(appBirthDate);
String insBirthDateStr = sdf.format(insBirthDate);

int cellType = insBirthDatetes.getCellType();
//System.out.println("test=="+cellType);

String uwsdataEntru = new String();
uwsdataEntru = "insert into uws_plan_convert_client_record(region_code_2,agentno,app_client_name,app_birth_date,ins_client_name,ins_birth_date,proc_type,is_valid,proc_date,proc_user)values('" +
regionCode.getStringCellValue()+"','"+
agentNo.getStringCellValue() + "','" +
appClientName.getStringCellValue() + "',date'" +
appBirthDateStr + "','" +
insClientName.getStringCellValue() + "',date'" +
insBirthDateStr + "','01','Y',sysdate,user);";

System.out.println(uwsdataEntru);

/*switch (cellType) {
case 0:// Numeric
String strCell = df.format(insBirthDate
.getNumericCellValue());
System.out.println(strCell);

break;
case 1:// String
strCell = insBirthDate.getStringCellValue();
System.out.println(strCell);

break;
default:

}*/
}
}
}
}
} catch (Exception e) {
System.out.println("ReadExcelError" + e);
}
}

public static void main(String[] args) {
ReadDataFromExcel2 poi = new ReadDataFromExcel2();
// poi.CreateExcel();
poi.readExcel();
}
}
vicvin 2011-04-13
  • 打赏
  • 举报
回复
不是要create一个cell是本来有一个EXCEL我要读取里面的值。而这个EXCEL有一列已经格式成了[h]:mm:ss,但是我取出来的却是yyyy-MM-dd HH:mm:ss.例如:Excel里面显示的是6322:00:00取出来的却是1900-9-15 13:00:00。
我现在的思路是这样的,取出1900-9-15 13:00:00,然后getTimeInMillis(),再取出1899-12-30 00:00:00的getTimeInMillis(),用这两个毫秒去减。然后用得到的值 / 1000是秒,/ (60 * 1000)是分等等。这方法太傻了。有的时候是好的,有的时候总差1秒。
lxbccsu 2011-04-12
  • 打赏
  • 举报
回复
看错了 那个格式是:
style.setDataFormat(format.getFormat("[h]:mm:ss"));
lxbccsu 2011-04-12
  • 打赏
  • 举报
回复
格式化这个cell就可以。

CellStyle style = wb.createCellStyle();
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("yyyy/m/d h:mm"));
cell.setCellStyle(style);

没验证,很久没使用了,还是一年前用过POI.

62,614

社区成员

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

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