struts2上传学生 csv数据文件 到数据库
上传的jsp页面 <%@ page contentType="text/html;charset=UTF-8" %> <%@ include file="/WEB-INF/page/share/taglib.jsp" %> <%@ include file="/WEB-INF/page/share/studentSelectAll.jsp"%>
InputStream in = null;
csvList = new ArrayList();
try {
in = new FileInputStream(file);
CsvReader reader = new CsvReader(in, Charset.forName("utf-8"));
try {
//读取CSV文件的表头,如果CSV文件没有表头则可以注释 掉
/// reader.readHeaders();
while (reader.readRecord()) {
columnCount = reader.getColumnCount();
csvList.add(reader.getValues());
}
reader.close();
list=new ArrayList();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//遍历CSV文件中的信息
for (int row = 0; row < csvList.size(); row++) {
Student student = new Student();
student.setEditMark(false);
student.setCommitMark(false);
student.setUserPwd("0");
student.setStuBirthCode("32233232");
for (int i = 0; i < columnCount; i++) {
String cell = csvList.get(row)*;
/// System.out.println(cell);
switch(i){
case 0: student.setUserName(cell); break;
case 1: student.setStuID(cell); break;
case 2: student.setStuIdCard(cell); break;
case 3: student.setStuGrade(Integer.valueOf(cell));break;
case 4: student.setStuBirthPlace(cell); break;
case 5: student.setStuDegree(cell); break;
case 6: student.setStuDepartment(cell); break;
case 7: student.setStuEduStu(cell); break;
case 8: student.setStuEduWay(cell); break;
case 9: student.setStuName(cell); break;
case 10:student.setStuParty(cell); break;
case 11:student.setStuPhoneNum(cell); break;
case 12:student.setStuPlace(cell); break;
case 13:student.setStuPost(cell); break;
case 14:student.setStuSex(cell); break;
case 15:student.setStuSpecial(cell); break;
case 16:student.setStuInYear(sdf.parse(cell)); break;
case 17:student.setStuOutYear(sdf.parse(cell)); break;
}
}
list.add(student);
}
} catch (IOException e1) {
e1.printStackTrace();
csvList=null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
csvList=null;
}
for(Student student:list){
System.out.println("学号为:"+student.getUserName());
System.out.println("生源地:"+student.getStuBirthPlace());
System.out.println("学位:"+student.getStuDegree());
}
studentService.setStudents(list);
处理成功后的跳转 ServletActionContext.getRequest().setAttribute("message", "上传学生基本信息成功"); ServletActionContext.getRequest().setAttribute("urladdress", SiteUrl.readUrl("control.student.list")); return "message";
csv处理数据 按照程序读取的列顺序排好数据,然后将excel另存为csv格式的文件,用editBus编辑文件的编码方式为utf-8,数据获取格式也是utf-8