@RequestMapping(value = "/importMapping", method = RequestMethod.POST) @ResponseBody public void importPos( @RequestParam(value = "file", required = false) CommonsMultipartFile file, HttpServletRequest req, String mappingType, String siteType, String customerId, HttpServletResponse response) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); String errorMsg = "ok"; List<com.winner.model.ExcelErrorVo> errors = new ArrayList<com.winner.model.ExcelErrorVo>(); // 上传文件 获得文件名(全路径) String fileName = file.getOriginalFilename(); if (file.isEmpty()) { map.put("errorMsg", "emptyFile"); JSONObject jo = JSONObject.fromObject(map); response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); response.getWriter().write(jo.toString()); return; } String path = req.getSession().getServletContext().getRealPath("upload"); File targetFile = new File(path, fileName); if (!targetFile.exists()) { targetFile.mkdirs(); } try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } String filePath = path + File.separator + fileName; ArrayList<HashMap<String, Object>> list = new ArrayList<>(); // 07+版本excel上传 if (filePath.indexOf(".xlsx") != -1) { InputStream is = new FileInputStream(filePath); XSSFWorkbook workbook = new XSSFWorkbook(is); int sheetNum = workbook.getNumberOfSheets(); for (int i = 0; i < sheetNum; i++) {//外循环遍历sheet页 XSSFSheet sheet = workbook.getSheetAt(i); for (int rowNum = 1; rowNum < sheet.getLastRowNum() + 1; rowNum++) { XSSFRow row = sheet.getRow(rowNum); HashMap<String, Object> hashMap = new HashMap<>(); hashMap.put("innerName", row.getCell(0).toString());//mappingType为2 hashMap.put("innerId", AesUtil.decrypt(row.getCell(1).toString(), customerId)); String outName = row.getCell(2).toString(); String outId = row.getCell(3).toString(); int dotNum = outName.indexOf(".");//确保为整型 if(dotNum>-1){ outName = outName.substring(0,dotNum); } dotNum = outId.indexOf(".");//确保为整型 if(dotNum>-1){ outId = outId.substring(0,dotNum); } hashMap.put("outName", outName); hashMap.put("outId", outId); hashMap.put("mappingType", mappingType); hashMap.put("siteType", siteType); list.add(hashMap); } } }else if(filePath.indexOf(".xls")!=-1) { InputStream is = new FileInputStream(filePath); HSSFWorkbook workbook = new HSSFWorkbook(is);//new的对象不一样一个是HSSFWorkbook HSSFSheet HSSFRow 07是一个是XSSFWorkbook XSSFSheet XSSFRow int sheetNum = workbook.getNumberOfSheets(); for (int i = 0; i < sheetNum; i++) { HSSFSheet sheet = workbook.getSheetAt(i); String sheetName = workbook.getSheetName(i); if (!"readme_cn".equals(sheetName)) { for (int rowNum = 1; rowNum < sheet.getLastRowNum() + 1; rowNum++) { HashMap<String, Object> hashMap = new HashMap<>(); HSSFRow row = sheet.getRow(rowNum); hashMap.put("innerName", row.getCell(0).toString());//mappingType为2 hashMap.put("innerId", AesUtil.decrypt(row.getCell(1).toString(), customerId)); String outName = row.getCell(2).toString(); String outId = row.getCell(3).toString(); int dotNum = outName.indexOf(".");//确保为整型 if(dotNum>-1){ outName = outName.substring(0,dotNum); } dotNum = outId.indexOf(".");//确保为整型 if(dotNum>-1){ outId = outId.substring(0,dotNum); } hashMap.put("outName", outName); hashMap.put("outId", outId); hashMap.put("mappingType", mappingType); hashMap.put("siteType", siteType); list.add(hashMap); } } } String url = "huike_importMapping"; JSONObject jsonParam = new JSONObject(); jsonParam.put("list", JSON.toJSONString(list)); jsonParam.put("mappingType", mappingType); jsonParam.put("siteType", siteType); jsonParam.put("customerId", customerId); JSONObject result = HttpClient.httpJsonPost(MessageManager.getMsg("apiAddress") + url, jsonParam, false); String statu = result.get("respCode").toString(); targetFile.delete(); if (statu != null && "90001".equals(statu)) { map.put("errorMsg", "ok"); map.put("count", list.size()); } else { map.put("errorMsg", "error"); } JSONObject jo = JSONObject.fromObject(map); response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); response.getWriter().write(jo.toString()); return; } }
poi导入excel 根据文件名区分07(.xlsx)和03 (.xls)两种情况进行解析
猜你喜欢
转载自blog.csdn.net/qq_29883183/article/details/79615882
今日推荐
周排行