在用java 读取excel文件时会有这样的情况的发生 文件内容 18:05 读出后 6:05 原因是引用jxl 时有个pattern类用的是12时制的时间,所以...., 要自己写个方法来再次将它读出的时间进行转换.
package
blogic;
import jxl.Cell;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.io.FileInputStream;
import java.io.File;
/** */ /**
* <概要描述> 倒入考勤表
*
* @author xx
* @version 1.0 2008/01/07
*/
public class readExcel ... {
/** *//**
* <概要描述> excle文件中时间类型数据格式转换
*
* @param excle文件中时间类型数据
* @return String
* @throws 无
*/
public static String FormateTime(Cell formatecell) ...{
try ...{
java.util.Date mydate = null;
DateCell datecll = (DateCell) formatecell;
mydate = datecll.getDate();
long time = (mydate.getTime() / 1000) - 60 * 60 * 8;
mydate.setTime(time * 1000);
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
return formatter.format(mydate);
} catch (Exception e) ...{
e.printStackTrace();
return null;
}
}
/** *//**
* <概要描述> 读excle文件
*
* @param 文件路径
* @return 动态数组
* @throws IOException
* @throws BiffException
* @throws 无
*/
public void ReadExcelToDb(String filePath) ...{
Workbook rwb = null;
boolean flag = true;
String OndutyDate=null;
String onTime=null;
String offTime = null;
String Remark = null;
String WorkTime = null;
String OverTime = null;
String WorkFlag = null;
int rowStart = 7;//cc.NUMBER_7;// 程序从文件的第几行开始读
try ...{
InputStream is = new FileInputStream(filePath);
rwb = Workbook.getWorkbook(is);
rwb = Workbook.getWorkbook(new File(filePath));
// 取得文件的第一个 sheet;
Sheet rs = rwb.getSheet(0);
//读取文件中数据
String year = rs.getCell(4, 2).getContents();
String month = rs.getCell(5, 2).getContents();
// 上面条件都符合后开始读取文件
for (int k = rowStart; k < rs.getRows(); k++) ...{
if (flag == false) ...{
break;
}
String day = rs.getCell(0, k).getContents();
// 当文件的日期列,数据为空时停止操作
if (day.equals("")) ...{
break;
}
// 将日期转换成YYYY-MM-DD格式
OndutyDate = year + "-" + month + "-" + day;
Cell onTimeCell = rs.getCell(2, k);
onTime = String.valueOf(FormateTime(onTimeCell));
Cell offTimeCell = rs.getCell(3, k);
offTime = String.valueOf(FormateTime(offTimeCell));
Remark = rs.getCell(4, k).getContents();
WorkTime = rs.getCell(5, k).getContents();
OverTime = rs.getCell(6, k).getContents();
WorkFlag = rs.getCell(7, k).getContents();
System.out.println(OndutyDate);
System.out.println(onTime);
System.out.println(offTime);
System.out.println(Remark);
System.out.println(WorkTime);
System.out.println(OverTime);
System.out.println(WorkFlag);
}
rwb.close();
} catch (Exception e) ...{
e.printStackTrace();
}
}
public static void main(String args[])...{
readExcel readexcel=new readExcel();
String filePath="C:/modual.xls";
readexcel.ReadExcelToDb(filePath);
}
}
import jxl.Cell;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.io.FileInputStream;
import java.io.File;
/** */ /**
* <概要描述> 倒入考勤表
*
* @author xx
* @version 1.0 2008/01/07
*/
public class readExcel ... {
/** *//**
* <概要描述> excle文件中时间类型数据格式转换
*
* @param excle文件中时间类型数据
* @return String
* @throws 无
*/
public static String FormateTime(Cell formatecell) ...{
try ...{
java.util.Date mydate = null;
DateCell datecll = (DateCell) formatecell;
mydate = datecll.getDate();
long time = (mydate.getTime() / 1000) - 60 * 60 * 8;
mydate.setTime(time * 1000);
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
return formatter.format(mydate);
} catch (Exception e) ...{
e.printStackTrace();
return null;
}
}
/** *//**
* <概要描述> 读excle文件
*
* @param 文件路径
* @return 动态数组
* @throws IOException
* @throws BiffException
* @throws 无
*/
public void ReadExcelToDb(String filePath) ...{
Workbook rwb = null;
boolean flag = true;
String OndutyDate=null;
String onTime=null;
String offTime = null;
String Remark = null;
String WorkTime = null;
String OverTime = null;
String WorkFlag = null;
int rowStart = 7;//cc.NUMBER_7;// 程序从文件的第几行开始读
try ...{
InputStream is = new FileInputStream(filePath);
rwb = Workbook.getWorkbook(is);
rwb = Workbook.getWorkbook(new File(filePath));
// 取得文件的第一个 sheet;
Sheet rs = rwb.getSheet(0);
//读取文件中数据
String year = rs.getCell(4, 2).getContents();
String month = rs.getCell(5, 2).getContents();
// 上面条件都符合后开始读取文件
for (int k = rowStart; k < rs.getRows(); k++) ...{
if (flag == false) ...{
break;
}
String day = rs.getCell(0, k).getContents();
// 当文件的日期列,数据为空时停止操作
if (day.equals("")) ...{
break;
}
// 将日期转换成YYYY-MM-DD格式
OndutyDate = year + "-" + month + "-" + day;
Cell onTimeCell = rs.getCell(2, k);
onTime = String.valueOf(FormateTime(onTimeCell));
Cell offTimeCell = rs.getCell(3, k);
offTime = String.valueOf(FormateTime(offTimeCell));
Remark = rs.getCell(4, k).getContents();
WorkTime = rs.getCell(5, k).getContents();
OverTime = rs.getCell(6, k).getContents();
WorkFlag = rs.getCell(7, k).getContents();
System.out.println(OndutyDate);
System.out.println(onTime);
System.out.println(offTime);
System.out.println(Remark);
System.out.println(WorkTime);
System.out.println(OverTime);
System.out.println(WorkFlag);
}
rwb.close();
} catch (Exception e) ...{
e.printStackTrace();
}
}
public static void main(String args[])...{
readExcel readexcel=new readExcel();
String filePath="C:/modual.xls";
readexcel.ReadExcelToDb(filePath);
}
}