代码改变世界

多个文件打包一起导出

2022-03-19 15:46  通往神之路  阅读(53)  评论(0编辑  收藏  举报
@RequestMapping(value = "/excel", method = RequestMethod.GET)
public void exportExcel(SettlementBillItemAllDTO dto, HttpServletRequest request, HttpServletResponse response){
settlementFinanceService.addOperationLog(null, null, "listOldExport", "导出历史演变表");
// 设置sheet名称和文件名称
String sheetName = "历史演变表";
String fileName = sheetName + ".zip";
FileDownloadUtils.createFile(tempExcelFilePath);
List<File> files = new ArrayList<File>(); //声明一个集合,用来存放多个Excel文件路径及名称
try {
Integer size = 1;
Integer page = 1;
while (size > 0){
PageHelper.startPage(page, 100000);
PageInfo<SettlementBillItemAllDTO> dtoList =settlementFinancePriceService.query(dto,null,null);
String path = tempExcelFilePath + sheetName + page + ".xlsx";
new ExportExcel("", SettlementBillItemAllDTO.class, sheetName).setDataList(dtoList.getList()).generateExcelToPath(path);
//excel添加到files中
files.add(new File(path));
if(page >= dtoList.getPages()){
size = 0;
}else{
size = dtoList.getSize();
}

page++;
}
String zipPath = tempExcelFilePath + fileName;
//下载zip
FileDownloadUtils.downloadZip(request, response, fileName, files, zipPath);
//删除tempDir文件夹和其中的excel和zip文件
boolean b = FileDownloadUtils.deleteDir(new File(tempExcelFilePath));
if (!b) {
throw new RuntimeException("tempDir文件夹及其中的临时Excel和zip文件删除失败");
}

} catch (Exception e) {
log.error(e.getMessage(), e);
}finally {

}
}