随手笔记(四)———多文件导出并压缩上传服务器以及下载

在博主做完了整个项目所有的下载模板,导入导出之后,迎来了一个阶段性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();
		}
	}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值