public void exportZip(String time, Integer courType, HttpServletResponse response) {
ZipOutputStream zipOutputStream = null;
try {
Date dateTime = DateUtil.beginOfMonth(DateUtil.parse(time));
DateTime beginTime = DateUtil.beginOfMonth(dateTime);
DateTime endTime = DateUtil.endOfMonth(dateTime);
response.setContentType("application/force-download;charset=UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("月度汇总", "utf-8") + ".zip");
zipOutputStream = new ZipOutputStream(response.getOutputStream());
exportAnalysisReportZip(beginTime, endTime, courType, zipOutputStream);
exportTraceability(beginTime, endTime, courType, zipOutputStream);
zipOutputStream.closeEntry();
} catch (Exception e) {
log.error("", e);
} finally {
try {
if (zipOutputStream != null) {
zipOutputStream.close();
}
} catch (IOException e) {
log.error("", e);
}
}
}
public void exportAnalysisReportZip(DateTime beginTime, DateTime endTime, Integer courType, ZipOutputStream zipOutputStream) throws IOException {
AnalysisReportDto analysisReportDto = getAnalysisReportDto(beginTime, endTime, courType);
InputStream is = this.getClass().getClassLoader().getResourceAsStream("template/demo.docx");
Configure config = Configure.builder().bind("orgcourselist", new LoopRowTableRenderPolicy())
.bind("teclorgathiratiolist", new LoopRowTableRenderPolicy())
.build();
XWPFTemplate template = XWPFTemplate.compile(is, config).render(analysisReportDto);
zipOutputStream.putNextEntry(new ZipEntry("常态化.docx"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
template.write(bos);
byte[] brray = bos.toByteArray();
InputStream in = new ByteArrayInputStream(brray);
IoUtil.copy(in, zipOutputStream);
IoUtil.close(in);
}
public void exportTraceability(DateTime courBeginTime, DateTime courEndTime, Integer courType, ZipOutputStream zipOutputStream) {
InputStream in = null;
try {
AttendanceHistorySearchDo attendanceHistorySearchDo = new AttendanceHistorySearchDo();
List<AttendanceHistory> list = attendanceHistoryDao.findListByPo(attendanceHistorySearchDo, "cour_begin_time", "ASC", null, null, false);
List<AttendanceHistoryExcelDto> attendanceHistoryExcelDtos = new ArrayList<>();
list.stream().forEach(dto -> {
AttendanceHistoryExcelDto attendanceHistoryExcelDto = new AttendanceHistoryExcelDto();
BeanUtils.copyProperties(dto, attendanceHistoryExcelDto);
attendanceHistoryExcelDtos.add(attendanceHistoryExcelDto);
});
zipOutputStream.putNextEntry(new ZipEntry("溯源数据.xlsx"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
EasyExcel.write(bos, AttendanceHistoryExcelDto.class).sheet("行数据").doWrite(attendanceHistoryExcelDtos);
byte[] brray = bos.toByteArray();
in = new ByteArrayInputStream(brray);
IoUtil.copy(in, zipOutputStream);
} catch (IOException e) {
log.error("", e);
} finally {
IoUtil.close(in);
}
}
web导出压缩包(多文件压缩到压缩包后导出)
于 2023-11-07 15:35:55 首次发布