多线程导入excel

多线程导入excel

 

1、pojo 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.yushuang.seckill.entity;
 
public class People {
 
    public String name;
    public String sex;
    public String phone;
    public String age;
 
    public People() {
    }
 
    public People(String name, String sex, String phone, String age) {
        this.name = name;
        this.sex = sex;
        this.phone = phone;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getSex() {
        return sex;
    }
 
    public void setSex(String sex) {
        this.sex = sex;
    }
 
    public String getPhone() {
        return phone;
    }
 
    public void setPhone(String phone) {
        this.phone = phone;
    }
 
    public String getAge() {
        return age;
    }
 
    public void setAge(String age) {
        this.age = age;
    }
}

  

2、主方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.yushuang.seckill.test;
 
import com.yushuang.seckill.entity.People;
import com.yushuang.seckill.thread.ExportExcelThread;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.*;
 
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
 
public class ExportExcel {
 
    public static void main(String[] args) throws Exception {
 
        //模拟数据中的数据
        ArrayList<People> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            People people = new People("姓名"+i,i+"","1234555555","男");
            list.add(people);
        }
//        doExportOneThread(list);
        doExportMoreThread(list,4);
    }
 
    /**
     * 单线程导出excel
     * @param list
     * @throws Exception
     */
    public static void doExportOneThread(ArrayList<People> list) throws Exception {
        //创建一个工作簿
        SXSSFWorkbook workbook = new SXSSFWorkbook();
        SXSSFSheet sheet = workbook.createSheet();
        sheet.setRandomAccessWindowSize(-1);
 
        Iterator<People> iterator = list.iterator();
        Field[] fields = People.class.getDeclaredFields();
        Integer currentRow = 0;
        while (iterator.hasNext()){
            People people = iterator.next();
            Row row = sheet.createRow(currentRow);
            for (int i = 0; i < fields.length; i++) {
                Cell cell = row.createCell(i);
                if(currentRow == 0){
                    cell.setCellValue(fields[i].getName());
                }else {
                    cell.setCellValue(String.valueOf(fields[i].get(people)));
                }
            }
            currentRow++;
        }
        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\aa.xlsx"));
        workbook.write(fileOutputStream);
 
 
    }
 
    /**
     * 多线程导出excel
     * @param dataList
     * @param threadNumber
     * @throws Exception
     */
    public static void doExportMoreThread(ArrayList<People> dataList,Integer threadNumber) throws Exception{
 
        int dataCount = dataList.size();
        //获取平均值
        int avgNumber = (int) Math.floor(dataCount / threadNumber);
        //获取余数
        int remainder = dataCount % threadNumber;
 
        CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
 
        SXSSFWorkbook workbook = new SXSSFWorkbook();
 
        int fromIndex = 0;
        int toIndex = 0;
 
        for (int i = 0; i < threadNumber; i++) {
            fromIndex = i * avgNumber;
            if (i == threadNumber - 1) {
                toIndex = (i + 1) * avgNumber -1 + remainder;
            }else {
                toIndex = (i + 1) * avgNumber -1;
            }
            List<People> data = dataList.subList(fromIndex, toIndex + 1);
            SXSSFSheet sheet = workbook.createSheet();
            new Thread(new ExportExcelThread(data,countDownLatch,sheet),"线程"+i).start();
        }
        //告诉主线程,要等所有的线程运行完之后在生成Excel 文件
        countDownLatch.await();
        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\bb.xlsx"));
        workbook.write(fileOutputStream);
        System.out.println("文件写入磁盘成功。。。。。。。");
 
    }
 
}

  

3、多线程使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.yushuang.seckill.thread;
 
import com.yushuang.seckill.entity.People;
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;
 
public class ExportExcelThread implements Runnable {
 
    private List<People> data;
 
    //信号量
    private CountDownLatch countDownLatch;
 
    private Sheet sheet;
 
    //记录开始的位置
//    private Integer startIndex;
 
    public ExportExcelThread(List<People> data, CountDownLatch countDownLatch, Sheet sheet) {
        this.data = data;
        this.countDownLatch = countDownLatch;
        this.sheet = sheet;
    }
 
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println(name + "--------开始执行-------");
        try {
            doExport();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void doExport() throws Exception {
        Iterator<People> iterator = data.iterator();
        Field[] fields = People.class.getDeclaredFields();
        Integer 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()){
            People people = iterator.next();
            sheet.createRow(startIndex);
            Row row = sheet.createRow(startIndex);
            for (int i = 0; i < fields.length; i++) {
                Cell cell = row.createCell(i);
                cell.setCellValue(String.valueOf(fields[i].get(people)));
            }
            startIndex++;
        }
        countDownLatch.countDown();
    }
 
}

  

 

 

以上就是多线程导出excel 的方法

 

posted @   xingmeng1  阅读(554)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示