/**
* 导出
*/
@PostMapping("/export")
@ApiOperationSupport(order = 10)
@ApiOperation(value = "导出", notes = "传入ids")
public void export(HttpServletResponse response) throws IOException {
ArrayList<BatchExcel> batchExcelList = new ArrayList<>();
ArrayList<FreshmanExcel> freshmanExcelList = new ArrayList<>();
ArrayList<TestResultExcel> testResultExcelArrayList = new ArrayList<>();
//查询所有的batch
List<Batch> batchList = batchService.list();
List<Freshman> freshmanList = freshmanService.list(new QueryWrapper<Freshman>().lambda().last("limit 1000"));
List<TestResult> testResults = testResultService.list(new QueryWrapper<TestResult>().lambda().last("limit 1000"));
for (TestResult testResult : testResults) {
TestResultExcel testResultExcel = new TestResultExcel();
BeanUtil.copyProperties(testResult, testResultExcel);
testResultExcelArrayList.add(testResultExcel);
}
for (Batch batch : batchList) {
BatchExcel batchExcel = new BatchExcel();
BeanUtil.copyProperties(batch, batchExcel);
//将开始日期和结束日期和通知时间转换成字符串
// batchExcel.setStartDate(batch.getStartDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
batchExcel.setStartDate(batch.getStartDate().toString());
batchExcel.setEndDate(batch.getEndDate().toString());
batchExcel.setNoticeTime(batch.getNoticeTime().toString());
batchExcelList.add(batchExcel);
}
for (Freshman freshman : freshmanList) {
FreshmanExcel freshmanExcel = new FreshmanExcel();
BeanUtil.copyProperties(freshman, freshmanExcel);
freshmanExcelList.add(freshmanExcel);
}
String file = "xrrj.zip";
response.setCharacterEncoding("utf-8");
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file, "UTF-8"));
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
if (!CollectionUtils.isEmpty(batchExcelList) || !CollectionUtils.isEmpty(freshmanExcelList)
|| !CollectionUtils.isEmpty(testResultExcelArrayList)) {
ExcelWriter excelWriter = EasyExcel.write(bos).excelType(ExcelTypeEnum.XLSX).build();
WriteSheet writeSheet = EasyExcel.writerSheet(0, "批次表").head(BatchExcel.class).build();
excelWriter.write(batchExcelList, writeSheet);
WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "人员表").head(FreshmanExcel.class).build();
excelWriter.write(freshmanExcelList, writeSheet1);
WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "记录表").head(RecordExcel.class).build();
excelWriter.write(testResultExcelArrayList, writeSheet2);
excelWriter.finish();
ZipEntry zipEntry = new ZipEntry("新人日志ETL数据表格.xlsx");
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.write(bos.toByteArray());
bos.reset();
}
zipOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭数据流,注意关闭的顺序
zipOutputStream.close();
bos.close();
}
}
前端vue的话
handleExportZip() {
this.$confirm("是否导出压缩包?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}).then(() => {
// 发送下载请求
return exportZipFile(this.query); // 假设有一个与后端通信的导出压缩包的方法
}).then((response) => {
// 处理下载的响应
// 解析文件名
let fileName = "xrrj.zip";
let contentDisposition = response.headers['content-disposition'] + "";
let index = contentDisposition.indexOf('=');
if (index > 0) {
fileName = decodeURIComponent(contentDisposition.substr(index + 1));
}
// 创建一个 Blob 对象并下载文件
const blob = new Blob([response.data], { type: 'application/zip' });
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
URL.revokeObjectURL(blob);
}).catch((error) => {
// 处理下载失败的情况
console.error("导出压缩包失败:", error);
this.$message.error("导出压缩包失败");
});
}
<el-button type="info"
size="small"
icon="el-icon-download"
plain
@click="handleExportZip">导出ETL数据表格
</el-button>