现在的位置: 首页 > 综合 > 正文

大数据导出POI之SXSSFWorkbook

2018年05月16日 ⁄ 综合 ⁄ 共 1226字 ⁄ 字号 评论关闭

最近要求一次性导出50W以上的数据,TMD

开始用的是XSSFWorkbook瞬间内存暴涨到2G然后内存溢出了,嘎嘎……

查了API才知道POI还有SXSSFWorkbook专门处理大数据,爱死你了POI,虽然之前都爱JXL!

贴下伪代码供大家参考:

			OutputStream os = null;
			try {
				HttpServletResponse response = super.getResponse();
				response.setContentType("application/force-download"); // 设置下载类型
				String filename ="risk_event.xlsx";
				response.setHeader("Content-Disposition","attachment;filename=" + filename); // 设置文件的名称
				os = response.getOutputStream(); // 输出流
				SXSSFWorkbook wb = new SXSSFWorkbook(1000);//内存中保留 1000 条数据,以免内存溢出,其余写入 硬盘
		        //获得该工作区的第一个sheet   
				Sheet sheet1 = wb.createSheet("sheet1"); 
		        int excelRow = 0;
		        //标题行
		        Row titleRow = (Row) sheet1.createRow(excelRow++);
				for (int i = 0; i < columnList.size(); i++) {
					Cell cell = titleRow.createCell(i);  
	                cell.setCellValue(columnList.get(i));
				}
				
				for (int m = 0; m < cycleCount; m++) {
					List<List<String>> eventStrList = this.convertPageModelStrList();
					if (eventStrList!= null && eventStrList.size() > 0) {
						for (int i = 0; i < eventStrList.size(); i++) {
							//明细行
					        Row contentRow = (Row) sheet1.createRow(excelRow++);
							List<String> reParam = (List<String>) eventStrList.get(i);
							for (int j = 0; j < reParam.size(); j++) {
								Cell cell = contentRow.createCell(j);  
				                cell.setCellValue(reParam.get(j));
							}
						}
					}
				}
				wb.write(os);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (os != null) {
						os.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				} // 关闭输出流
			}

抱歉!评论已关闭.