@GetMapping("/export")
    @ApiOperation(value = "导出", notes = "导出")
    public void export(HttpServletResponse response) throws IOException {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("aaa.xlsx", "UTF-8"));

            // 查询条件
            TradeEntInfoDto dto = new TradeEntInfoDto();
            dto.setAuditStatus("06");
            dto.setPageSize(-1);
            MybatisPlusPager<TradeEntInfoVo> data = tradeEntInfoService.page(dto);

            EasyExcel.write(response.getOutputStream(), TradeEntInfoVo.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    .sheet("sheet1").doWrite(data.getRecords());
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, Object> map = new HashMap<>(2);
            map.put("code", ResultCode.CODE_C0400.getCode());
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JacksonUtil.mapToJson(map));
        }
    }

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐