java动态生成excel_java动态生成excel打包下载
@SuppressWarnings("unchecked")public String batchExport() throwsDBException{
@SuppressWarnings("unused")
List chkList= this.checkValueToList();//获取复选框的值
List srcfile=newArrayList();
SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHSS");
String path= sdf.format(newDate());
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= newLhjcOrgSelfQuery();
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= newExportUtils();
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(newDate());
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 voidzipFiles(List srcfile, File zipfile) {byte[] buf = new byte[1024];try{//Create the ZIP file
ZipOutputStream out = new ZipOutputStream(newFileOutputStream(zipfile));//Compress the files
for (int i = 0; i < srcfile.size(); i++) {
File file=srcfile.get(i);
FileInputStream in= newFileInputStream(file);//Add ZIP entry to output stream.
out.putNextEntry(newZipEntry(file.getName()));//Transfer bytes from the file to the ZIP file
intlen;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 voiddownFile(HttpServletResponse response,String serverPath, String str) {try{
String path= serverPath +str;
File file= newFile(path);if(file.exists()) {
InputStream ins= newFileInputStream(path);
BufferedInputStream bins= new BufferedInputStream(ins);//放到缓冲流里面
OutputStream outs = response.getOutputStream();//获取文件输出IO流
BufferedOutputStream bouts = newBufferedOutputStream(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动态生成excel打包下载相关推荐
- 前端批量生成二维码并打包下载
前端批量生成二维码并打包下载 项目中遇到一个紧急需求在没有后端配合的情况下,前端独自实现生成二维码并打包下载 生成二维码:下载 qrcodejs2 批量打包下载:下载 JSZip 和 FileSave ...
- java实现excel打包下载
最近公司让我做一个报表的打包下载功能,以前从来没有接触过,从问了下度娘,实现其功能的方式很多,其中我找到一篇博文就写得非常好,本人也是根据博文所讲做的,但是现在找不到他的链接地址了,,在这里就本人所做 ...
- java后台生成excel_Java后台生成Excel前台下载
Java后台通过poi生成HSSFWorkbook 对生成HSSFWorkbook 类型处理 转为文件流通过response 返回到前台 HSSFWorkbook hw = null; try{ hw ...
- java导入导出excel_Java导入导出Excel工具 easyexcel
Java导入导出Excel工具 easyexcel 做Java开发的同学,尤其是做管理后台的同学绝大多数都会接触到报表系统,这时候就少不了Excel的导入和导出了.Java解析生成Excel比较有名 ...
- java通用解析excel_java读取简单excel通用工具类
本文实例为大家分享了java读取简单excel通用工具类的具体代码,供大家参考,具体内容如下 读取excel通用工具类 import java.io.File; import java.io.File ...
- java重命名excel_Java重命名Excel工作表并设置工作表及标签颜色
码农公社 210.net.cn 210是何含义?10月24日是程序员节,1024 =210.210既 210 之意. 一份Excel文档中通常包含多个内容不同的工作表,而他们的默认名都为Sheet ...
- java对象转excel_Java对象和Excel转换工具XXL-EXCEL
一.简介 概述 XXL-EXCEL 是一个灵活的Java对象和Excel文档相互转换的工具. 一行代码完成Java对象和Excel之间的转换. 特性1.Excel导出:支持Java对象装换为Excel ...
- Vue批量生成二维码并打包下载
//[插件地址](https://github.com/Binaryify/vue-qr) <template v-for="(item, index) in qr_code_data ...
- java实现把数据写入到Excel并下载
引入依赖: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency><grou ...
最新文章
- 频率计设计——电路部分
- 某谷 P1654 OSU!
- 解决:Unable to access jarfile xxx-0.0.1-SNAPSHOT.jar
- win10更新1809版本后运行Dev-cpp, dos控制台字符乱码解决方法
- Request.GetOwinContext()打不到
- 什么是3D建模?一文帮助小白了解建模全流程!
- 解决502 bad gateway的九种方案及原因
- composition API重构mixin实践
- 从低位开始取出长整型变量s中奇数位上的数,依次构成一个新数放在t中
- 记一次云主机如何挂载对象存储
- 计算机老师新年贺卡祝福语,老师新年贺卡祝福语
- html如何给盒子设置位置,CSS盒子定位
- IBM:宏伟规划,拯救地球
- 自动登录QQ空间 --- Selenium打开带有xpath-helper的chrom
- html在线弹幕,HTML5 弹幕
- Docker技术研究
- 扎拉赞恩 服务器 微信群,魔兽世界怀旧服1至60级练级任务攻略——部落篇1
- 西南大学计算机应用基础试题及答案2019,2019年12月网络教育统考《计算机应用基础》单选题61...
- 中小企业在管理中常见的几大问题
- c语言程序设计王延梅,关于举办2013年京联合大学计算机应用大赛等六项.doc