PgSchoolCalendarWeekManagePageQry pgSchoolCalendarWeekManagePageQry = new PgSchoolCalendarWeekManagePageQry();标签:zipOutputStream,pgTeachingSchedulingCurriculumQry,Excel,excelWriter,导出,pgSchoolC From: https://www.cnblogs.com/niuniu0108/p/17967623
pgSchoolCalendarWeekManagePageQry.setPgSemesterManageId(pgTeachingSchedulingCurriculumQry.getPgSemesterManageId());
pgSchoolCalendarWeekManagePageQry.setStudentType(pgTeachingSchedulingCurriculumQry.getStudentType());
List<PgSchoolCalendarWeekManageListVO> schoolCalendarWeekManageListVOList = pgSchoolCalendarWeekManageService.getList(pgSchoolCalendarWeekManagePageQry).getData();
if (!CollectionUtils.isEmpty(schoolCalendarWeekManageListVOList)) {
ServletOutputStream outputStream = response.getOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
for (PgSchoolCalendarWeekManageListVO pgSchoolCalendarWeekManageVo : schoolCalendarWeekManageListVOList) {
pgTeachingSchedulingCurriculumQry.setStartDate(pgSchoolCalendarWeekManageVo.getWeekStartDate());
pgTeachingSchedulingCurriculumQry.setEndDate(pgSchoolCalendarWeekManageVo.getWeekEndDate());
MultiResponse<PgTeachingSchedulingCurriculumVO> pgTeachingSchedulingCurriculumVO = pgTeachingSchedulingCurriculumService.getCurriculumList(pgTeachingSchedulingCurriculumQry);
List<PgTeachingSchedulingCurriculumVO> pgTeachingSchedulingCurriculumVOList = pgTeachingSchedulingCurriculumVO.getData();
List<XsxExportVO> xsxExportVOList = getExcelData(pgTeachingSchedulingCurriculumVOList);
String titleExcel = "学术型硕士研究生";
if (!StringUtils.isEmpty(pgTeachingSchedulingCurriculumQry.getPgSemesterManageId())) {
String pgSemesterManageName = pgSemesterManageService.get(new PgSemesterManageInfoQry(pgTeachingSchedulingCurriculumQry.getPgSemesterManageId())).getData().getSemesterName();
titleExcel += pgSemesterManageName;
}
if (!StringUtils.isEmpty(pgTeachingSchedulingCurriculumQry.getSchoolCalendarWeekId())) {
PgSchoolCalendarWeekManageVO pgSchoolCalendarWeekManageVO = pgSchoolCalendarWeekManageService.get(new PgSchoolCalendarWeekManageInfoQry(pgSchoolCalendarWeekManageVo.getSchoolCalendarWeekId())).getData();
titleExcel += pgSchoolCalendarWeekManageVO.getWeekNumberName();
if (pgSchoolCalendarWeekManageVO.getWeekStartDate() != null) {
titleExcel += "(" + DateUtils.parseDateToStr("MM月dd日", pgSchoolCalendarWeekManageVO.getWeekStartDate());
}
if (pgSchoolCalendarWeekManageVO.getWeekEndDate() != null) {
titleExcel += "至" + DateUtils.parseDateToStr("MM月dd日", pgSchoolCalendarWeekManageVO.getWeekEndDate()) + ")课程表";
}
}
XsxExportTitleVO xsxExportTitleVO = new XsxExportTitleVO();
xsxExportTitleVO.setTitle(titleExcel);
String fileName = "Lesson";
try {
response.setCharacterEncoding("utf-8");
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".zip");
ClassPathResource couponOrderTemplateResource = new ClassPathResource("template/xsxExport.xlsx");
ExcelWriter excelWriter = EasyExcel
.write()
.withTemplate(couponOrderTemplateResource.getInputStream())
.registerWriteHandler(new CustomCellWriteHandler())
.inMemory(true)
.build();
//创建第一个sheet
WriteSheet sumEvaSheet = EasyExcel.writerSheet("课程表").build();
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
// 标题
excelWriter.fill(new FillWrapper("title", Arrays.asList(xsxExportTitleVO)), sumEvaSheet);
excelWriter.fill(new FillWrapper("lesson", xsxExportVOList), fillConfig, sumEvaSheet);
//创建压缩文件
ZipEntry zipEntry = new ZipEntry(titleExcel + ".xlsx");
zipOutputStream.putNextEntry(zipEntry);
Workbook workbook = excelWriter.writeContext().writeWorkbookHolder().getWorkbook();
//将excel对象以流的形式写入压缩流
workbook.write(zipOutputStream);
excelWriter.close();
} catch (Exception e) {
}
}
zipOutputStream.closeEntry();
zipOutputStream.flush();
//关闭数据流,注意关闭的顺序
zipOutputStream.close();
outputStream.close();
}