就是用的时候传进去一个实现类方法和excel名字,以前的硬盘丢了,谢谢大神!
(导入)(导出)
@Test
public void getCheckInPersonList(){
String filePath = new String("E:\2014年龙居山9月考勤.xls");
File file = new File(filePath);
if(file.exists()){
if(file.getName() != null){
String suffix = file.getName().substring(file.getName().lastIndexOf(".")+1);
if("xls".equals(suffix))
for (CheckInPersonBean p : readXLS(file)) {
System.out.println("ID:"+p.getId()+" 单位:"+p.getUnit()+" 姓名:"+p.getName()+" 时间:"+p.getWorkDate()+" 上班打卡时间:"+p.getWorkStartTime()+" 下午打卡时间:"+p.getWorkEndTime());
}
}
}
}
public List<CheckInPersonBean> readXLS(File file){
List<CheckInPersonBean> personBeanList = new ArrayList<CheckInPersonBean>();
try {
InputStream is = new FileInputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(is);
if(workbook != null){
//getNumberOfSheets 得到工作表数量
//System.out.println(workbook.getNumberOfSheets()+"工作表数量");
for(int i=0;i<workbook.getNumberOfSheets();i++){
//getSheetAt(i) 循环得到第i张工作表
HSSFSheet sheet = workbook.getSheetAt(i);
if(sheet != null){
//getLastRowNum() 得到工作表中最后一行号
//System.out.println("第"+(i+1)+"张表最后一个行号是:"+sheet.getLastRowNum());
CheckInPersonBean personBean = null;
for(int j=1;j<=sheet.getLastRowNum();j++){
//getRow(j) 得到第j行数据
HSSFRow row = sheet.getRow(j);
//得到行最后一个单元格数字 getLastCellNum 该表中第j行最后一个单元格号(只要是单元格就计算,不论内值是否有效)
if(row != null && row.getLastCellNum() >= 9){
personBean = new CheckInPersonBean();
setData(row.getCell(0), personBean, "i");
personBean.setUnit(String.valueOf(row.getCell(1)));
personBean.setName(String.valueOf(row.getCell(4)));
setData(row.getCell(5), personBean, "y");
setData(row.getCell(6), personBean, "t");
setData(row.getCell(8), personBean, "t");
personBeanList.add(personBean);
}
}
}
}
}
} catch (Exception e) {
System.err.println(e);
}
return personBeanList;
}
public Object setDataByType(HSSFCell cell,String methodName,Class<?> paramType,Object obj){
try {
Class<?> clazz = obj.getClass();
Method method = clazz.getMethod(methodName,paramType);
switch (cell.getCellType()) {
//0 double, 1 String 3 空白,4 boolean 5 错误
case 0:
if("setId".equals(methodName)) method.invoke(obj, String.valueOf((int)cell.getNumericCellValue()));
break;
case 1: method.invoke(obj, cell.getStringCellValue()); break;
case 3: method.invoke(obj, String.valueOf(cell)); break;
case 4: method.invoke(obj, cell.getBooleanCellValue()); break;
case 5: method.invoke(obj, ""); break;
default:
method.invoke(obj, new SimpleDateFormat("yyyy-MM-dd"),cell);
break;
}
} catch (Exception e) {
}
return obj;
}
public Class<?> getClazz(HSSFCell cell){
switch (cell.getCellType()) {
case 0: return double.class;
case 1: return String.class;
case 3: return String.class;
case 4: return boolean.class;
case 5: return String.class;
default:
break;
}
return Date.class;
}
//setDataByType(row.getCell(0), "setId",getClazz(row.getCell(0)),personBean);
public void setData(HSSFCell cell,CheckInPersonBean bean,String sign){
if("i".equals(sign)){
if(cell.getCellType() == cell.CELL_TYPE_NUMERIC && cell != null){
bean.setId(String.valueOf((int)cell.getNumericCellValue()));
}
}else if("y".equals(sign)){
if(cell != null && cell.getCellType() == cell.CELL_TYPE_STRING){
try {
bean.setWorkDate(new SimpleDateFormat("yyyy-MM-dd").parse((String.valueOf(cell).replace(".", "-"))));
} catch (ParseException e) {
//转换错误,抛出异常
}
}
}else{
if(cell != null && cell.getCellType() == cell.CELL_TYPE_STRING){
try {
bean.setWorkStartTime(new SimpleDateFormat("mm:HH:ss").parse((String.valueOf(cell))));
} catch (ParseException e) {
//转换错误,抛出异常
}
}
}
}
}