您的位置:首页 > 娱乐 > 明星 > 程序开发是什么工作_小规模公司自学做账_石家庄网络营销网站推广_百度快照排名

程序开发是什么工作_小规模公司自学做账_石家庄网络营销网站推广_百度快照排名

2025/2/26 14:51:06 来源:https://blog.csdn.net/qq_42678469/article/details/143702940  浏览:    关键词:程序开发是什么工作_小规模公司自学做账_石家庄网络营销网站推广_百度快照排名
程序开发是什么工作_小规模公司自学做账_石家庄网络营销网站推广_百度快照排名

通过多线程读取数据,使用EasyExcel按顺序导出数据

导出时如果要保证顺序需要使用单线程,但是查询时可以用多线程,因为多线程查询后返回数据不是按照顺序排列的,所以我的思路是再循环时给每个线程打标识,通过标识来排序多线程返回的结果

创建一个Future对象,用于排序多线程查询结果

	static class Result {final Integer threadId;final List<UserInfo> data;Result(Integer threadId, List<UserInfo> data) {this.threadId = threadId;this.data = data;}}
	@PostMapping("export3")public void export3(HttpServletResponse response) throws IOException, InterruptedException, ExecutionException {// 查询总数Long dataCount = userInfoMapper.selectCount();// 每页条数Long searchCount = 100000L;// 获取页数并向上取整 5.2 -> 6int ceil = (int) Math.ceil((double) dataCount / searchCount);// 使用线程池ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());List<Future<Result>> futures = new ArrayList<>();Map<Integer, Result> resultsMap = new ConcurrentHashMap<>();// 通过多线程查询,并设置线程IDfor (int i = 1; i <= ceil; i++) {Integer pageNum = Math.toIntExact((i - 1) * searchCount);int finalI = i;futures.add(executorService.submit(() -> new Result(finalI, userInfoMapper.getList(pageNum, searchCount))));}// 收集所有线程的结果for (Future<Result> future : futures) {resultsMap.put(future.get().threadId, future.get());}// 通过线程ID排序List<Result> sortedResults = resultsMap.values().stream().sorted(Comparator.comparingInt(result -> result.threadId)).collect(Collectors.toList());// 构建表头WriteCellStyle headWriteCellStyle = new WriteCellStyle();headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);WriteCellStyle contentWriteCellStyle = new WriteCellStyle();contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);// 设置返回格式response.setHeader("Content-Disposition", "attachment; filename=test" + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx");response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("UTF-8");ExcelWriter excelWriter = EasyExcelFactory.write(response.getOutputStream(), UserInfo.class).registerWriteHandler(horizontalCellStyleStrategy).needHead(true).excelType(ExcelTypeEnum.XLSX).build();// 如果是单sheet,则放在循环外面,多sheet放在循环里面WriteSheet writeSheet = EasyExcelFactory.writerSheet("Sheet1").head(UserInfo.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).registerWriteHandler(horizontalCellStyleStrategy).build();try {// 使用单线程写入ExcelsortedResults.forEach(r -> excelWriter.write(r.data, writeSheet));} finally {executorService.shutdown();excelWriter.finish();}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com