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 block
e1.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的配置文件了