@Action(value = "undoexcel",results={@Result(name="success",location="/WEB-INF/jsp/function/billqq.jsp")})
public String undo() throws Exception{
String directory = "/file";
String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);
File target = UploadFile.Upload(upload, uploadFileName, targetDirectory);
List<BillEntity> billList = new ArrayList<BillEntity>();
excelFile = new FileInputStream(target);
Workbook workbook = new HSSFWorkbook(excelFile);
Sheet sheet = workbook.getSheetAt(0);
int rowNum = sheet.getLastRowNum() + 1;
for (int i = 1; i < rowNum; i++) {
BillEntity billentity = new BillEntity();
Row row = sheet.getRow(i);
int cellNum = row.getLastCellNum();
for (int j = 0; j < cellNum; j++) {
Cell cell = row.getCell(j);
String cellValue = null;
switch (cell.getCellType()) { // 判断excel单元格内容的格式,并对其进行转换,以便插入数据库
case 0:
cellValue = String
.valueOf((int) cell.getNumericCellValue());
break;
case 1:
cellValue = cell.getStringCellValue();
break;
case 2:
cellValue = String
.valueOf((int) cell.getNumericCellValue());
break;
case 3:
cellValue = cell.getStringCellValue();
break;
case 4:
cellValue = cell.getStringCellValue();
break;
}
switch (j) {// 通过列数来判断对应插如的字段
case 1:
billentity.setBi_date(cellValue);
break;
case 2:
billentity.setIncome(Integer.valueOf(cellValue));
break;
case 3:
billentity.setExpend(Integer.valueOf(cellValue));
break;
case 4:
billentity.setRemark(cellValue);
break;
}
}
billList.add(billentity);
}
baseService.add(billList);
return "success";
}
@Override
public void add(List<BillEntity> billentity) {
// TODO Auto-generated method stub
Session session=sessionfactory.openSession();
if(billentity.size() > 0){
int sNum = billentity.size();
for(int i=0;i<sNum;i++){
session.saveOrUpdate(billentity.get(i));
}
session.beginTransaction().commit();
}