java动态生成excel打包下载
@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打包下载的更多相关文章
- 利用Java动态生成 PDF 文档
利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...
- Java 动态生成 PDF 文件
每片文章前来首小诗: 今日夕阳伴薄雾,印着雪墙笑开颜.我心仿佛出窗前,浮在半腰望西天. --泥沙砖瓦浆木匠 需求: 项目里面有需要java动态生成 PDF 文件,提供下载.今天我找了下有关了,系 ...
- Java 动态生成复杂 Word
Java 动态生成复杂 Word 阅读目录 1. 制作 Word 模版,将你需要动态生成的字段用${}替换.2. 将 Word文档保存为 xml .3. 引入项目. 项目中需要用 java 程序生成d ...
- phpexcel生成excel并下载
Loader::import('PHPExcel.Classes.PHPExcel'); // tp5中只需将phpexcel文件放入extend文件夹中,即可采用该方法引入,需要先 use thin ...
- Java Web 生成临时文件并下载(原)
概述:本文是 java 服务器端生成文件并下载的示例,并不完善,下载之后一般来说还需要删除临时文件. 注意:临时文件存放在 /WEB-INF/tmp 目录下,所以先要把 tmp 目录建起来. pu ...
- Java Web 生成临时文件并下载
转自: Java Web 生成临时文件并下载 概述:本文是 java 服务器端生成文件并下载的示例,并不完善,下载之后一般来说还需要删除临时文件. 注意:临时文件存放在 /WEB-INF/tmp 目 ...
- java动态生成HTML文件
在eclipse中,用java动态生成html文件. //用于存储html字符串 StringBuilder stringHtml = new StringBuilder(); try{ //打开文件 ...
- java动态生成带下拉框的Excel导入模板
在实际开发中,由于业务需要,常常需要进行Excel导入导出操作.以前做一些简单的导入时,先准备一个模板,再进行导入,单有十几. 二十几个导入模板时,往往要做十几.二十几个模板.而且,当在模板中需要有下 ...
- 使用node.js生成excel报表下载(excel-export express篇)
引言:日常工作中已经有许多应用功能块使用了nodejs作为web服务器,而生成报表下载也是我们在传统应用. java中提供了2套类库实现(jxl 和POI),.NET 作为微软的亲儿子更加不用说,各种 ...
随机推荐
- 【C语言入门教程】2.1 数据类型(5种基本数据类型),聚合类型与修饰符
C语言有5种基本的数据类型,分别为 字符型.整型.单精度浮点型.双精度浮点型.空类型. 在不同的操作系统或硬件平台中,这些数据类型的值域范围和所占用的内存是有差异的.这种差异影响了C语言的可移植性能, ...
- 怎样用Lodrunner测试WAP站点的性能(两种解决方案)
其实用IE就可以的!!!! 1.借助opera实现对WAP站点的录制 第一:安装opera软件 第二:Lodrunner选择Web(HTTP/HTML)协议 第三:Lodrunner的Applicat ...
- ubutu之Navicat安装
1.下载navicat111_premium_cs.tar.gz压缩包 2.进入压缩包所在目录,执行一下命令解压 tar -zxvf navicat111_premium_cs.r.gz 3.进入解压 ...
- _beginThreadex创建多线程解读【转】
_beginThreadex创建多线程解读 一.需要的头文件支持 #include <process.h> // for _beginthread() 需要的设置:Proj ...
- jQuery学习:用按键移动方块
<!doctype html> <html> <head> <meta charset="utf-8"> <style typ ...
- EF自动生成的模型edmx代码分析
edmx代码分析 本文分析Entity Framework从数据库自动生成的模型文件代码(扩展名为edmx). 1. 概述 本文使用的数据库结构尽量简单,只有2个表,一个用户表和一个分公司表(相当于部 ...
- 高性能Java网络框架 MINA
Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络 ...
- TP中手动加载类库
加载第三方类库,包括不符合命名规范和后缀的类库,以及没有使用 命名空间或者空间和路径不一致的类库.可手动加载. // 导入Org类库包 Library/Org/Util/Date.class.php类 ...
- (原创)在service中定时执行网络操作的几点说明
执行网络操作是耗时操作,即便是在service中也要放到子线程中执行 这里我用到了async-http-client框架来执行异步请求操作 计时用的java原生Timer和TimerTask类 本来这 ...
- centos7 关闭firewall安装iptables并配置
一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop fi ...