@SuppressWarnings("unchecked")
public String batchExport() throws DBException{ @SuppressWarnings("unused")
List chkList = this.checkValueToList();//获取复选框的值
List srcfile=new ArrayList(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHSS");
String path = sdf.format(new Date()); String serverPath = request.getSession().getServletContext().getRealPath("/");
//在服务器端创建文件夹
File file = new File(serverPath+path);
if(!file.exists()){
file.mkdir();
} for (int i = 0; i < chkList.size(); i++ ){
String t[] = chkList.get(i).split("\\|");
LhjcOrgSelfQuery lhjcunioncheckquery = new LhjcOrgSelfQuery();
lhjcunioncheckquery.setChnoticeId(t[0]);
lhjcunioncheckquery.setOrgId(t[1]);
lhjcunioncheckquery.setSelfChTempId(t[2]);
List> reportViews=commonService.getObjectPages("ExportLhjcunioncheckresultReportView", lhjcunioncheckquery);
String orgName = (String)commonService.get2Object("getUnioncheckOrgName", t[1]);
//生成excel的名字
String templateName = nyear+"深圳市党政机关信息安全检查结果-"+(orgName==null?"未知单位":orgName); String[] headArray = new String[]{"编码","检查项名称"," 检查内容 ","考核类型"," 检查结果 "," 备注 ","权重","得分"}; String[] fieldArray = new String[]{"CHECKITEMCODE","CHITEMNAME","CHCONTENT","REPORTTYPE","QUESTDESC","CHITEMDESC","REPORTWEIGHT","UNIONSCORE"}; ExportUtils exportUtils = new ExportUtils();
exportUtils.setTitle(templateName);
exportUtils.setHead(templateName);
exportUtils.setHeadArray(headArray);
exportUtils.setFieldArray(fieldArray);
try {
HttpServletResponse response = ServletActionContext.getResponse(); SimpleDateFormat sfm = new SimpleDateFormat("yyyy-MM-dd");
String filename = templateName + "_" + sfm.format(new Date()); String encodedfileName = new String(filename.getBytes(), "GBK");
//将生成的多个excel放到服务器的指定的文件夹中
FileOutputStream out = new FileOutputStream(serverPath+path+"\"+encodedfileName+".xls"); if(fileType.indexOf(",") != -1){
fileType = StringUtils.substringBefore(fileType, ",");
}
response.setHeader("Content-Disposition", " filename="" + encodedfileName + "." + fileType + """); //导出excel
if ("xls".equals(fileType) || "xlsx".equals(fileType)) {
exportUtils.exportExcel(reportViews,fileType,out);
} else if("doc".equals(fileType) || "docx".equals(fileType)){
exportUtils.exportWord(reportViews, fileType, out);
} else if("pdf".equals(fileType)){
exportUtils.exportPdf(reportViews, out);
} srcfile.add(new File(serverPath+path+"\"+encodedfileName+".xls")); } catch (Exception e) {
e.printStackTrace();
}
}
//将服务器上存放Excel的文件夹打成zip包
File zipfile = new File(serverPath+path+".zip");
this.zipFiles(srcfile, zipfile);
//弹出下载框供用户下载
this.downFile(ServletActionContext.getResponse(),serverPath, path+".zip");
return null;
} public void zipFiles(List srcfile, File zipfile) {
byte[] buf = new byte[1024];
try {
// Create the ZIP file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
// Compress the files
for (int i = 0; i < srcfile.size(); i++) {
File file = srcfile.get(i);
FileInputStream in = new FileInputStream(file);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(file.getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} public void downFile(HttpServletResponse response,String serverPath, String str) {
try {
String path = serverPath + str;
File file = new File(path);
if (file.exists()) {
InputStream ins = new FileInputStream(path);
BufferedInputStream bins = new BufferedInputStream(ins);// 放到缓冲流里面
OutputStream outs = response.getOutputStream();// 获取文件输出IO流
BufferedOutputStream bouts = new BufferedOutputStream(outs);
response.setContentType("application/x-download");// 设置response内容的类型
response.setHeader(
"Content-disposition",
"attachment;filename="
+ URLEncoder.encode(str, "GBK"));// 设置头部信息
int bytesRead = 0;
byte[] buffer = new byte[8192];
//开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
bouts.flush();// 这里一定要调用flush()方法
ins.close();
bins.close();
outs.close();
bouts.close();
} else {
response.sendRedirect("../error.jsp");
}
} catch (IOException e) {
e.printStackTrace();
}
}

java动态生成excel打包下载的更多相关文章

  1. 利用Java动态生成 PDF 文档

    利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...

  2. Java 动态生成 PDF 文件

    每片文章前来首小诗:   今日夕阳伴薄雾,印着雪墙笑开颜.我心仿佛出窗前,浮在半腰望西天.  --泥沙砖瓦浆木匠 需求: 项目里面有需要java动态生成 PDF 文件,提供下载.今天我找了下有关了,系 ...

  3. Java 动态生成复杂 Word

    Java 动态生成复杂 Word 阅读目录 1. 制作 Word 模版,将你需要动态生成的字段用${}替换.2. 将 Word文档保存为 xml .3. 引入项目. 项目中需要用 java 程序生成d ...

  4. phpexcel生成excel并下载

    Loader::import('PHPExcel.Classes.PHPExcel'); // tp5中只需将phpexcel文件放入extend文件夹中,即可采用该方法引入,需要先 use thin ...

  5. Java Web 生成临时文件并下载(原)

    概述:本文是  java 服务器端生成文件并下载的示例,并不完善,下载之后一般来说还需要删除临时文件. 注意:临时文件存放在 /WEB-INF/tmp 目录下,所以先要把  tmp 目录建起来. pu ...

  6. Java Web 生成临时文件并下载

    转自: Java Web 生成临时文件并下载 概述:本文是  java 服务器端生成文件并下载的示例,并不完善,下载之后一般来说还需要删除临时文件. 注意:临时文件存放在 /WEB-INF/tmp 目 ...

  7. java动态生成HTML文件

    在eclipse中,用java动态生成html文件. //用于存储html字符串 StringBuilder stringHtml = new StringBuilder(); try{ //打开文件 ...

  8. java动态生成带下拉框的Excel导入模板

    在实际开发中,由于业务需要,常常需要进行Excel导入导出操作.以前做一些简单的导入时,先准备一个模板,再进行导入,单有十几. 二十几个导入模板时,往往要做十几.二十几个模板.而且,当在模板中需要有下 ...

  9. 使用node.js生成excel报表下载(excel-export express篇)

    引言:日常工作中已经有许多应用功能块使用了nodejs作为web服务器,而生成报表下载也是我们在传统应用. java中提供了2套类库实现(jxl 和POI),.NET 作为微软的亲儿子更加不用说,各种 ...

随机推荐

  1. Swift2.1 语法指南——可空链式调用

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  2. js兼容注意事项--仅供参考

    做BS开发就难免会用到javascript,而每个浏览器对javascript的支持有不同.这就需要我们程序员去兼容他们,不然有些浏览器就无法运行我们的代码.就会造来客户的投诉,如果让BoSS知道了, ...

  3. Leonbao:MapKit学习笔记

    以下仅作了解, 实际使用以百度地图居多, 因为百度地图有动态路径规划等接口 MapKit学习笔记    原帖: http://www.cocoachina.com/bbs/read.php?tid-6 ...

  4. python del 注意点

    >>> del a[:] >>> a [] del也可以用于删除整个变量: >>> >>> del a 之后再引用名称 a 将会 ...

  5. OpenGL基础图形的绘制

    例一:绘制五角星 设五角星5个顶点分别为ABCDE,顶点到坐标轴原点距离为r,ABCDE各点用r表示,分别为 A(0,r); B(-r*sin(2/5*Pi),r*cos(2/5*Pi)); C(-r ...

  6. MyEclipse自带maven找不到或自己外置安装

    我用的MyEclipse2015 stable2.0版本. 1.删除相关SoftwareHelp --> choose components --> 进入后点 已安装的maven选项. 由 ...

  7. BZOJ 4665: 小w的喜糖

    Sol DP+容斥. 这就是一个错排的扩展...可是想到容斥却仅限于种数的容斥,如果种数在一定范围内我就会做了QAQ. 但是容斥的是一定在原来位置的个数. 发现他与原来的位置无关,可以先把每个同种的糖 ...

  8. 20 BasicTaskScheduler0 基本任务调度类基类(二)——Live555源码阅读(一)任务调度相关类

    这是Live555源码阅读的第二部分,包括了任务调度相关的三个类.任务调度是Live555源码中很重要的部分. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/ol ...

  9. Python自动化之配置Python模块国内镜像

    Linux环境 在~/.pip/pip.conf文件中添加或修改 [global] index-url = http://mirrors.aliyun.com/pypi/simple/ [instal ...

  10. [NOIP2015] 子串substring 题解

    [题目描述] 有两个仅包含小写英文字母的字符串A和B.现在要从字符串A中取出k个互不重叠的非空子串,然后把这k个子串按照其在字符串A中出现的顺序依次连接起来得到一个新的字符串,请问有多少种方案可以使得 ...