//把处理好的list传入准备导出
public File exportExcel(List<> all, HttpServletResponse res) throws Exception {
FILE_PATH = "path/模板.xlsx";
FILE_NAME = "模板.xlsx";
File export = new File("/" + UUID.randomUUID().toString());
try {
final Integer[] index = {1};
List<RyzzUserEx> collect = all.stream().collect(Collectors.toList());
int pageSize = 1000000;//每个sheet页的数据量
int sheetCount = DataUtils.castInteger(Math.ceil(collect.size() * 1.0 / pageSize));
ExcelExportConfig[] list = new ExcelExportConfig[sheetCount];
for (int i = 0; i < sheetCount; i++) {
int start = i * pageSize;
int end = start + pageSize;
if (start + pageSize > all.size()) {
end = all.size();
}
ExcelExportConfig<RyzzUserEx> excelExportConfig = new ExcelExportConfig("Sheet1",
"A,1", //模板开始位置
"N,1", //模板结束位置
collect.subList(start, end), RyzzUserEx.class);
excelExportConfig.setExportSheetName("Sheet" + (i == 0 ? "1" : i + 1));
excelExportConfig.setNeedShift(false);
list[i] = excelExportConfig;
}
ExportService exportService = new ExportService(this.buildExportProperties());
InputStream bis = null;
//如果路径不存在则创建路径
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(filePath + FILE_NAME);//导出路径
// 读取模板
bis = this.getClass().getClassLoader().getResourceAsStream(FILE_PATH);
byte[] buff = new byte[1024];
int i = bis.read(buff);
while (i != -1) {
fos.write(buff, 0, i);// 写入数据
fos.flush();
i = bis.read(buff);
}
bis.close();
fos.close();// 保存数据
export = exportService.export(FILE_NAME, list);
String absolutePath = export.getAbsolutePath();
FileInputStream fileInputStream = new FileInputStream(absolutePath);
byte[] buffOut = new byte[1024];
int j = fileInputStream.read(buffOut);
// 发送给客户端的数据
OutputStream outputStream = res.getOutputStream();
while (j != -1) {
outputStream.write(buffOut, 0, j);
outputStream.flush();
j = fileInputStream.read(buffOut);
}
fileInputStream.close();
outputStream.close();
return file;
} catch (Exception e) {
throw e;
} finally {
File file = new File(filePath + FILE_NAME);
FileUtils.deleteQuietly(file);
FileUtils.deleteQuietly(export.getAbsoluteFile());
}
}
public ExportProperties buildExportProperties() {
ExportProperties exportProperties = new ExportProperties();
exportProperties.setTempFolder(filePath);
exportProperties.setTemplateFolder(filePath);
return exportProperties;
}
//对应excel实体类
@Data
public class RyzzKszxDto implements IEntity {
@ExportColumnName("序号")
private Integer index;
@ExportColumnName("表头1")
private String a1;
@ExportColumnName("表头2")
private String a2;
@ExportColumnName("表头3")
private String a3;
}