多线程导excel表

ServiceImpl层:

public List<SchoolTask> exportAll(HttpServletRequest request) throws Exception{
        List<String> list = indexDao.findKemu();
        List<SchoolTask> listA = indexDao.findAll();//查找全部实体属性字段
        List<String> questionName = indexDao.selectSubjectName(list);
        List<SchoolTask> addList = new ArrayList<>();
        Map<String, List<SchoolTask>> maps = new HashMap<String, List<SchoolTask>>();
        String subjectName = listA.get(0).getSubjectName();
        for (int x = 0; x < listA.size(); x++) {
            if (!listA.get(x).getSubjectName().equals(subjectName)) {
                maps.put(subjectName, addList);
                subjectName = listA.get(x).getSubjectName();
                addList = new ArrayList<>();
            }
            addList.add(listA.get(x));
            if (x == listA.size() - 1) {
                maps.put(subjectName, addList);
            }
        }
        
        
        ExecutorService pool = Executors.newSingleThreadExecutor();//创建线程池
        CountDownLatch doneSignal = new CountDownLatch(list.size());//线程计数器
        for(int x=0;x<list.size();x++) {
            pool.submit(new ExcelRunnable(doneSignal,list.get(x)+".xlsx",maps.get(list.get(x)),questionName));
        }
        doneSignal.await();
        pool.shutdown();
        
        return listA;
    }

util:

public class ExcelRunnable implements Runnable {

    private final CountDownLatch doneSignal;

    private List<SchoolTask> listA;
    
    private List<String> questionName;

    private String excelName;


    public ExcelRunnable(CountDownLatch doneSignal, String excelName, List<SchoolTask> listA, List<String> questionName) {
        this.doneSignal = doneSignal;
        this.listA = listA;
        this.questionName=questionName;
        this.excelName=excelName;
    }


    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        String[] excelHeader = new String[questionName.size() + 1];
        excelHeader[0] = "学校";
        int add = 1;
        for (String qn : questionName) {
            excelHeader[add] = qn;
            add++;
        }
        XSSFWorkbook hwb = null;

        try {
            hwb = creatExcel(listA, excelHeader);
        } catch (Exception e) {
            e.printStackTrace();
        }
        FileOutputStream fos=null;
        try {
            fos = new FileOutputStream("E://"+excelName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            hwb.write(fos);// 写入
            fos.close();// 关闭资源
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            doneSignal.countDown();//线程-1
        }
        
    }

    private XSSFWorkbook creatExcel(List<SchoolTask> listA,String[] excelHeader) throws Exception {
        XSSFWorkbook hwb = new XSSFWorkbook();// 第一步,创建一个workbook(一个excel文件)
        XSSFSheet hs = hwb.createSheet("表格详情信息");// 第二步,在workbook中添加一个sheet,对应excel文件中sheet
        XSSFRow hr = hs.createRow((int) 0);// 第三部,在sheet中添加表头第0行(相当于解释字段)
        XSSFCellStyle hcs = hwb.createCellStyle();// 第四步,设置第0行(表头)居中
        hcs.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 创建居中格式
        // 将表头的字段放入数组当中
        for (int i = 0; i < excelHeader.length; i++) {
            XSSFCell cell = hr.createCell(i);// 顺序创建
            cell.setCellValue(excelHeader[i]);// 顺序塞入
            cell.setCellStyle(hcs);// 居中
            hs.autoSizeColumn(i);// 设置 i这一列为自动调整列宽
        }
        
        String sname="";
        int j=1;
        int ce=0;
        XSSFRow xr = null;
        for(int x=0;x<listA.size();x++) {
            if(!listA.get(x).getSchoolName().equals(sname)) {
                xr = hs.createRow(j);
                xr.createCell(0).setCellValue(listA.get(x).getSchoolName());
                sname = listA.get(x).getSchoolName();
                j++;
                ce=0;
            }
            ce++;
            xr.createCell(ce).setCellValue(listA.get(x).getTask());
        }
        return hwb;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后端从入门到精通

你的鼓励是我最大的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值