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;
}}