poi实现多线程导出Excel分sheet(二)

昨天文章讲了讲了如何单线程导出Excel今天就简单写一下如何多线程导出Excel

废话不多说直接上代码

简单的多线程类

package com.learn.mul;

import com.learn.entity.Student;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author summer
 * @date 2022-05-11  13:01
 */
public class MoreThread implements  Runnable{

    private List studentList;
    private Sheet sheet;
    private CountDownLatch countDownLatch;

    public MoreThread(List studentList, Sheet sheet, CountDownLatch countDownLatch) {
        this.studentList = studentList;
        this.sheet = sheet;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        try {
            exportMore();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public void exportMore() throws IllegalAccessException {
        Iterator iterator = studentList.iterator();
        Field[] fields = Student.class.getDeclaredFields();
        int startIndex = 0;
        if(startIndex == 0){
            Row row = sheet.createRow(startIndex);
            for (int i = 0; i < fields.length; i++) {
                Cell cell = row.createCell(i);
                cell.setCellValue(fields[i].getName());
            }
            startIndex++;
        }
        while(iterator.hasNext()){
            Student student = iterator.next();
            Row row = sheet.createRow(startIndex);
            for (int i = 0; i < fields.length; i++) {
                Cell cell = row.createCell(i);
                Field field = fields[i];
                field.setAccessible(true);
                cell.setCellValue(String.valueOf(field.get(student)));
            }
            startIndex++;
        }
        countDownLatch.countDown();
    }


}

在上篇文章中Main方法写多线程导出方法

public static void exportDataMore(List studentList, int threadNum) throws Exception {
        XSSFWorkbook workbook = new XSSFWorkbook();
//        Field field = workbook.getClass().getDeclaredField("sharedStringSource");
//        field.setAccessible(true);
//        field.set(workbook,new CustomSharedStringsTable());

        int count = studentList.size();
        int avgCount = (int) Math.floor(count / threadNum);
        int modeCount = count % threadNum;

        int start = 0;
        int end = 0;
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        for (int i = 0; i < threadNum; i++) {
            start = i * avgCount;
            if (i == threadNum - 1) {
                end = (i + 1) * avgCount + modeCount;
            } else {
                end = (i + 1) * avgCount;
            }
            List students = studentList.subList(start, end);
            Sheet sheet = workbook.createSheet();

            new Thread(new MoreThread(students,sheet,countDownLatch),"线程:"+i).start();
        }
        countDownLatch.await();
        FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\Summer\\Desktop\\poi\\student-more.xlsx"));

        workbook.write(fos);

    }

你可能感兴趣的:(Java开发,java,开发语言)