自个的excel导入备份

自个用的

public List<ResourceCWD> loaddata1(String filename) throws RuntimeException{
FileInputStream fis = null;
XSSFWorkbook xwb = null;
List<ResourceCWD> dataList = new ArrayList<ResourceCWD>();

try {
fis = new FileInputStream(filename);
xwb = new XSSFWorkbook(fis);
XSSFSheet resourceSheet = null;
XSSFRow resourcerow = null;

//To parse the start_date and Effective_end_date
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);

resourceSheet = xwb.getSheet(sheetname1);

log.info("Retrieve file: [" + filename + " ]");

for (int i = resourceSheet.getFirstRowNum() + 1; i < resourceSheet
.getPhysicalNumberOfRows(); i++) {
ResourceCWD res = new ResourceCWD();
resourcerow = resourceSheet.getRow(i);
if (resourcerow.getCell(0) == null)
break;

res.setAm(resourcerow.getCell(0) == null ? "" : resourcerow
.getCell(0).getStringCellValue());
res.setP_start_date(resourcerow.getCell(1) == null ? null
: resourcerow.getCell(1).getDateCellValue());
res.setProject_end_date(resourcerow.getCell(2) == null ? null
: resourcerow.getCell(2).getDateCellValue());
res.setWon(resourcerow.getCell(3) == null ? null : resourcerow
.getCell(3).getRawValue());
res.setProject_name(resourcerow.getCell(4) == null ? ""
: resourcerow.getCell(4).getStringCellValue());
res.setEmp_no(resourcerow.getCell(5) == null ? null : resourcerow
.getCell(5).getRawValue());
res.setEmp_name(resourcerow.getCell(6) == null ? "" : resourcerow
.getCell(6).getStringCellValue());
res.setAllocation_start_date(resourcerow.getCell(7) == null ? null
: resourcerow.getCell(7).getDateCellValue());
res.setAllocation_end_date(resourcerow.getCell(8) == null ? null
: resourcerow.getCell(8).getDateCellValue());
res.setPm(resourcerow.getCell(10) == null ? "" : resourcerow
.getCell(10).getRawValue());
res.setCwd_name(resourcerow.getCell(11) == null ? "" : resourcerow
.getCell(11).getRawValue());
res.setSso_id(resourcerow.getCell(12) == null ? "" : resourcerow
.getCell(12).getRawValue());
res.setStart_date(resourcerow.getCell(15) == null ? null : sdf
.parse(resourcerow.getCell(15).getRawValue()));
res.setEff_end_date(resourcerow.getCell(16) == null ? null : sdf
.parse(resourcerow.getCell(16).getRawValue()));
dataList.add(res);
}

} catch (ParseException e) {
e.printStackTrace();
//log.error(e.toString());
throw new RuntimeException("Excel Format error!");
} catch (FileNotFoundException e) {
e.printStackTrace();
log.error(e.toString());
throw new RuntimeException("File Not Found!");
} catch (IOException e) {
e.printStackTrace();
log.error(e.toString());
throw new RuntimeException("I/O Exception!");
}finally{
try {
fis.close();
} catch (IOException e) {
log.error(e.getMessage());
throw new RuntimeException("Excel Close Exception!");
}
}
log.info("Retrieve file: [" + filename + " ] success!");

return dataList;

}



你可能感兴趣的:(Excel)