Bootstrap

EasyExcel 使用多线程按顺序导出数据

通过多线程读取数据,使用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 -> 6
        int 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<>();

        // 通过多线程查询,并设置线程ID
        for (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 {
            // 使用单线程写入Excel
            sortedResults.forEach(r -> excelWriter.write(r.data, writeSheet));
        } finally {
            executorService.shutdown();
            excelWriter.finish();
        }
    }
;