【导出excel】学习

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值