package com.example.demo;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
@SuppressWarnings("all")
public class ExcelUtils {
public static Integer exportExcel(SXSSFSheet sheet, List<Object> dataList,Integer currentRow) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
Object object =dataList.get(0);
Class<?> clazz=object.getClass();
Field[] fields=clazz.getDeclaredFields();
Integer columnLength =fields.length;
if (currentRow == 0){
SXSSFRow row=sheet.createRow(currentRow++);
for (int i=0;i<columnLength;i++){
SXSSFCell cell=row.createCell(i);
cell.setCellValue(fields[i].getName());
}
}
for (Object data:dataList){
SXSSFRow row=sheet.createRow(currentRow++);
for (int i = 0; i < columnLength; i++) {
SXSSFCell cell =row.createCell(i);
PropertyDescriptor descriptor=new PropertyDescriptor(fields[i].getName(),data.getClass());
Method method=descriptor.getReadMethod();
Object value =method.invoke(data);
cell.setCellValue(value.toString());
}
}
return currentRow;
}
private static final ConcurrentHashMap<Integer,FutureTask<Integer>> taskList =new ConcurrentHashMap<>();
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
Integer[] startRow ={0,10000,20000,30000,40000,50000,60000,70000,80000,90000};
Integer taskIndex=0;
SXSSFWorkbook workbook=new SXSSFWorkbook();
SXSSFSheet sheet=workbook.createSheet();
for (int i = 0; i < 10; i++) {
final int temp = i;
FutureTask<Integer> futureTask = new FutureTask<>(new Callable() {
@Override
public Integer call() throws Exception {
List<Object> students = new ArrayList<>();
Student student1 = new Student();
student1.setId(1);
student1.setName("2");
for (int j = 0; j < 10000; j++) {
students.add(student1);
}
// Thread.sleep(100);
Integer currentRow = exportExcel(sheet, students, startRow[temp]);
return currentRow;
}
});
taskList.putIfAbsent(i, futureTask);
}
while (true){
FutureTask<Integer> task=taskList.remove(taskIndex++);
if (task!=null){
task.run();
Integer currentRow=task.get();
if (taskList.size()==0){
FileOutputStream fileOutputStream=new FileOutputStream(new File("C:\\test\\1.xlsx"));
workbook.write(fileOutputStream);
break;
}
}
}
}
}
package com.example.demo;
public class Student {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
}
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>