package action;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
public class Downlaod {
@Context
HttpServletResponse response;
@Context
HttpServletRequest request;
/**
*
* 多sheet导出数据
* @Title: download
* @Description: TODO (这里用一句话描述这个方法的作用)
* @Author: xubowen
* @Create Date: 2015年11月24日 下午4:07:16
* @History: 2015年11月24日 下午4:07:16 xubowen Created.
*
* @throws IOException
*
*/
@GET
@Path("/download")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public void download() throws IOException{
long d1 = Calendar.getInstance().getTimeInMillis();// 结束时间
SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory,
int maxSheetSize = 20480;//每个sheet页最多写20480条数据
int currnum = 50000;//总数据量
BigDecimal b1 = new BigDecimal(currnum);
BigDecimal b2 = new BigDecimal(maxSheetSize);
int t = b1.divide(b2, BigDecimal.ROUND_UP).intValue();
for(int j =1;j<t+1;j++){//决定excel的个数
Sheet sh = wb.createSheet();
wb.setSheetName(j-1, "data-sheet-"+j);
int max = j* maxSheetSize<=currnum?maxSheetSize:currnum-(j-1)*maxSheetSize;
for (int rownum = 0; rownum <max; rownum++) {
Row row = sh.createRow(rownum);
for (int cellnum = 0; cellnum <20; cellnum++) {
Cell cell = row.createCell(cellnum);
cell.setCellValue("data中文" + rownum + "," + cellnum);
}
}
}
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("application/octet-stream;charset=gb2312");
response.addHeader("Content-Disposition", "attachment;filename=data.zip");//设置下载文件名称
OutputStream ouputStream = response.getOutputStream();
ZipOutputStream zip = new ZipOutputStream(ouputStream);
ZipEntry entry = new ZipEntry("textpoi9.xlsx");//压缩包中具体的workbook的名字
zip.putNextEntry(entry);
wb.write(zip);
ouputStream.flush();
ouputStream.close();
long d2 = Calendar.getInstance().getTimeInMillis();// 结束时间
System.out.println("导出结束");
System.out.println("导出 5Wx20的数据共花费时间:" + (d2 - d1));
}
/**
*
* 单一sheet导出数据
* @Title: downloadSingle
* @Description: TODO (这里用一句话描述这个方法的作用)
* @Author: xubowen
* @Create Date: 2015年11月24日 下午4:06:55
* @History: 2015年11月24日 下午4:06:55 xubowen Created.
*
* @throws IOException
*
*/
@GET
@Path("/download-single")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public void downloadSingle() throws IOException{
long d1 = Calendar.getInstance().getTimeInMillis();// 结束时间
SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory,
int currnum = 50000;
Sheet sh = wb.createSheet();
wb.setSheetName(0, "data-sheet-");
for (int rownum = 0; rownum <currnum; rownum++) {
Row row = sh.createRow(rownum);
for (int cellnum = 0; cellnum <20; cellnum++) {
Cell cell = row.createCell(cellnum);
cell.setCellValue("data中文" + rownum + "," + cellnum);
}
}
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("application/octet-stream;charset=gb2312");
response.addHeader("Content-Disposition", "attachment;filename=data.zip");
OutputStream ouputStream = response.getOutputStream();
ZipOutputStream zip = new ZipOutputStream(ouputStream);
ZipEntry entry = new ZipEntry("textpoi9.xlsx");
zip.putNextEntry(entry);
wb.write(zip);
ouputStream.flush();
ouputStream.close();
long d2 = Calendar.getInstance().getTimeInMillis();// 结束时间
System.out.println("导出结束");
System.out.println("导出 5Wx20的数据共花费时间:" + (d2 - d1));
}
}
转载于:https://my.oschina.net/Blanicy/blog/534916