java导出多个excel表格,并压缩成zip输出

	/**
	 * 导出支付宝批量支付文件excel
	 * 
	 * @param name
	 * @param begintime
	 * @param endtime
	 * @param p
	 * @param l
	 * @param k
	 * @param request
	 * @param response
	 */
	@RequestMapping("exportApplyBatchExcel")
	public void exportApplyBatchExcel(String name, String begintime, String endtime, Integer p, Integer l, String k,
			HttpServletRequest request, HttpServletResponse response) {
		// if (auth.getCurrentUserId(k) == null) {
		// ResponseUtil.json(JsonUtil.toString(GenericResponses.TOKEN_LOSE),
		// response);
		// return;
		// }
		// 逐页查询数据,将所有数据导出到excel表中(注:此方法中不传p,l参数,使用的是service层中,默认的第1页开始,每页显示50条)
		Integer pp = 1;
		Long tt = 1l;
		String[] headers = { "批次号", "付款日期", "付款人email", "账户名称", "总金额(元)", "总笔数" };
		OutputStream out = null;
		while (true) {
			pp++;
			// 查询数据库
			ListResponse<?> listResponse = this.finApi.selectApplyBatch(name, begintime, endtime, p, l, k);
			// 获取总页数
			Long total = listResponse.getTotal();
			if (tt > 0) {
				tt = total - pp;
			} else {
				break;
			}
			// 获取查询结果,数据列表
			Object result = listResponse.getResult();
			// 类型转换
			if (result != null) {
				List<ApplyBatchMXVo> applyBatchMXVos = JsonUtil.readJsonList(JsonUtil.toString(result),
						ApplyBatchMXVo.class);
				// 导出
				try {
					// 设置导出excel文件
					out = response.getOutputStream();
					ZipOutputStream zipOutputStream = new ZipOutputStream(out);
					String fileName = "批量支付文件" + ".zip";
					response.setContentType("application/octet-stream ");
					response.setHeader("Connection", "close"); // 表示不能用浏览器直接打开
					response.setHeader("Accept-Ranges", "bytes");// 告诉客户端允许断点续传多线程连接下载
					response.setHeader("Content-Disposition",
							"attachment;filename=" + new String(fileName.getBytes("GB2312"), "ISO8859-1"));
					response.setCharacterEncoding("UTF-8");
					// 遍历填充数据
					for (ApplyBatchMXVo vo : applyBatchMXVos) {
						List<List<Object>> datas = new ArrayList<>();
						List<Object> data = new ArrayList<>();
						data.add(vo.getId());// 批次号
						data.add(vo.getCreateTime());// 付款时间
						data.add(vo.getPayAccount());// 付款人email
						data.add(vo.getPayName());// 账户名称
						data.add(vo.getTotalCost());// 总金额(元)
						data.add(vo.getTotalCount());// 总笔数
						datas.add(data);
						List<Object> data2 = new ArrayList<>();
						// 插入第二行表头
						data2.add("商户流水号");// 商户流水号
						data2.add("收款人email");// 收款人email
						data2.add("收款人姓名");// 收款人姓名
						data2.add("付款金额(元)");// 付款金额(元)
						data2.add("付款理由");// 付款理由
						datas.add(data2);
						for (ApplyMoneyMXVo amvo : vo.getApplyMoneyMXVos()) {
							List<Object> data3 = new ArrayList<>();
							data3.add(amvo.getApplyNo());// 商户流水号
							data3.add(amvo.getUserAccount());// 收款人email
							data3.add(amvo.getUserName());// 收款人姓名
							data3.add(amvo.getMoney());// 付款金额(元)
							data3.add(amvo.getPayCase());// 付款理由
							datas.add(data3);
						}
						// 导出文件zip压缩设置
						Workbook book = GenerateXmlUtil.generateCreateXsl(headers, datas, "批量支付文件");
						ZipEntry entry = new ZipEntry(vo.getId() + ".xls");
						zipOutputStream.putNextEntry(entry);
						book.write(zipOutputStream);
					}
					// 关闭输出流
					zipOutputStream.flush();
					zipOutputStream.close();
				} catch (Exception e) {
					e.printStackTrace();
					ResponseUtil.text(TraceUtil.trace(e), response);
				}
			}
			// 重新设置分页参数
			p = pp;
		}
	}





好的,对于Java导出多个Excel表生成多级目录压缩包的需求,您可以按照以下步骤实现: 1. 首先,您需要使用Java操作Excel表格,这里推荐使用Apache POI库。您可以使用POI库读取Excel表格中的数据,也可以使用POI库创建新的Excel表格。 2. 接下来,您需要将生成的Excel表格按照要求组织成多级目录结构。您可以使用Java的File类来创建文件夹和文件,将生成的Excel表格存放在对应的文件夹中。 3. 最后,您需要使用JavaZipOutputStream类将生成的文件夹和文件压缩成一个多级目录的压缩包ZipOutputStream类可以将多个文件或文件夹压缩成一个压缩包,同时可以设置压缩包的名称和路径。 以下是一个示例代码,用于将两个Excel表格导出并生成多级目录压缩包: ```java import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; public class ExcelExport { public static void main(String[] args) { // 创建两个Excel表格 Workbook workbook1 = new HSSFWorkbook(); Sheet sheet1 = workbook1.createSheet("Sheet1"); Row row1 = sheet1.createRow(0); Cell cell1 = row1.createCell(0); cell1.setCellValue("Hello"); Workbook workbook2 = new HSSFWorkbook(); Sheet sheet2 = workbook2.createSheet("Sheet2"); Row row2 = sheet2.createRow(0); Cell cell2 = row2.createCell(0); cell2.setCellValue("World"); // 创建目录结构 File dir = new File("export"); dir.mkdir(); File subDir1 = new File(dir, "subdir1"); subDir1.mkdir(); File subDir2 = new File(subDir1, "subdir2"); subDir2.mkdir(); // 保存Excel表格到文件夹中 try { FileOutputStream fileOut1 = new FileOutputStream(new File(subDir2, "file1.xls")); workbook1.write(fileOut1); fileOut1.close(); FileOutputStream fileOut2 = new FileOutputStream(new File(subDir2, "file2.xls")); workbook2.write(fileOut2); fileOut2.close(); } catch (IOException e) { e.printStackTrace(); } // 压缩文件夹为多级目录压缩包 String zipFileName = "export.zip"; try { FileOutputStream fileOut = new FileOutputStream(zipFileName); ZipOutputStream zipOut = new ZipOutputStream(fileOut); addToZip(subDir1, "", zipOut); zipOut.close(); fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } private static void addToZip(File file, String parentDir, ZipOutputStream zipOut) throws IOException { String filePath = parentDir + file.getName(); if (file.isDirectory()) { if (!filePath.isEmpty()) { ZipEntry zipEntry = new ZipEntry(filePath + "/"); zipOut.putNextEntry(zipEntry); zipOut.closeEntry(); } for (File subFile : file.listFiles()) { addToZip(subFile, filePath + "/", zipOut); } } else { FileInputStream fileIn = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(filePath); zipOut.putNextEntry(zipEntry); byte[] buf = new byte[1024]; int len; while ((len = fileIn.read(buf)) > 0) { zipOut.write(buf, 0, len); } fileIn.close(); zipOut.closeEntry(); } } } ``` 在这个示例代码中,我们创建了两个Excel表格,然后将它们保存在一个名为"export"的文件夹中,同时按照要求组织了多级目录结构。最后,我们使用ZipOutputStream类将"export"文件夹压缩成一个名为"export.zip"的多级目录压缩包
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值