主要方法:
/**
* 学生信息导入
*
* @throws Exception
*/
public String serInfoImport() throws Exception {
// 返回各种学生信息
String message = "";
majorList = majorDao.getAll();// 得到所有专业列表
yearList = yearDao.getAll(); // 取得所有的年纪列表
setClassListToRequest(majorDao, yearDao, classesDao, majorId, yearId);
if (classesId == 0) {
super.setToRequest("message", "班级未选择,请重新选择班级后导入!");
returnmsg = "班级未选择,请重新选择班级后导入";
return "errorImport";
}
tempClass = classesDao.findUniqueBy(Restrictions.eq("id", classesId));
if (tempClass == null || tempClass.getId() == null) {
super.setToRequest("message", "选择的班级不存在,请不要恶意破坏!");
returnmsg = "选择的班级不存在,请不要恶意破坏!";
return "errorImport";
}
// 导入信息的学生列表
List<Users> importedUsers = new ArrayList<Users>();
// 字段
List<String> segment = new ArrayList<String>();
Workbook workbook = null;
String fileName = System.currentTimeMillis() + "_"
+ getUploadFileName();
String filePath = ServletActionContext.getServletContext().getRealPath(
"")
+ File.separator + "uploadfiles" + File.separator + fileName;
FileUploadUtil.uploadFile(filePath, getUpload());
workbook = Workbook.getWorkbook(new File(filePath));
Sheet sheet = workbook.getSheet(0);
int col = sheet.getColumns();
int row = sheet.getRows();
// 开始学生信息导入逻辑
// 得到字段,如学号、姓名、性别等字段名,用于结果显示
for (int z = 0; z < col; z++) {
segment.add(sheet.getCell(z, 0).getContents().trim());
}
// 一行一个学生的完整信息,下面一段代码为得到表中所有学生信息
for (int i = 1; i < row; i++) {
Users user = new Users();
List<String> result = new ArrayList<String>();
// 得到单行学生信息,填充result
for (int j = 0; j < col; j++) {
Cell cellContent = sheet.getCell(j, i);
result.add(cellContent.getContents().trim());
}
// 如果学号为空,那么无法比较信息,故对该学生信息不予操作,进行下一个学生信息的处理
if (result.get(0).toString() == "") {
continue;
} else {
Users tempUser = usersDao.findUniqueBy(Restrictions.eq(
"number", result.get(0)));
String password = Md5Util.md5(result.get(0));
try {
if (tempUser == null) {
user.setNumber(result.get(0));
user.setName(result.get(1));
user.setSex(result.get(2));
user.setBirthPlace(result.get(3));
user.setNation(result.get(4));
user.setBirthDate(result.get(5));
user.setIDCard(result.get(6));
user.setGraduateSchool(result.get(7));
user.setBedroom(result.get(8));
user.setPolitics(result.get(9));
user.setMobile(result.get(10));
user.setBankCard(result.get(11));
user.setClasses(tempClass);
user.setPassword(password);
user.setStatus(1);
user.setParent(saveParent(user));// 保存家长
usersDao.save(user);
importedUsers.add(user);
} else {
tempUser.setNumber(result.get(0));
tempUser.setName(result.get(1));
tempUser.setSex(result.get(2));
tempUser.setBirthPlace(result.get(3));
tempUser.setNation(result.get(4));
tempUser.setBirthDate(result.get(5));
tempUser.setIDCard(result.get(6));
tempUser.setGraduateSchool(result.get(7));
tempUser.setBedroom(result.get(8));
tempUser.setPolitics(result.get(9));
tempUser.setMobile(result.get(10));
tempUser.setBankCard(result.get(11));
tempUser.setClasses(tempClass);
tempUser.setStatus(1);
usersDao.save(tempUser);
importedUsers.add(tempUser);
}
} catch (Exception e) {
System.out.println(e.getMessage());
returnmsg = "格式不对!";
return "errorImport";
}
}
}
// 学生信息导入结束
@SuppressWarnings("unused")
boolean suc = (new File(filePath)).delete();
super.setToRequest("message", message);
super.setToRequest("segment", segment);
super.setToRequest("importedUsers", importedUsers);
return "successImport";
}