导入excel源文件
controller类
/** * 导入Excel源数据 * * @param file */ @ApiOperation(value = "导入Excel源数据") @RequestMapping("/excelExport") public AjaxResult excelExport(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request) { String fileTitle = request.getParameter("fileTitle"); return toAjax(attRegulationsService.excelExport(file, fileTitle)); }
impl实现类
/** * 导入Excel文件 * * @param file * @return */ @Override @Transactional(rollbackFor = Exception.class) public int excelExport(MultipartFile file, String fileTitle) { if (file.isEmpty()) { throw new ServiceException("文件为空"); } // 读取Excel文件,循环每行每列数据,封装成集合 //getExcelInfo 方法我单独粘在下面 List<SourceDataInfo> excelInfo = ReadExcelUtil.getExcelInfo(file); if (null == excelInfo) { throw new ServiceException("文件格式异常"); } String filename = file.getOriginalFilename(); // 需要什么字段,自己获取,实体类自己定义 try { return attRegulationsMapper.insertExcelExportInfo(attExcelExportEntity); } catch (Exception e) { log.error("插入导入文件详情信息失败"); throw new ServiceException("文件异常"); } }
方法 getExcelINfo
/** * 读EXCEL文件,获取信息集合 * 实体类根据自己的需求定义 * @return */ public static List<SourceDataInfo> getExcelInfo(MultipartFile mFile) { String fileName = mFile.getOriginalFilename();//获取文件名 try { if (!validateExcel(fileName)) {// 验证文件名是否合格 return null; } boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本 if (isExcel2007(fileName)) { isExcel2003 = false; } List<SourceDataInfo> userList = createExcel(mFile.getInputStream(), isExcel2003); return userList; } catch (Exception e) { e.printStackTrace(); } return null; }
方法createExcel
/** * 根据excel里面的内容读取客户信息 * * @param is 输入流 * @param isExcel2003 excel是2003还是2007版本 * @return * @throws IOException */ public static List<SourceDataInfo> createExcel(InputStream is, boolean isExcel2003) { try { Workbook wb = null; if (isExcel2003) {// 当excel是2003时,创建excel2003 wb = new HSSFWorkbook(is); } else {// 当excel是2007时,创建excel2007 wb = new XSSFWorkbook(is); } List<SourceDataInfo> userList = readExcelValue(wb);// 读取Excel里面客户的信息 return userList; } catch (IOException e) { e.printStackTrace(); } return null; }
方法 readExcelValue
/** * 读取Excel里面客户的信息 * * @param wb * @return */ private static List<SourceDataInfo> readExcelValue(Workbook wb) { //默认会跳过第一行标题 // 得到第一个shell Sheet sheet = wb.getSheetAt(0); // 得到Excel的行数 totalRows = sheet.getPhysicalNumberOfRows(); // 得到Excel的列数(前提是有行数) if (totalRows > 1 && sheet.getRow(0) != null) { totalCells = sheet.getRow(0).getPhysicalNumberOfCells(); } List<SourceDataInfo> userList = new ArrayList<SourceDataInfo>(); // 循环Excel行数 for (int r = 1; r < totalRows; r++) { Row row = sheet.getRow(r); if (row == null) { continue; } SourceDataInfo user = new SourceDataInfo(); // 循环Excel的列 for (int c = 0; c < totalCells; c++) { Cell cell = row.getCell(c); if (null != cell) { if (c == 0) { //第一列时间 //如果是纯数字,将单元格类型转为String if (cell.getCellTypeEnum() == CellType.NUMERIC) { cell.setCellType(CellType.STRING); } user.setPunchTime(cell.getStringCellValue());//将单元格数据赋值给user } else if (c == 1) { // 人员名称 if (cell.getCellTypeEnum() == CellType.NUMERIC) { cell.setCellType(CellType.STRING); } user.setName(cell.getStringCellValue()); } else if (c == 2) { // 卡号(暂时不用) if (cell.getCellTypeEnum() == CellType.NUMERIC) { cell.setCellType(CellType.STRING); } } else if (c == 3) { // 考勤编号(人员编号) if (cell.getCellTypeEnum() == CellType.NUMERIC) { cell.setCellType(CellType.STRING); } user.setAttendanceNo(cell.getStringCellValue()); } else if (c == 4) { // 打卡地点 if (cell.getCellTypeEnum() == CellType.NUMERIC) { cell.setCellType(CellType.STRING); } user.setPunchPlace(String.valueOf(cell.getStringCellValue())); } else if (c == 5) { // 打卡方式 if (cell.getCellTypeEnum() == CellType.NUMERIC) { cell.setCellType(CellType.STRING); } user.setPunchMode(cell.getStringCellValue()); } } } //将excel解析出来的数据赋值给对象添加到list中 user.setId(IdWorkerUtil.getId()); user.setCreateTime(LocalDateTime.now()); // 添加到list userList.add(user); } return userList; }
上面这个方法,主要修改这里
在这里处理逻辑 ,返回需要插入的数据
思路:根据你的内容,如果包含“卡号”,或者不包含“卡号”处理
然后再在你的主逻辑里面,写一个插入list<自己的实体类> insert 大功告成!