现在我有一个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。
请问如何实现。
我现在用POI来取这个Cell里面的这个值,不是6322:00:00而是1900-9-15 13:00:00,我现在要转化成6322:00:00。
请问如何实现。
解决方案 »
- 【散分】进者有分
- JDI问题,关于各种EventRequests的suspendpolicy设置
- java 如何添加图片?
- 谁有lucene.jar的包给我发一个吧!!
- 笔试
- volatile关键字的具体作用有哪些?
- 请启动资源管理器后,再运行任一JFrame程序,窗口最小化后再恢复,看看内存消耗值的变化大不大,还能不能恢复到最小化前那么大
- java正则表达式截取字符的问题
- 下周开始看java版的数据结构和算法,内附问题请指教,兼散分
- 100分 加 一躬到地!如何将 ResultSet 对象转化成 Object[][]的形式?
- 新手求教~~~~~~~~~~~~~
- eclipse编译错误为什么会显示?
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("yyyy/m/d h:mm"));
cell.setCellStyle(style);没验证,很久没使用了,还是一年前用过POI.
style.setDataFormat(format.getFormat("[h]:mm:ss"));
我现在的思路是这样的,取出1900-9-15 13:00:00,然后getTimeInMillis(),再取出1899-12-30 00:00:00的getTimeInMillis(),用这两个毫秒去减。然后用得到的值 / 1000是秒,/ (60 * 1000)是分等等。这方法太傻了。有的时候是好的,有的时候总差1秒。
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();
}
}
做程序几年了,越来越反感这一行。今年六月份要转行了,看到各位的回答真的很无语。
以前做数据仓库时的回答更是无语,结贴了,贴出正确答案是在国外一个论坛上找到答案的。什么时候能像老外一样的话,在中国做软件还是有前途的。
不过不可能了,所以中国没有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
你要在Java中求两个Date间相差的天数或小时数或秒数等,我是没有发现API中有现成的操作,都只能自己去实现;也没有提供像Excel这样定义一个这样的[h]格式,就能得出总小时数。