package com.bonus.enroll.controller; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.bonus.core.StringHelper; import com.bonus.enroll.beans.EnrollBean; import com.bonus.enroll.service.EnrollService; import com.bonus.sys.AjaxRes; import com.bonus.sys.BaseController; import com.bonus.sys.GlobalConst; import com.bonus.sys.Page; import com.bonus.sys.beans.CheckCard; import com.bonus.sys.beans.UserBean; @Controller @RequestMapping("/backstage/enroll/") public class EnrollController extends BaseController{ @Autowired private EnrollService service; private List enrolist=new ArrayList(); private Map map=new HashMap(); @RequestMapping("list") public String launchList(Model model) { return "/enroll/enrollList"; } //list @RequestMapping("findByPage") @ResponseBody public AjaxRes findByPage(Page page,EnrollBean o){ AjaxRes ar = getAjaxRes(); try { Page result = service.findByPage(o, page); Map p = new HashMap(); p.put("list",result); ar.setSucceed(p); } catch (Exception e) { logger.error(e.toString(),e); ar.setFailMsg(GlobalConst.DATA_FAIL); } return ar; } @RequestMapping(value = "import", method = RequestMethod.POST) @ResponseBody public AjaxRes export(@RequestParam("file") MultipartFile file) { AjaxRes ar = getAjaxRes(); AjaxRes ars = getAjaxRes(); Integer result = 0; int errorNum = 0; int status=0;//此状态的作用是区分是否有异常数据 enrolist.clear(); map.clear(); try { // @RequestParam("file") MultipartFile file 是用来接收前端传递过来的文件 // 1.创建workbook对象,读取整个文档 InputStream inputStream = file.getInputStream(); POIFSFileSystem poifsFileSystem = new POIFSFileSystem(inputStream); HSSFWorkbook wb = new HSSFWorkbook(poifsFileSystem); // 2.读取页脚sheet HSSFSheet hssfSheet = wb.getSheetAt(0); // 判断Sheet是否为空 if (hssfSheet != null) { System.out.println(hssfSheet.getLastRowNum()); // 获得数据的总行数 int totalRowNum = hssfSheet.getLastRowNum(); // 要获得属性 String idcard = ""; //身份证号 // 遍历行row,continue只是终止本次循环,接着还执行后面的循环 for (int rownum = 1; rownum <= hssfSheet.getLastRowNum(); rownum++) { // 获取到每一行 HSSFRow sheetRow = hssfSheet.getRow(rownum); if (sheetRow != null && !isRowEmpty(sheetRow)) { // 获取到每一行 errorNum = rownum; // 获得第i行对象 Row row = hssfSheet.getRow(rownum); if (sheetRow != null && !isRowEmpty(sheetRow)) { // 先去判断 ars = compareSaveData(sheetRow, rownum + 1); if (ars.getRes() == 3) { ar.setFailMsg(ars.getResMsg()); result = 3; status=1; break; } if (ars.getRes() == 4) { ar.setFailMsg(ars.getResMsg()); result = 4; status=1; break; } if (ars.getRes() == 6) { ar.setFailMsg(ars.getResMsg()); result = 6; status=1; break; } } if (result != 6 && result != 3 && result != 4) { //for (int rownums = 1; rownums <= hssfSheet.getLastRowNum(); rownums++) { // 获取到每一行 HSSFRow sheetRows = hssfSheet.getRow(rownum); if (sheetRows != null && !isRowEmpty(sheetRows)) { addSaveData(sheetRows); } // } }else { status=1; } } } if(status == 0) { for(int i=0;i lists = service.finds(beans); Collections.shuffle(lists); String[] seatArr={"1","3","2","4","5", "6","8","7","9", "10", "21","22","23","24","25", "26","27","28","29","30", "11","12","13","14","15", "16","17","20","19","18", "41","42","43","44","45", "46","47","48","49","50", "33","32","31","34","35", "36","37","40","39","38" }; for (int i = 0;i 0) { ar.setFailMsg(loginName+"身份证有重复,导入失败"); ar.setRes(6); return ar; } } catch (Exception e) { logger.error(e.toString(), e); ar.setFailMsg("第" + rownum + "行数据有误,导入失败"); } return ar; } // 判断该行是否为空 public static boolean isRowEmpty(Row row) { for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) { Cell cell = row.getCell(c); if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) { return false; } } return true; } private void addSaveData(HSSFRow sheetRow) throws Exception { // 保存数据到数据库 EnrollBean bean = new EnrollBean(); bean.setName(getValue(sheetRow.getCell(1))); bean.setIdcard(change(getValue(sheetRow.getCell(2)))); bean.setTimes(change(getValue(sheetRow.getCell(3)))); bean.setSpecialty(change(getValue(sheetRow.getCell(4)))); enrolist.add(bean); //service.add(bean); } private static String getValue(HSSFCell hssfCell) { // hssfCell.getCellType() 获取当前列的类型 if(hssfCell ==null) { return ""; } else if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { return String.valueOf(hssfCell.getBooleanCellValue()); } else if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { return String.valueOf(hssfCell.getNumericCellValue()); } else { return String.valueOf(hssfCell.getStringCellValue()); } } // 将double类型字符串转化为整型 private static String change(String str) { if (str.indexOf('.') != -1) { return str.substring(0, str.indexOf('.')); } else { return str; } } public static void main(String[] args) { String str="123"; Map map=new HashMap(); map.put(str, str); map.put(str, str); System.out.println(map.get("1233")); } }