页面:
<a href="javascript:upload();" class="button float-right"><span class="icon-plus" style="margin-right: 5px;"></span>导入学生</a><section style="display: none" > <div id="uploadFileWrap"> <form method="post" action="${request.contextPath}/admin/excel" id="excelForm" enctype="multipart/form-data"> <input id="fileName" size="100" name="file_Name" type="file"/> </form> </div> </section>
<script src="${request.contextPath}/static/admin/js/assert/jquery.js"></script> <script src="${request.contextPath}/static/admin/js/assert/pintuer.js"></script> <script src="${request.contextPath}/static/admin/js/assert/respond.js"></script> <script src="${request.contextPath}/static/admin/js/assert/jquery.mloading.js" type="text/javascript" charset="utf-8"></script> <script src="${request.contextPath}/static/admin/js/assert/alert.js" type="text/javascript" charset="utf-8"></script> <script src="${request.contextPath}/static/admin/js/vue/vue.min.js" type="text/javascript" charset="utf-8"> </script>
<script type="text/javascript">
function upload() { if (M) { return M.show(); } M = jqueryAlert({ 'style': 'pc', 'title': '确定导入学生', 'content': $("#uploadFileWrap"), 'modal': true, 'contentTextAlign': 'left', 'animateType': 'linear', 'buttons': { '确认': function () { //将excele 表格传入后台处理 // document.getElementById("excelForm").submit(); $(".alert-content #excelForm").submit(); M.close(); }, } }) } </script>
Controller层:
package com.haue.jobonline.controller; import com.haue.jobonline.service.ExcelService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; /** * @author 赵鑫 * @Time: 2018/7/21 * @Email:[email protected] */ @Controller public class ExcelController { @Resource private ExcelService excelService; @RequestMapping(value = "/admin/excel") public String upload(@RequestParam("file_Name") MultipartFile file,Model model) { boolean flag; String name = file.getOriginalFilename(); // File file1 = new File("E:\\" + name); System.out.println(name); if (file.getOriginalFilename().isEmpty() || file.getSize() == 0) { String message="上传失败"; model.addAttribute("m",message); return "/admin/400"; } try { String message=excelService.studentExcel(name,file); if (!message.equals("success")){ model.addAttribute("m",message); return "/admin/400"; } } catch (Exception e) { e.printStackTrace(); return "/admin/404"; } return "redirect:/admin/toStudentListIndex"; } }
Service层:
import org.springframework.web.multipart.MultipartFile; /** * @author 赵鑫 * @Time: 2018/7/21 * @Email:[email protected] */ public interface ExcelService { String studentExcel(String fileName, MultipartFile file) throws Exception; }
import com.haue.jobonline.dao.StudentDao; import com.haue.jobonline.entity.Student; import com.haue.jobonline.service.ExcelService; //import com.haue.jobonline.utils.MD5Utils; import com.haue.jobonline.service.StudentManagerService; import com.haue.jobonline.utils.MD5Utils; import com.haue.jobonline.utils.UUIDUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @author 赵鑫 * @Time: 2018/7/21 * @Email:[email protected] */ @Service @Transactional public class ExcelServiceImpl implements ExcelService { @Resource private StudentManagerService studentManagerService; @Resource private StudentDao studentDao; @Override @Transactional(readOnly = false, rollbackFor = Exception.class) public String studentExcel(String fileName, MultipartFile file) throws Exception { MD5Utils md5Utils =new MD5Utils(); UUIDUtils uuidUtils=new UUIDUtils(); String message = "success"; List<Student> studentList = new ArrayList<Student>(); boolean isExcel2003 = false; //判断excle类型失败 代码 已删除 上面定义的isExcel2003 实际上并没有用 DecimalFormat df = new DecimalFormat("0"); InputStream is=file.getInputStream(); Workbook wb = null; if (isExcel2003) { wb = new HSSFWorkbook(is); } else { wb = new XSSFWorkbook(is); } Sheet sheet = wb.getSheetAt(0); if (sheet == null) { message = "Excel 表为空"; } Student student; System.out.println(sheet.getLastRowNum()); for (int r = 1; r <= sheet.getLastRowNum(); r++) { Row row = sheet.getRow(r); if (row == null) { continue; } student = new Student(); //姓名 if (row.getCell(0).getCellType() != 1) { message="导入失败(第" + (r + 1) + "行,姓名请设为文本格式)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,姓名请设为文本格式)"); } String name = row.getCell(0).getStringCellValue(); if (name == null || name.isEmpty()) { message="导入失败(第" + (r + 1) + "行,姓名未填写)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,姓名未填写)"); } //性别 if (row.getCell(1).getCellType() != 1) { message="导入失败(第" + (r + 1) + "行,性别请设为文本格式"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,性别请设为文本格式)"); } String sex = row.getCell(1).getStringCellValue(); if (sex == null || sex.isEmpty()) { message="导入失败(第" + (r + 1) + "行,性别未填写)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,性别未填写)"); } String level=null; //级别 if (row.getCell(2).getCellType() == 1) { level = row.getCell(2).getStringCellValue(); if (level == null || level.isEmpty()) { message="导入失败(第" + (r + 1) + "行,级别未填写)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,级别未填写)"); } // if (row.getCell(2).getCellType()==0){ // // } // throw new Exception("导入失败(第" + (r + 1) + "行,级别请设为文本格式)"); }else if (row.getCell(2).getCellType() == 0){ level=df.format(row.getCell(2).getNumericCellValue()); if (level == null || level.isEmpty()) { message="导入失败(第" + (r + 1) + "行,级别未填写)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,级别未填写)"); } }else{ message="导入失败(第" + (r + 1) + "行,级别请设为文本格式或数值类型)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,级别请设为文本格式或数值类型"); } System.out.println(level); //班级 // if (row.getCell(3).getCellType() != 1) { // throw new Exception("导入失败(第" + (r + 1) + "行,班级请设为文本格式)"); // } // String studentClass = row.getCell(3).getStringCellValue(); // // if (studentClass == null || studentClass.isEmpty()) { // throw new Exception("导入失败(第" + (r + 1) + "行,班级未填写)"); // } String studentClass=null; if (row.getCell(3)!=null){ if (row.getCell(3)!=null){ if (row.getCell(3).getCellType() == 1) { studentClass = row.getCell(3).getStringCellValue(); if (studentClass == null || studentClass.isEmpty()){ message="导入失败(第" + (r + 1) + "行,班级未填写)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,班级未填写)"); } }else if (row.getCell(3).getCellType() == 0){ studentClass =df.format(row.getCell(3).getNumericCellValue()); if (studentClass == null || studentClass.isEmpty()){ message="导入失败(第" + (r + 1) + "行,班级未填写)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,班级未填写)"); } } } } else{ message="导入失败(第" + (r + 1) + "行,班级请设为文本格式或数值格式))"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,班级请设为文本格式或数值格式)"); } System.out.println(studentClass); //专业 if (row.getCell(4).getCellType() != 1) { message="导入失败(第" + (r + 1) + "行,专业请设为文本格式)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,专业请设为文本格式)"); } String studentSpecially = row.getCell(4).getStringCellValue(); System.out.println(studentSpecially); if (studentSpecially == null || studentSpecially.isEmpty()) { message="导入失败(第" + (r + 1) + "行,专业未填写)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,专业未填写)"); } //学号 String studentNum=null; if (row.getCell(5)!=null){ if (row.getCell(5).getCellType() == 1) { studentNum = row.getCell(5).getStringCellValue(); if (studentNum == null || studentNum.isEmpty()){ message="导入失败(第" + (r + 1) + "行,学号级未填写)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,学号级未填写)"); } }else if (row.getCell(5).getCellType() == 0){ studentNum =df.format(row.getCell(5).getNumericCellValue()); if (studentNum == null || studentNum.isEmpty()){ message="导入失败(第" + (r + 1) + "行,学号级未填写)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,学号未填写)"); } } } else{ message="导入失败(第" + (r + 1) + "行,学号请设为文本格式或数值格式)"; return message; // throw new Exception("导入失败(第" + (r + 1) + "行,学号请设为文本格式或数值格式)"); } System.out.println("姓名:" + name + "---" + "性别:" + sex + "----" + "级别:" + level + "-----" + "班级:" + studentClass + "---" + "专业:" + studentSpecially + "---" + "学号:" + studentNum + ""); student.setStudentName(name); student.setStudentSex(sex); student.setStudentLevel(level); student.setStudentClass(studentClass); student.setStudentSpecialty(studentSpecially); student.setStudentSchoolNum(studentNum); student.setStudentLoginPassword(md5Utils.GetMD5Code(studentNum)); student.setStudentId(uuidUtils.getUUID()); //查询表中是否有重复学号的 如果有 给出提示 数据全部不导入 List<Student> list=studentDao.findAll(); for (int i=0;i<list.size();i++){ System.out.println(list.get(i).getStudentSchoolNum()); if (list.get(i).getStudentSchoolNum().equals(studentNum)){ return "学号为"+studentNum+"的学生已存在"; } } studentList.add(student); } for (Student s :studentList){ studentManagerService.addStudent(s); } System.out.println(message); return message; } }