| | |
| | | } |
| | | |
| | | /** |
| | | * 通用异步导出方法(支持动态类型) |
| | | * |
| | | * @param clazz 导出实体类 |
| | | * @param exportKey 导出任务的唯一标识 |
| | | * @param sheetName 工作表名称 |
| | | * @param dataFetcher 数据获取函数,接受分页参数返回数据列表 |
| | | */ |
| | | @Async |
| | | public <R> void export2(Class<R> clazz, String exportKey, String sheetName, Function<Integer, List<R>> dataFetcher) { |
| | | String fileName = ExcelUtil.encodeFileName(sheetName); |
| | | |
| | | // 设置当前任务为"下载中"状态 |
| | | DownloadExportUtil.deleteDownloadFile(redisCache, exportKey, "0"); |
| | | |
| | | try { |
| | | // 执行导出并获取文件名 |
| | | fileName = exportData2(clazz, fileName, sheetName, dataFetcher); |
| | | // 设置下载完成状态 |
| | | DownloadExportUtil.setDownloadFile(redisCache, exportKey, fileName); |
| | | logger.info("Export completed for key: {}, file: {}", exportKey, fileName); |
| | | } catch (Exception e) { |
| | | logger.error("Export failed for key: {}, error: {}", exportKey, e.getMessage(), e); |
| | | DownloadExportUtil.deleteDownloadFile(redisCache, exportKey, "1"); // 设置失败状态 |
| | | throw e; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 通用分页导出逻辑 |
| | | * |
| | | * @param fileName 文件名 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 通用分页导出逻辑(支持动态类型) |
| | | * |
| | | * @param fileName 文件名 |
| | | * @param sheetName 工作表名称 |
| | | * @param dataFetcher 数据获取函数 |
| | | * @return 导出后的文件名 |
| | | */ |
| | | protected <R> String exportData2(Class<R> clazz, String fileName, String sheetName, |
| | | Function<Integer, List<R>> dataFetcher) { |
| | | ExcelUtil<R> excelUtil = new ExcelUtil<>(clazz); |
| | | excelUtil.initialize(sheetName, null, Excel.Type.EXPORT); |
| | | |
| | | int pageNum = 1; |
| | | boolean hasMoreData = true; |
| | | |
| | | while (hasMoreData) { |
| | | List<R> pageData = dataFetcher.apply(pageNum); |
| | | |
| | | if (pageData != null && !pageData.isEmpty()) { |
| | | // 导出当前页的数据 |
| | | excelUtil.exportExcel(pageData); |
| | | pageNum++; |
| | | } else { |
| | | // 没有数据时退出 |
| | | hasMoreData = false; |
| | | } |
| | | |
| | | } |
| | | |
| | | excelUtil.finishExport(fileName); |
| | | return fileName; |
| | | } |
| | | |
| | | /** |
| | | * 导出两个不同sheet的数据到同一个Excel文件 |
| | | * |
| | | * @param clazz1 第一个sheet对应的实体类 |