java多线程批量导出excel

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelExporter {
    public static void main(String[] args) {
        // 模拟数据
        List<List<String>> dataList = new ArrayList<>();
        dataList.add(List.of("ID", "Name", "Age"));
        dataList.add(List.of("1", "John", "25"));
        dataList.add(List.of("2", "Mary", "30"));
        dataList.add(List.of("3", "Tom", "28"));

        // 设定每个线程处理的数据量和线程数
        int batchSize = 2;
        int threadCount = 2;

        // 创建线程池
        ExecutorService executor = Executors.newFixedThreadPool(threadCount);

        // 创建导出任务
        for (int i = 0; i < dataList.size(); i += batchSize) {
            int endIndex = Math.min(i + batchSize, dataList.size());
            List<List<String>> subList = dataList.subList(i, endIndex);
            Runnable exportTask = new ExportTask(subList);
            executor.execute(exportTask);
        }

        // 关闭线程池
        executor.shutdown();
    }

    static class ExportTask implements Runnable {
        private List<List<String>> dataList;

        public ExportTask(List<List<String>> dataList) {
            this.dataList = dataList;
        }

        @Override
        public void run() {
            try (Workbook workbook = new XSSFWorkbook();
                 FileOutputStream fileOut = new FileOutputStream("Export" + Thread.currentThread().getId() + ".xlsx")) {

                Sheet sheet = workbook.createSheet("Sheet1");
                int rowNum = 0;
                for (List<String> rowData : dataList) {
                    Row row = sheet.createRow(rowNum++);
                    int cellNum = 0;
                    for (String cellData : rowData) {
                        Cell cell = row.createCell(cellNum++);
                        cell.setCellValue(cellData);
                    }
                }
                workbook.write(fileOut);

                System.out.println("Exported by Thread " + Thread.currentThread().getId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java多线程分批导出Excel可以提高导出效率,可以使用线程池和CountDownLatch来实现。以下是一个示例代码片段: ```java public void exportToExcel(List<List<String>> data, int batchSize, String filePath, int threadCount) throws InterruptedException, ExecutionException, IOException { Workbook workbook = new XSSFWorkbook(); // 使用线程池和CountDownLatch来实现多线程导出 ExecutorService executorService = Executors.newFixedThreadPool(threadCount); CountDownLatch countDownLatch = new CountDownLatch(threadCount); // 计算每个线程需要导出的数据量 int dataSize = data.size(); int batchCount = dataSize % batchSize == 0 ? dataSize / batchSize : dataSize / batchSize + 1; int batchCountPerThread = batchCount % threadCount == 0 ? batchCount / threadCount : batchCount / threadCount + 1; // 创建多个导出任务 List<Future<?>> futures = new ArrayList<>(); for (int i = 0; i < threadCount; i++) { int startIndex = i * batchCountPerThread * batchSize; int endIndex = Math.min((i + 1) * batchCountPerThread * batchSize, dataSize); List<List<String>> subData = data.subList(startIndex, endIndex); ExportTask exportTask = new ExportTask(subData, batchSize, workbook, countDownLatch); futures.add(executorService.submit(exportTask)); } // 等待所有任务执行完毕 countDownLatch.await(); // 导出Excel文件 try (OutputStream outputStream = new FileOutputStream(filePath)) { workbook.write(outputStream); } // 关闭线程池 executorService.shutdown(); } private static class ExportTask implements Runnable { private List<List<String>> data; private int batchSize; private Workbook workbook; private CountDownLatch countDownLatch; public ExportTask(List<List<String>> data, int batchSize, Workbook workbook, CountDownLatch countDownLatch) { this.data = data; this.batchSize = batchSize; this.workbook = workbook; this.countDownLatch = countDownLatch; } @Override public void run() { int rowIndex = 0; Sheet sheet = workbook.createSheet(); for (List<String> rowData : data) { // 如果当前工作表的数据超过指定大小,则新建一个工作表 if (rowIndex % batchSize == 0 && rowIndex > 0) { sheet = workbook.createSheet(); rowIndex = 0; } Row row = sheet.createRow(rowIndex++); int cellIndex = 0; for (String cellData : rowData) { Cell cell = row.createCell(cellIndex++); cell.setCellValue(cellData); } } countDownLatch.countDown(); } } ``` 在上述代码中,使用了线程池和CountDownLatch来实现多线程导出。首先计算了每个线程需要导出的数据量,然后创建多个导出任务,每个任务负责导出一部分数据到Excel中。在导出过程中,如果当前工作表的数据超过指定大小,则新建一个工作表。所有任务执行完毕后,将Excel文件写入到磁盘中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值