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 作为微软的亲儿子更加不用说,各种 ...
随机推荐
- 【PHP面向对象(OOP)编程入门教程】18.__call()处理调用错误
在程序开发中,如果在使用对象调用对象内部方法时候,调用的这个方法不存在那么程序就会出错,然后程序退出不能继续执行.那么可不可以在程序调用对象内部 不存在的方法时,提示我们调用的方法及使用的参数不存在, ...
- Javascript高级程序设计——this、闭包、函数表达式
在javascript中函数声明会被提升,而函数表达式不会被提升.当函数执行时,会创建一个执行环境和相应的作用域链,然后利用arguments和其他的命名参数的值来初始化函数的活动对象,作用域链链中所 ...
- JavaScript的面临的9个陷阱
虽然不是什么很高深的技术问题,但注意一下,会使您的编程轻松些,即所谓make life easier. 笔者对某些陷阱会混杂一些评点. 1. 最后一个逗号 如这段代码,注意最后一个逗号,按语言学角 ...
- 用jQuery实现的一种网页内容呈现方式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CSU 1333 Funny Car Racing (最短路)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1333 解题报告:一个图里面有n个点和m条单向边,注意是单向边,然后每条路开a秒关闭b秒 ...
- iOS开发——多线程篇——NSOperation(基于GCD多线程编程),下载图片并合成新图片
一.NSOperation的基本概念1.简介NSOperation的作用配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperatio ...
- CSS浏览器兼容性与解析问题终极归纳
1.怪异模式问题:漏写DTD声明,Firefox仍然会按照标准模式来解析网页,但在IE中会触发怪异模式.为避免怪异模式给我们带来不必要的麻烦,最好养成书写DTD声明的好习惯. 2.IE6双边距问题:在 ...
- 23 使用环境 UsageEnvironment——Live555源码阅读
23 使用环境 UsageEnvironment——Live555源码阅读(三)UsageEnvironment 23 使用环境 UsageEnvironment——Live555源码阅读(三)Usa ...
- 使用jar命令打war包
1.打开cmd进入web项目发布文件夹 2.,输入jar -cvf qxpt.war * (*表示当前目录下所有子目录) 3,回车等待执行完成就可以了 4.如果web项目发布文件夹有多个文件夹,而打w ...
- Java对象访问 类的静态变量
Java类的静态变量用对象和类名都能访问,一般用类名,但如果用对象来访问静态变量呢,有何种效果? 测试一下: package JavaTest; public class test{ public s ...