在博主做完了整个项目所有的下载模板,导入导出之后,迎来了一个阶段性Boss。
根据一个客户的ID去导出相关联的各模块的excel表并且要把各模块与该客户相关的附件一起导出来并且压缩成.zip/.rar。
做了那么多个导入导出总归是要有些心得的,也有些底气,毕竟干了一年多不就是一压缩包吗?
盘他!!!!
为了降低复杂度把大部分业务去掉,咱意思意思!
@ResponseBody
@GetMapping("/excelAllList")
public void excelCustomerList1(@RequestParam Map<String, Object> params,HttpServletRequest request, HttpServletResponse response) throws IOException{
//获取客户信息 && 获取>=0个customer_id
List<CustomerDO> customerList = customerService.list(map);
List<Integer> cIds = Lists.newArrayList();
//客户的附件id收集
List<Integer> cFIds = Lists.newArrayList();
//获取客户信息的
customerList.forEach(customer -> {
cIds.add(customer.getId());
cFIds.add(customer.getFileID());
});
//获取与客户相匹配的销售信息
List<XiaoshouDO> xiaoshouDOS = xiaoshouService.getDataByCustomerIds(cIds);
xiaoshouDOS.stream().forEach(xs->{
cFIds.add(xs.getXiaoshouFile());
});
try {
//1.0客户资源文件
NewExcelExport excelExport = new NewExcelExport();
String fileName = "Customer.xlsx";
String filePath = apiProperties.getFilePath()+Const.ZIPFILEPATHFILE+fileName; //上传路径
String nFilePath = apiProperties.getFilePath()+Const.ZIPFILEPATHFILE;
filePath = java.net.URLDecoder.decode(filePath,"utf-8");
OutputStream out = new FileOutputStream(filePath);
//转移客户的附件id
cFIds.forEach(cf->{
OrderInFileDO orderInFileDO = orderInFileService.get(cf);
String oldfilePath = orderInFileDO.getFilePath();
String fName = orderInFileDO.getFileName();
ZipDHelper.copyFile(apiProperties.getFilePath()
+oldfilePath,nFilePath+fName);
});
List<List<String>> customers = new ArrayList<>();
String [] customerMessage = {"名称","别称","英文名称","城市","备注"};
customerList.forEach(customer-> {
List<String> list = new ArrayList<>();
list.add(customer.getName());
list.add(customer.getNickName());
list.add(customer.getEngName());
list.add(customer.getCity());
list.add(customer.getRemarks());
customers.add(list);
});
//3、生成格式是xlsx可存储103万行数据,如果是xls则只能存不到6万行数据
XSSFWorkbook workbook = new XSSFWorkbook();
//第一个表格内容
excelExport.exportExcel(workbook, 0, "客户资源", customerMessage, customers, out);
ZipDHelper.delAllFile(nFilePath);
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName=" + DownHelper.setFileDownloadHeader(request, fileName));
workbook.write(out);
FileUtils.writeBytes(filePath, response.getOutputStream());
//测试导出第二个外挂excel 销售资源
NewExcelExport excelExportCell =new NewExcelExport();
String fileNameCell = "Cell.xlsx";
String filePathCell = apiProperties.getFilePath()+Const.ZIPFILEPATHFILE+fileNameCell; //上传路径
filePathCell = java.net.URLDecoder.decode(filePathCell,"utf-8");
OutputStream outCell = new FileOutputStream(filePathCell);
String[] cellMessage = { "客户名称", "销售机会描述","目标" ,"预算","备注"};
List<List<String>> cellData = Lists.newArrayList();
xiaoshouDOS.stream().forEach(cell->{
if (cell==null) return;
List<String> rowData = new ArrayList<>();
rowData.add(cell.getCustomerName());
rowData.add(cell.getTitle());
rowData.add(new BigDecimal(cell.getTargetMoney()).toPlainString());
rowData.add(new BigDecimal(cell.getTargetMoney()).toPlainString());
rowData.add(cell.getRemarks());
cellData.add(rowData);
});
XSSFWorkbook workbookCell = new XSSFWorkbook();
excelExportCell.exportExcel(workbookCell,0,"销售",cellMessage,cellData,outCell);
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName=" + DownHelper.setFileDownloadHeader(request, fileNameCell));
workbookCell.write(outCell);
FileUtils.writeBytes(filePathCell, response.getOutputStream());
//压岁包工具类
ZipDHelper.generateFile(nFilePath,"zip");
request.getSession().setAttribute("fileName","CompressFile.zip");
} catch (Exception e) {
e.printStackTrace();
}
}