生成的Excel出力到指定文件夹生成Zip文件再打包这些文件到页面下载

@At
@Ok("raw:stream")
@Permit(url = "/web/monitor/reportBillOrder/index")
public void exportExcel(@Param("q") String q, HttpServletResponse response) throws UnsupportedEncodingException {
QueryBillVo queryBillVo = Json.fromJson(QueryBillVo.class, q);
Map<String, List<HSSFWorkbook>> retMap = billNewService.exportBillOrder(queryBillVo);


response.setHeader("content-disposition",
"attachment;filename=" + URLEncoder.encode(DateUtils.toDate(new Date(), "yyyyMMddHHmmss") + ".gz", "UTF-8"));


OutputStream out = null;
try {


String fd = DateUtils.getCurTime3();


String fdPath = ZIP_TMP_PATH + fd;


// 出力到文件夹
for (Map.Entry<String, List<HSSFWorkbook>> entry : retMap.entrySet()) {


String eptName = entry.getKey();


// 创建文件夹
File f = new File(fdPath);
if (!f.exists()) {
f.mkdirs();
}


// 创建文件
String fileName = eptName + ".zip";
File zipFile = new File(f, fileName);
if (!zipFile.exists()) {
try {
zipFile.createNewFile();
} catch (IOException e) {
LOG.error(e.getMessage(),e);
e.printStackTrace();
}
}


ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));


List<HSSFWorkbook> wbLst = entry.getValue();
for (HSSFWorkbook workbook : wbLst) {
ZipEntry zipEntry = new ZipEntry(eptName+ "_" + DateUtils.getCurTime3() + ".xls");
zipOut.putNextEntry(zipEntry);
workbook.write(zipOut);
}


zipOut.flush();
zipOut.close();


}


// 创建 response 输出流
out = response.getOutputStream();
ZipOutputStream zip = new ZipOutputStream(out);


// 读取文件
File file = new File(fdPath);
if (file.isDirectory()) {
File[] files = file.listFiles();
InputStream input = null;
for (int i = 0; i < files.length; ++i) {
input = new FileInputStream(files[i]);
zip.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
int temp = 0;
while ((temp = input.read()) != -1) {
zip.write(temp);
}
input.close();
}
}
zip.close();

deleteDir(new File(fdPath));


} catch (IOException e) {
LOG.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值