Java EasyExcel 导出

@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();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值