## 一:可能需要的maven依赖。我项目的依赖有点多。要是不够或者不对自己在找找吧
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.0</version>
</dependency>
二: 功能代码
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.Collectors;
@Resource(name = "taskExector")
private Executor executor;
@GetMapping("/leading")
public Response leading(HttpServletResponse response, @RequestParam("uuIds") String uuIds) throws Exception {
Integer chunkSize = 100000;
List<Integer> ids = simulationMapper.selectIdsByUuId(uuId);
List<List<Integer>> chunkedList = new LinkedList<>();
for (int i = 0; i < ids.size(); i += chunkSize) {
chunkedList.add(ids.subList(i, Math.min(i + chunkSize, ids.size())));
}
Workbook wb = new SXSSFWorkbook(100);
LongAdder count = new LongAdder();
List<CompletableFuture<Void>> id = chunkedList.stream().map(x -> CompletableFuture.runAsync(() -> {
List<Simulation> simulations = simulationMapper.selectList(new QueryWrapper<Simulation>().in("id", x));
int pageRowNo = 0;
Sheet sheet = null;
Row nRow = null;
Cell nCell = null;
synchronized (Exception.class) {
long sum = count.sum();
count.increment();
wb.createSheet("第" + sum + "个工作簿");
sheet = wb.getSheetAt((int) sum);
}
for (Simulation dataInterface : simulations) {
nRow = sheet.createRow(pageRowNo++);
nCell = nRow.createCell(0);
nCell.setCellValue("写入数据");
nCell = nRow.createCell(1);
nCell.setCellValue("写入数据");
}
}, executor)).collect(Collectors.toList());
for (CompletableFuture<Void> voidCompletableFuture : id) {
voidCompletableFuture.join();
}
OutputStream output;
output = response.getOutputStream();
response.reset();
LocalDateTime now = LocalDateTime.now();
String s = now + "";
response.setHeader("Content-disposition", "attachment;filename=" + new String(s.getBytes(), "iso-8859-1") + ".xls");
response.setContentType("application/vnd.ms-xls");
wb.write(output);
output.close();
wb.close();
}
三: 线程池代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
public class Executor {
@Bean("taskExector")
public java.util.concurrent.Executor taskExector() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
int i = Runtime.getRuntime().availableProcessors();
executor.setCorePoolSize(i);
executor.setMaxPoolSize(i+1);
executor.setQueueCapacity(256);
executor.setKeepAliveSeconds(20);
executor.setThreadNamePrefix("tsak-asyn");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}