import org.apache.poi.ss.usermodel.Cell; //導入方法依賴的package包/類
/**
* 讀取Excel裏麵客戶的信息
* @param wb
* @return
*/
private Map readInfoValue(Workbook wb) {
//得到第一個shell
Sheet sheet = wb.getSheetAt(0);
Map modelMap = new HashMap<>();
//得到Excel的行數
this.totalRows = sheet.getPhysicalNumberOfRows();
//得到Excel的列數(前提是有行數)
if (totalRows >= 1 && sheet.getRow(0) != null) {
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
}
List infoList = new ArrayList();
InfoEntity info;
List errorList = new ArrayList();
ErrorJson error;
//循環Excel行數,從第二行開始。標題不入庫
for (int r = 1; r < totalRows; r++) {
Row row = sheet.getRow(r);
if (row == null) continue;
info = new InfoEntity();
//循環Excel的列
for (int c = 0; c < this.totalCells; c++) {
Cell cell = row.getCell(c);
if (null != cell) {
if (c == 0) {
cell.setCellType(Cell.CELL_TYPE_STRING);
info.setTitle(cell.getStringCellValue());//title
} else if (c == 1) {
cell.setCellType(Cell.CELL_TYPE_STRING);
info.setContext(cell.getStringCellValue());//context
} else if (c == 2) {
Date ti =new Date();
Timestamp time = new Timestamp(ti.getTime());
info.setTime(time);//time
} else if (c == 3) {
cell.setCellType(Cell.CELL_TYPE_STRING);
info.setSender(cell.getStringCellValue());//sender
}
else if (c == 4) {
info.setStudentId((int) cell.getNumericCellValue());//學號
}
}
}
//添加學生
System.out.println(info);
if (info.getStudentId()!=0&&info.getTitle()!=""&&info.getContext()!=""&&info.getTime()!=null&&info.getSender()!="") {
infoList.add(info);
} else if (info.getStudentId()==0) {
error = new ErrorJson();
error.setId(info.getId());
error.setErrors("學號不能為空");
errorList.add(error);
} else if ( info.getTitle()=="") {
error = new ErrorJson();
error.setId(info.getId());
error.setErrors("title不能為空");
errorList.add(error);
} else if (info.getId() != 0 && info.getContext() == "") {
error = new ErrorJson();
error.setId(info.getId());
error.setErrors("內容不能為空");
errorList.add(error);
} else if (info.getId() != 0 && info.getSender() == "") {
error = new ErrorJson();
error.setId(info.getId());
error.setErrors("發送者不能為空");
errorList.add(error);
}
}
modelMap.put("result", infoList);
modelMap.put("error", errorList);
return modelMap;
}