Poi导出(防止内存溢出)的两种方式
来源:互联网 发布:做美工需要学什么 编辑:程序博客网 时间:2024/05/21 21:48
Num1 多Sheet 导出
/**
* 导出excel
*
* @throws Exception
*/
@Override
public boolean exportExcel(HttpServletRequest request, HttpServletResponse response, Map map) throws Exception {
boolean flag = false;
List<Operation_log> list = operation_logDao.queryAll(map);
if (list.size() > 0) {
flag = true;
// 创建excel对象 内存中缓存100条数据(100条最佳)
SXSSFWorkbook wb = new SXSSFWorkbook(100);
//临时文件进行压缩,建议不要true,否则会影响导出时间
wb.setCompressTempFiles(false);
List<Operation_log> list = operation_logDao.queryAll(map);
//样式
CellStyle style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);//水平居中
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直居中
Sheet sheet = null;
int count = 1;
for (int i = 0, j = 2; i < list.size(); i++, j++) {
if (i == 0 || i % 100000 == 0) {
sheet = wb.createSheet("操作日志第" + (count) + "篇");
sheet.setDefaultColumnWidth(30);
Row theme = sheet.createRow(0);
theme.createCell(1).setCellValue("操作日志");
theme.setRowStyle(style);
Row title = sheet.createRow(1);
title.createCell(0).setCellValue("操作人");
title.createCell(1).setCellValue("操作时间");
title.createCell(2).setCellValue("IP");
title.setRowStyle(style);
count++;
j = 2;
}
Row row = sheet.createRow(j);
row.createCell(0).setCellValue(list.get(i).getUser_name());
row.createCell(1).setCellValue(list.get(i).getCreateTime());
row.createCell(2).setCellValue(list.get(i).getIp());
// 清空内存中缓存的行数
if (i % 100 == 0) {
((SXSSFSheet) sheet).flushRows();
}
}
//以excel多sheet方式下载
OutputStream out = response.getOutputStream();
response.reset();
response.setHeader("content-disposition",
"attachment;filename=" + new String(("操作日志").getBytes(),"ISO8859-1") + ".xlsx");
response.setContentType("application/ms-excel");
out.flush();
wb.write(out);
out.close();
//以压缩包excel多sheet方式形式下载
// OutputStream out = response.getOutputStream();
// response.reset();
// ZipOutputStream zip = new ZipOutputStream(out);
// zip.setEncoding("gbk");
// zip.putNextEntry(new ZipEntry("操作日志.xlsx"));
// response.setContentType("application/octet-stream;charset=UTF-8");
// response.setHeader("Content-Disposition", "attachment;filename="
// +new String(("操作日志").getBytes(),"ISO8859-1")+".xlsx");
// zip.flush();
// wb.write(zip);
// out.close();
// zip.close();
}
return flag;
}
Num2 多exel导出
/**
* 导出excel
*
* @throws Exception
*/
@Override
public boolean exportExcel(HttpServletRequest request, HttpServletResponse response, Map map) throws Exception {
boolean flag = false;
List<Operation_log> list = operation_logDao.queryAll(map);
if (list.size() > 0) {
flag = true;
List<String> fileNames = new ArrayList();// 用于存放生成的文件名称
File zip = new File("操作日志.zip");// 压缩文件
SXSSFWorkbook wb =null;
// 生成excel
for (int j = 0, n = list.size() % 100000 == 0 ? list.size() / 100000 : (list.size() / 100000) + 1; j < n; j++) {
// 在内存中缓存100行数据(100测试最佳)
wb = new SXSSFWorkbook(100);
//临时文件进行压缩,建议不要true,否则会影响导出时间
wb.setCompressTempFiles(false);
Sheet sheet = wb.createSheet();
sheet.setDefaultColumnWidth(30);
CellStyle style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
String file = "操作日志-" + (j+1 )+ ".xlsx";
fileNames.add(file);
FileOutputStream o = new FileOutputStream(file);
Row title=sheet.createRow(0);
title.createCell(1).setCellValue("操作日志");
title.setRowStyle(style);
Row row = sheet.createRow(1);
row.createCell(0).setCellValue("操作人");
row.createCell(1).setCellValue("操作时间");
row.createCell(2).setCellValue("IP");
row.setRowStyle(style);
for (int i = 0, min = (list.size() - j * 100000) > 100000 ?100000
: (list.size() - j * 100000); i < min; i++) {
Operation_log log = list.get(100000 * (j) + i);
row = sheet.createRow((i+2));
row.createCell(0).setCellValue(log.getUser_name());
row.createCell(1).setCellValue(log.getCreateTime());
row.createCell(2).setCellValue(log.getIp());
if(i%100==0){
( (SXSSFSheet) sheet).flushRows();
}
}
o.flush();
wb.write(o);
o.close();
}
OutputStream out = response.getOutputStream();
response.reset();
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="
+new String(("操作日志").getBytes(),"ISO8859-1")+".zip");
ZipFiles(fileNames, zip);
FileInputStream inStream = new FileInputStream(zip);
byte[] buf = new byte[1024];
int readLength;
while (((readLength = inStream.read(buf)) != -1)) {
out.write(buf, 0, readLength);
}
inStream.close();
out.flush();
out.close();
}
return flag;
}
public static void ZipFiles(List<String> filename, File zipfile) {
byte[] buf = new byte[1024];
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
out.setEncoding("gbk");// 防止文件名乱码
for (int i = 0; i < filename.size(); i++) {
File file = new File(filename.get(i));
FileInputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
- Poi导出(防止内存溢出)的两种方式
- poi导出Excel的两种方式
- JAVA关于POI导出Excel内存溢出的解决方案
- poi导出excel内存溢出解决办法
- java导出2007excel--防止内存溢出
- itext导出报表防止内存溢出
- JXL和POI两种导出excel方式
- 两种内存溢出
- POI : 解决大批量数据导出Excel产生内存溢出的方案
- 使用Poi的HSSFWorkbook导出Excel大小限制,即内存溢出
- 解决POI导出Excel文件内存溢出问题
- 解决POI大数据导出Excel内存溢出、应用假死
- Bitmap的压缩处理(防止内存溢出)
- POI 内存溢出解决方案
- POI 内存溢出解决方案
- 防止加载图片内存溢出的方法
- Tweenlite防止内存溢出的写法
- Quartz的两种配置方式之上(内存方式)
- JavaScript对象的属性详解
- atitit.js浏览器环境下的全局异常捕获
- sql盲注入 python
- 为什么说php是弱类型好学,跟其他语言的区别
- 访问远程文件夹访问不了的问题之其中一个解决方案
- Poi导出(防止内存溢出)的两种方式
- [转]计算机视觉和模式识别的code
- 设计模式——适配器模式
- DOM解析(JavaWeb)
- Java异常处理机制
- NOIP2008传纸条
- 原码 反码 补码 详解
- Best Time to Buy and Sell Stock——最优买卖股票问题
- 基本国际格局