🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 一、概述 提供一个样板,用于标准化的处理文件导出数据的场景; ## 二、方案 ### **controller**: ``` @ApiOperation(value = "导出日志列表", notes = "导出当前所有的日志列表") // @ApiOperationSupport(order = 5) @ApiImplicitParams({ @ApiImplicitParam(name = RayConstants.rayAccessToken, value = "授权token", required = true, dataTypeClass = String.class, paramType = "query") // }) @GetMapping("/api/system/log/export/list") @RayEscapeTokenValidateControllerMethod @RayEscapeRequestHeaderValidateControllerMethod @RayValidateTokenInParamControllerMethod public void exportLogList(@RequestParam String rayAccessToken) throws Exception { sysLogService.exportLogList(); } ``` >[danger] 注意, > 1、这里必须增加的注解: > @RayEscapeTokenValidateControllerMethod > @RayEscapeRequestHeaderValidateControllerMethod > @RayValidateTokenInParamControllerMethod >2、必须使用@GetMapping ### **service**: ``` @RayServiceMethod public void exportLogList() throws Exception { excelExportProcessor.export(response, "日志列表.xlsx", new ExportExcelCallback() { @Override public String buildExcelObject(Sheet sheet, Object... exportedDataObjects) { List<SysLog> dataList = (List<SysLog>) exportedDataObjects[0]; int excelRow = 0; createLogTitle(sheet); if (dataList != null && dataList.size() > 0) { for (int i = 0; i < dataList.size(); i++) { Row dataRow = sheet.createRow(i + 1); Cell cell0 = dataRow.createCell(0); Cell cell1 = dataRow.createCell(1); Cell cell2 = dataRow.createCell(2); Cell cell3 = dataRow.createCell(3); cell0.setCellValue(i + 1); cell1.setCellValue(dataList.get(i).getInfoCode()); cell2.setCellValue(dataList.get(i).getOperateTime()); } } return null; } }, sysLogDao.findAll()); } ``` ### **前端使用**: 按照接口定义,拼凑URL地址,包括token,带上两个安全参数,再按照接口定义的参数请求,构成了完整的url,然后模拟点击url,即可获得生成的导出文件;