首先准备一个form表单
注意表单写法:
重点:enctype=“multipart/form-data”
第二步:在页面上加上
第三步:
在servlet写入相对应的方法
例如我需要把学生id(studentId)和课程id(courseId)还有score、remark导入
private void importScore(HttpServletRequest request, HttpServletResponse response) {
FileUpload fileUpload = new FileUpload(request);
fileUpload.setFileFormat(“xls”);
fileUpload.setFileFormat(“xlsx”);
fileUpload.setFileSize(2048);
response.setCharacterEncoding(“UTF-8”);
try {
InputStream uploadInputStream = fileUpload.getUploadInputStream();
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(uploadInputStream);
//这里是得到sheet,每一个工作表
HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
int count = 0;
String errorMsg = “”;
StudentDao studentDao = new StudentDao();
CourseDao courseDao = new CourseDao();
ScoreDao scoreDao = new ScoreDao();
SelectedCourseDao selectedCourseDao = new SelectedCourseDao();
for(int rowNum = 1; rowNum <= sheetAt.getLastRowNum(); rowNum++){
HSSFRow row = sheetAt.getRow(rowNum);
HSSFCell cell = row.getCell(0);
//获取第0列,学生id
if(cell == null){
errorMsg += “第” + rowNum + “行学生id缺失!\n”;
continue;
}
if(cell.getCellType() != cell.CELL_TYPE_NUMERIC){
errorMsg += “第” + rowNum + “行学生id类型不是整数!\n”;
continue;
}
int studentId = new Double(cell.getNumericCellValue()).intValue();
//获取第1列,课程id
cell = row.getCell(1);
if(cell == null){
errorMsg += “第” + rowNum + “行课程id缺失!\n”;
continue;
}
if(cell.getCellType() != cell.CELL_TYPE_NUMERIC){
errorMsg += “第” + rowNum + “行课程id不是整数!\n”;
continue;
}
int courseId = new Double(cell.getNumericCellValue()).intValue();
//获取第2列,成绩
cell = row.getCell(2);
if(cell == null){
errorMsg += “第” + rowNum + “行成绩缺失!\n”;
continue;
}
if(cell.getCellType() != cell.CELL_TYPE_NUMERIC){
errorMsg += “第” + rowNum + “行成绩类型不是数字!\n”;
continue;
}
double scoreValue = cell.getNumericCellValue();
//获取第3列,备注
cell = row.getCell(3);
String remark = null;
if(cell != null){
remark = cell.getStringCellValue();
}
Student student = studentDao.getStudent(studentId);
if(student == null){
errorMsg += “第” + rowNum + “行学生id不存在!\n”;
continue;
}
Course course = courseDao.getCourse(courseId);
if(course == null){
errorMsg += “第” + rowNum + “行课程id不存在!\n”;
continue;
}
if(!selectedCourseDao.isSelected(studentId, courseId)){
errorMsg += “第” + rowNum + “行课程该同学未选,不合法!\n”;
continue;
}
if(scoreDao.isAdd(studentId, courseId)){
errorMsg += “第” + rowNum + “行成绩已经被添加,请勿重复添加!\n”;
continue;
}
Score score = new Score();
score.setCourseId(courseId);
score.setRemark(remark);
score.setScore(scoreValue);
score.setStudentId(studentId);
if(scoreDao.addScore(score)){
count++;
}
}
errorMsg += “成功录入” + count + “条成绩信息!”;
studentDao.closeCon();
courseDao.closeCon();
selectedCourseDao.closeCon();
scoreDao.closeCon();
try {
response.getWriter().write("
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (ProtocolException e) {
// TODO Auto-generated catch block
try {
response.getWriter().write("<div id='message'>上传协议错误!</div>");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}catch (NullFileException e1) {
// TODO: handle exception
try {
response.getWriter().write("<div id='message'>上传的文件为空!</div>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
e1.printStackTrace();
}
catch (SizeException e2) {
// TODO: handle exception
try {
response.getWriter().write("<div id='message'>上传文件大小不能超过"+fileUpload.getFileSize()+"!</div>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
e2.printStackTrace();
}
catch (IOException e3) {
// TODO: handle exception
try {
response.getWriter().write("<div id='message'>读取文件出错!</div>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
e3.printStackTrace();
}
catch (FileFormatException e4) {
// TODO: handle exception
try {
response.getWriter().write("<div id='message'>上传文件格式不正确,请上传 "+fileUpload.getFileFormat()+" 格式的文件!</div>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
e4.printStackTrace();
}
catch (FileUploadException e5) {
try {
response.getWriter().write("<div id='message'>上传文件失败!</div>");
} catch (IOException e) {
e.printStackTrace();
}
e5.printStackTrace();
}
}