HTML层:
<iframe name="message" id="message" style="display: none"></iframe> <form id="Form2" action="MessageGroup/ImportExcel" method="post" enctype="multipart/form-data" target="message" > <input type="file" name="file"/> <input id="formSubmit" type="button" value="提交" /> </div>
js层:
$("#formSubmit").unbind('click').click(function () { $("#Form2").submit(); $('#message').unbind('load').on('load',function () { var res = $("#message").contents().find('body').text(); if(res ==1){ alert("导入成功!"); getSelectPageList(); }else{ alert("导入失败!"); } }); });
controller层:
/** * * @Description: 导入Excel文件 * @return */ @RequestMapping( value = "/ImportExcel") @ResponseBody public int readExcel(@RequestParam("file") CommonsMultipartFile file) throws IOException { ImportExcelUtil importExcelUtil = new ImportExcelUtil(); String path="E:/"+new Date().getTime()+file.getOriginalFilename(); File newFile=new File(path); file.transferTo(newFile); List<Map<String,String>> list = importExcelUtil.readExcel(newFile); List<Map<String,Object>> personnelMobile = messageGroupUserService.quarryPersonnelMobile(groupId); Map<Object,Object> map=new HashMap<>(); for(int j=0;j<personnelMobile.size();j++){ map.put(personnelMobile.get(j).get("personnel_mobile"),personnelMobile.get(j).get("group_id")); } List<MessageGroupUser> messageGroupUserList =new ArrayList<>(); for(int i = 0;i<list.size();i++){ MessageGroupUser messageGroupUser = new MessageGroupUser(); if(list.get(i).get("cell1") == null || "".equals(messageGroupUser.getPersonnelMobile())){ continue; } if (map.get(list.get(i).get("cell1")) != null && map.get(list.get(i).get("cell1")).equals(groupId)){ continue; } messageGroupUser.setGroupId(groupId); messageGroupUser.setPersonnelMobile(list.get(i).get("cell1")); messageGroupUser.setPersonnelName(list.get(i).get("cell0")); messageGroupUserList.add(messageGroupUser); } messageGroupUserService.insert(messageGroupUserList); return 1; }
工具类:
/** * * @ClassName: ImportExcelUtil * @Description: 读取Excel工具类 * @author fumw * @date 2017年9月20日 */ @Component public class ImportExcelUtil { private final static String excel2003L =".xls"; //2003- 版本的excel private final static String excel2007U =".xlsx"; //2007+ 版本的excel /** * 根据url读取Excel数据 * @return List<Map<String,String>> 表格每一行数据在一个List中,每一单元格存在List中的map中 * 每一行中的单元格依次为cell0,cell1,cell2.... * @Throws IOException */ public List<Map<String,String>> readExcel(File file) throws IOException { if(file != null){ FileInputStream fis = new FileInputStream(file); String fileName = file.getName(); String fileType = fileName.substring(fileName.lastIndexOf(".")); List<Map<String,String>> list = new ArrayList<>(); if(excel2003L.equals(fileType)){ HSSFWorkbook workbook = new HSSFWorkbook(fis); //2003- HSSFSheet sheet = workbook.getSheetAt(0); //从第二行开始循环拿数据 for(int i=1;i<=sheet.getLastRowNum();i++){ HSSFRow xr = sheet.getRow(i); Map<String,String> map = new HashMap<>(); //从此行第一列开始循环拿数据 for(int j=0;j<xr.getLastCellNum();j++){ HSSFCell codeCell = xr.getCell(j); codeCell.setCellType(XSSFCell.CELL_TYPE_STRING); //设置map,格式为cellX(X为此单元格的列数),值 if(codeCell.getStringCellValue().equals("-")){ map.put("cell"+j,null); } else{ map.put("cell"+j,codeCell.getStringCellValue()); } } list.add(map); } return list; } else if(excel2007U.equals(fileType)){ XSSFWorkbook workbook = new XSSFWorkbook(fis); //2007+ XSSFSheet sheet = workbook.getSheetAt(0); //从第二行开始循环拿数据 for(int i=1;i<=sheet.getLastRowNum();i++){ XSSFRow xr = sheet.getRow(i); Map<String,String> map = new HashMap<>(); //从此行第一列开始循环拿数据 for(int j=0;j<xr.getLastCellNum();j++){ XSSFCell codeCell = xr.getCell(j); codeCell.setCellType(XSSFCell.CELL_TYPE_STRING); //设置map,格式为cellX(X为此单元格的列数),值 if(codeCell.getStringCellValue().equals("-")){ map.put("cell"+j,null); } else{ map.put("cell"+j,codeCell.getStringCellValue()); } } list.add(map); } return list; }else{ return null; } }else { return null; } } }