java导出多个excel压缩zip下载
来源:互联网 发布:噪声检测软件 编辑:程序博客网 时间:2024/06/05 19:20
package com.xx.emidas.activity.biz.utils;import java.io.BufferedOutputStream;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.ws.rs.core.Application;import jxl.Workbook;import jxl.format.Alignment;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import jxl.write.WriteException;import org.apache.commons.collections.CollectionUtils;import org.apache.struts2.ServletActionContext;import com.dianping.combiz.util.DateUtils;/** * 批量导出excel生成zip * @author hongwang.zhang 16-01-29 * */public class ExcelGenerateZIP {static HttpServletRequest request=ServletActionContext.getRequest();static int recordNum = 60000;public static void main(String[] args) {int s=getSheetCount(52300);System.out.println(1);}/** * 确定分页的个数 * * @param rCount 总得记录条数 * @return */ private static int getSheetCount(int rCount) { if (recordNum <= 0) return 1; if (rCount <= 0) return 1; int n = rCount % (recordNum); // 余数 if (n == 0) { return rCount / recordNum; } else { return (int) (rCount / (recordNum - 1)) + 1; } }//忽略警告@SuppressWarnings("unchecked") public static String batchExport(List list) throws IOException, WriteException{SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHSS"); String path = sdf.format(new Date()); //获取服务器web路径 String serverPath=request.getRealPath("/")+path; System.out.println(serverPath); //在服务器端创建文件夹 File file = new File(serverPath); if(!file.exists()){ file.mkdir(); //创建文件夹 }if(CollectionUtils.isEmpty(list)){return null;} Class clazz = list.get(0).getClass(); // 文件流 ByteArrayOutputStream ostream = new ByteArrayOutputStream(); WritableWorkbook book = null; //获取导出多少个excel int excelCount=getSheetCount(list.size()); //导出每个excel int fromIndex=0; int toIndex=list.size()<60000?list.size():60000; for(int excel=0;excel<excelCount;excel++){ List listExcel=null; if(toIndex>list.size()){ listExcel=list.subList(fromIndex, list.size()); }else{ listExcel=list.subList(fromIndex,toIndex); }try {book = Workbook.createWorkbook(new File(serverPath+"/" + new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date() )+"_"+(excel+1)+".xls"));} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} WritableSheet ws = null; Label lable = null; int rowIndex = 0; int rCount = listExcel.size() > 0 ? listExcel.size() : 0; // 行数 int sheetCount = getSheetCount(rCount);// 获取分页工作表的个数 for (int i = 0; i < sheetCount; i++) {// 初始化工作表的个数 book.createSheet("Sheet-" + i, i); // 添加一个工作表 } for (int index = 0; index < sheetCount; index++) { ws = book.getSheet(index);// 获取工作簿的第一个工作表 // 列标 WritableFont wfColumn = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false); WritableCellFormat wcfColumn = new WritableCellFormat(wfColumn); wcfColumn.setAlignment(Alignment.CENTRE); wcfColumn.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); int j = 0; // 标题 Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (!fields[i].getName().equalsIgnoreCase("serialVersionUID") && !fields[i].getName().equalsIgnoreCase("$jacocoData")) { lable = new Label(j++, 0, fields[i].getName(), wcfColumn); ws.addCell(lable); } } Method[] methods = clazz.getMethods(); List<Method> getMethods = new ArrayList<Method>(); for (j = 0; j < fields.length; j++) { for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equalsIgnoreCase( "get" + fields[j].getName())) { getMethods.add(methods[i]); } } } // 内容 WritableCellFormat wcfCell = new WritableCellFormat(); wcfCell.setAlignment(Alignment.CENTRE); wcfCell.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); int i = 0; for (int k = rowIndex; k < listExcel.size(); k++) { Object obj = null; obj = list.get(k); int r = 0; i++; if (recordNum == i) { rowIndex += recordNum - 1; break; } else { for (Method method : getMethods) { try { Object result = method.invoke(obj); if (result == null) { result = ""; } if (result instanceof Date) { result = DateUtils.format((Date) result, DateUtils.getSecondFormatter()); } lable = new Label(r++, i, result == null ? "" : result.toString(), wcfCell); ws.addCell(lable); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } } book.write(); book.close(); fromIndex+=60000; toIndex+=60000; } return serverPath; /* InputStream istream = new ByteArrayInputStream(ostream.toByteArray()); return istream;*/ //return null;}/** * 生成.zip文件; * @param path * @throws IOException */public static InputStream craeteZipPath(String path) throws IOException{ZipOutputStream zipOutputStream = null;String lj=path+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".zip";File file = new File(lj);zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)));File[] files = new File(path).listFiles();FileInputStream fileInputStream = null;byte[] buf = new byte[1024];int len = 0;if(files!=null && files.length > 0){for(File excelFile:files){String fileName = excelFile.getName();fileInputStream = new FileInputStream(excelFile);//放入压缩zip包中;zipOutputStream.putNextEntry(new ZipEntry(path + "/"+fileName));//读取文件;while((len=fileInputStream.read(buf)) >0){zipOutputStream.write(buf, 0, len);}//关闭;zipOutputStream.closeEntry();if(fileInputStream != null){fileInputStream.close();}}}if(zipOutputStream !=null){zipOutputStream.close();} File f = new File(lj); InputStream istream = new FileInputStream(f);return istream;}}
这个是利用jxl写的一个工具类 拿过来就可以直接用 jar我就不提供下载了网上很多的。里面部分功能不是很完善但是可以用的
try { String path=ExcelGenerateZIP.batchExport(listWinningRecordDTOList); InputStream data= ExcelGenerateZIP.craeteZipPath(path);/*InputStream data = JxlHelper.getExcel(listWinningRecordDTOList);*/actionContext.getContext().put("data", data);} catch (Exception e) {e.printStackTrace();}
protected ActionContext actionContext; 里面的list List<WinningRecordExportDTO> listWinningRecordDTOList 是这种格式的
<action name="selectPrizePackageInformationDown" class="com.dianping.emidas.activity.prizePool.SelectPrizePackageInformationAction" method="download"><result name="success" type="stream"><param name="contentType">application/x-zip-compressed</param><param name="inputName">data</param><param name="contentDisposition">filename=data.zip</param><param name="bufferSize">409600000</param></result></action>
这个是struts2的配置文件了
0 0
- java导出多个excel压缩zip下载
- java动态导出excel压缩成zip下载
- java导出多个excel表格,并压缩成zip输出
- java实现Excel压缩成Zip导出
- 导出Zip压缩Excel
- 【Java】Java实现zip压缩多个文件下载
- 把数据量大导出 放入多个excel 然后压缩成zip文件,导出
- java导出多个excel并打成zip包
- Java实现zip压缩多个文件下载
- java的多个excel打包成zip下载
- 导出下载多个Excel文件并打包成zip文件
- java 多excel下载 打成zip压缩包 程序中创建文件 响应到浏览器
- jeesite框架导出zip压缩包(包含一个excel文件和一张缩略图以及多个附件)
- 将EXCEL导出成ZIP压缩包
- java zip压缩多个文件
- JAVA实现对多个文件压缩为zip格式的下载
- JAVA ZIP压缩打包下载
- java 动态生成多个excel文件打成zip包供用户下载
- org.eclipse.core.runtime.CoreException: Plug-in org.eclipse.jdt.ui was unable to load class org.ecli
- POJ 2429 GCD & LCM Inverse(素数判定Miller-Rabin+素因子分解Pollard-rho)
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
- IOS:Swift基本语法
- tomcat配置301重定向
- java导出多个excel压缩zip下载
- 【程序员的爱情】彼岸花开谁又种下了执念
- python文本处理常用工具代码(一)
- MVC,MVP 和 MVVM 的图示
- 操作系统 之 银行家算法
- EXC_BAD_ACCESS的本质详解以及僵尸模式调试原理
- 项目中用到的转义字符
- 团队培养的四个方向
- PXE实现批量部署linux系统