多线程导入excel
多线程导入excel
1、pojo 类
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、主方法
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、多线程使用
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 的方法

浙公网安备 33010602011771号