@PostMapping(value = "/uploadFenceMmsi") public ResponseBean uploadFenceMmsi(@RequestParam("file") MultipartFile file, @RequestParam("fenceId") Long fenceId, HttpServletRequest request) { try { if (file.isEmpty()) { return new ResponseBean(ResponseCode.FAIL, "文件为空"); } // 获取文件名 String fileName = file.getOriginalFilename(); log.info("上传的文件名为:" + fileName); // 获取文件的后缀名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); //创建一个临时文件,用于暂时存放 List<List<String>> result = excelOptionsService.writeWithoutHead(new ByteArrayInputStream(file.getBytes())); //上传完成 删除临时文件 tmpFile.delete(); if (CollectionUtils.isNotEmpty(result)) { List<TbFenceMmsiReqVO> list = new ArrayList<>(); TbFenceMmsiReqVO fenceMmsi = null; for (List<String> strings : result) { boolean isNumDigits = StringUtils.isNumeric(strings.get(0)); if (isNumDigits) { fenceMmsi = new TbFenceMmsiReqVO(); fenceMmsi.setMmsi(Long.parseLong(strings.get(0))); fenceMmsi.setShipName(strings.get(1)); list.add(fenceMmsi); } else { log.error("无法转化:" + JSON.toJSONString(result)); } } if (CollectionUtils.isNotEmpty(list)) { BatchInsertFenceMmsiReqVO reqVO = new BatchInsertFenceMmsiReqVO(); reqVO.setFenceId(fenceId); reqVO.setUserId(user.getId()); reqVO.setSysLabel(OKHttpUtil.SYS_LABEL); reqVO.setList(list); Response httpResp = okHttpUtil.postOkHttp(JSON.toJSONString(reqVO), systemVariableConstant.getFenceUrl() + "fenceMmsi/batchInsertFenceMmsi"); if(httpResp == null){ return new ResponseBean(ResponseCode.FAIL, "addFence http failed !!!"); } String res = httpResp.body().string(); logger.error("http resp === " + res); if (res.contains("成功")) { JSONObject json = new JSONObject(); json.put("success", true); json.put("msg", "上传成功"); return new ResponseBean(ResponseCode.SUCCESS, json); } else if (res.contains("999")) { JSONObject resObject = JSONObject.parseObject(res); JSONObject json = new JSONObject(); json.put("success", false); json.put("msg", resObject.getString("result")); return new ResponseBean(ResponseCode.SUCCESS,messageFailureWrap(resObject.getString("result"))); } } else { return new ResponseBean(ResponseCode.FAIL, "解析出来数据为空"); } } } catch (Exception e) { log.error("解析报错====:" + e); } return new ResponseBean(ResponseCode.FAIL, "上传失败"); } //获取导入模板 @GetMapping(value = "/downloadTemple") public void downloadTemple(HttpServletRequest request, HttpServletResponse response) throws IOException { Map<String, String> map = getParameterMap(request); //创建HSSFWorkbook对象(excel的文档对象) HSSFWorkbook wb = new HSSFWorkbook(); //建立新的sheet对象(excel的表单) HSSFSheet sheet=wb.createSheet("船舶"); HSSFCellStyle cellStyle = wb.createCellStyle(); HSSFFont font = wb.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 13);//设置字体大小 cellStyle.setFont(font); cellStyle.setAlignment(HorizontalAlignment.CENTER); HSSFRow row0=sheet.createRow(0); row0=sheet.createRow(0);//第二行 row0.createCell(0).setCellValue("Mmsi"); row0.createCell(1).setCellValue("船名"); // int rowNum = 1; // List<ShoreBasedType> list = shoreBasedTypeService.getAll(); for (int i = 0; i < 1; i++) { row0=sheet.createRow(1);//第二行 HSSFCell cell = row0.createCell(1); cell.setCellStyle(cellStyle); cell.setCellValue("123"); row0.createCell(2).setCellValue("123"); } //输出Excel文件 OutputStream output= response.getOutputStream(); response.reset(); response.setHeader("Content-disposition", "attachment; filename="+ URLEncoder.encode("船舶模板.xls", "UTF-8")); response.setContentType("application/msexcel"); wb.write(output); output.close(); }
Java EasyExcel 导出
最新推荐文章于 2024-08-25 16:29:44 发布