/**
* excel导入学生基本信息
*/
public String impStuBaseInfo(File stuFile, Student student)
throws AppBaseException, ParseException {
String result = "";
List clientList = new ArrayList();
InputStream is = null;
try {
is = new FileInputStream(stuFile);
} catch (FileNotFoundException e) {
result = "找不到指定的文件。";
e.printStackTrace();
return result;
}
// 工作薄
Workbook rwb = null;
try {
rwb = Workbook.getWorkbook(is);
} catch (Exception e) {
result = "您提交的文件不是有效的Excel文档。";
e.printStackTrace();
return result;
}
Sheet organSheet1 = rwb.getSheet(0);
// 基本信息
result = this.buildStuBaseInfoFromSheet(clientList, organSheet1,
student);
if (!result.equals("")) {
return result;
}
// 插入或修改
try {
insertStuBaseInfo(clientList);
} catch (Exception e) {
e.printStackTrace();
}
rwb.close();
return result;
}
//调用的方法
public String buildStuBaseInfoFromSheet(List clientList, Sheet sheet,
Student student) {
String result = "";
int sheetRows = sheet.getRows();
for (int i = 1; i < sheetRows; i++) {
student = new Student();
if (this.checkAllColumnsEmpty(sheet, i, 8) == true) {
continue;
}
// 得到列
Cell c0 = sheet.getCell(0, i);
String content = null;
content = c0.getContents();
student.setId(Integer.parseInt(content));
c0 = sheet.getCell(1, i);
content = c0.getContents();
student.setName(content);
c0 = sheet.getCell(2, i);
content = c0.getContents();
student.setAge(Integer.parseInt(content));
c0 = sheet.getCell(3, i);
content = c0.getContents();
System.out.println(content.toString());
student.setSex(content);
c0 = sheet.getCell(4, i);
content = c0.getContents();
student.setScore(Integer.parseInt(content));
c0 = sheet.getCell(5, i);
content = c0.getContents();
student.setGrade(content);
c0 = sheet.getCell(6, i);
content = c0.getContents();
student.setCnum(Integer.parseInt(content));
c0 = sheet.getCell(7, i);
content = c0.getContents();
student.setCname(content);
clientList.add(student);
}
return result;
}
public void insertStuBaseInfo(List list) throws Exception {
Iterator it = list.iterator();
while (it.hasNext()) {
Student model = (Student) it.next();
insertStuBasicInfo(model);
}
}
public void insertStuBasicInfo(Student model) {
this.dao.getSqlMapClientTemplate().insert("addStudent", model);
this.dao.getSqlMapClientTemplate().insert("addScore", model);
}