🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 一、概述 导出的数据量特别大的时候,系统需要消耗大量计算时间的情况下,可以采用异步导出数据的方案来提升用户体验; 平台提供了完整的解决方案来实现异步导出机制; ## 二、方案 参考异步任务的技术特性:[后端框架/SprigBoot/核心技术/异步任务](%E5%90%8E%E7%AB%AF%E6%A1%86%E6%9E%B6/SprigBoot/%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF/%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1.md) 相关配置: ``` rayframework.path.static-exported-local-root-path=/usr/lib/app/resource/exported/ rayframework.url.static-exported-local-access-url=http://dns/rayapp ``` 导出的业务代码: ``` @RayServiceMethod public ResponseObject exportAll() throws Exception { exportAgentCardAction.exportAction(model, asynOrderExecutor.startExportOrder("导出代理卡片", model)); return responseObj; } ``` ``` @Component public class ExportAgentCardAction extends BaseAsynAction { @Autowired private CardInfoDao cardInfoDao; @Autowired private SysStaffRelationDao sysStaffRelationDao; @Async public void exportAction(RayModel model, SysAsynOrder sysAsynOrder) throws Exception { List<Long> myAgentList = sysStaffRelationDao.getStaffIdListByRelatedStaffIdAndRelationType(model.getCurLoginData().getCurStaffId(), RayiotSysStaffRelationType.parent_agent); myAgentList.add(model.getCurLoginData().getCurStaffId()); List<CardInfo> cardinfoList = cardInfoDao.findByTerminalAgentStaffIn(myAgentList); String[] paths = asynOrderExecutor.generatingExportedFilePaths(); excelExportProcessor.export(paths[0], new ExportExcelCallback() { @Override public String buildExcelObject(XSSFSheet sheet, Object... exportedDataObjects) throws Exception { int i = 0; Row title = sheet.createRow(i); title.createCell(0).setCellValue("序号"); title.createCell(1).setCellValue("用户码"); title.createCell(2).setCellValue("Iccid码"); title.createCell(3).setCellValue("接入码"); List<CardInfo> dataList = (List<CardInfo>) exportedDataObjects[0]; for (CardInfo cardinfo : cardinfoList) { //省略业务逻辑代码 } return null; } }, cardinfoList); asynOrderExecutor.completeExportOrder(sysAsynOrder, paths[1], "完成", AsynOrderStatus.EXECUTED_SUCCESS); } } ``` 这样,就实现了异步导出,在下载订单列表里面,即可查看生成的文件情况,如果生成完成,即可得到生成的文件链接,下载即可;