poi 导出大数据量数据到excel. 百万数据

	public void print() throws IOException{
		Long startTime = System.currentTimeMillis();
		HpaDAO oDao = (HpaDAO) this.getDao("daoHpa");
		List<Hpa> dataList = oDao.find("from Hpa o");
		Workbook wb = new SXSSFWorkbook(1000); //重点在这句 .每1000条写一个临时文件
		Sheet sheet = wb.createSheet("我的第一个工作簿");
		Row nRow =null;
		Cell nCell = null;
		int rowNo = 0;
		short colNo = 0;
		for(Hpa hpa : dataList){
			colNo = 0;
			nRow = sheet.createRow(rowNo++);
			
			nCell = nRow.createCell(colNo++);
			nCell.setCellValue(hpa.getId());
			
			nCell = nRow.createCell(colNo++);
			nCell.setCellValue(hpa.getBreast());
			
			nCell = nRow.createCell(colNo++);
			nCell.setCellValue(hpa.getAdipocytes());
			
			nCell = nRow.createCell(colNo++);
			nCell.setCellValue(hpa.getNegative());
			
			nCell = nRow.createCell(colNo++);
			nCell.setCellValue(hpa.getStaining());
			
			nCell = nRow.createCell(colNo++);
			nCell.setCellValue(hpa.getSupportive());
			if(rowNo%1000==0){
				System.out.println(rowNo);
			}
		}


		//生成excel文件
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//生成流对象
		wb.write(byteArrayOutputStream);
		
		//工具类 , 封装弹出下载框:
		String outFile = "bigdata.xls";
		DownloadBase downloadBase = new DownloadBase();
		HttpServletResponse response = ServletActionContext.getResponse();
		downloadBase.download(byteArrayOutputStream, response, outFile);
		
		
		Long stopTime = System.currentTimeMillis();
		System.out.println((stopTime-startTime)+"ms");

	}
Workbook wb = new SXSSFWorkbook(1000); //重点在这句 .每1000条写一个临时文件
		Sheet sheet = wb.createSheet("我的第一个工作簿");
		Row nRow =null;
		Cell nCell = null;
		int rowNo = 0;
		short colNo = 0;
		for(Hpa hpa : dataList){
			colNo = 0;
			nRow = sheet.createRow(rowNo++);
			
			nCell = nRow.createCell(colNo++);
			nCell.setCellValue(hpa.getId());
			
			nCell = nRow.createCell(colNo++);
			nCell.setCellValue(hpa.getBreast());
			
			nCell = nRow.createCell(colNo++);
			nCell.setCellValue(hpa.getAdipocytes());
			
			nCell = nRow.createCell(colNo++);
			nCell.setCellValue(hpa.getNegative());
			
			nCell = nRow.createCell(colNo++);
			nCell.setCellValue(hpa.getStaining());
			
			nCell = nRow.createCell(colNo++);
			nCell.setCellValue(hpa.getSupportive());
			if(rowNo%1000==0){
				System.out.println(rowNo);
			}
		}


		//生成excel文件
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//生成流对象
		wb.write(byteArrayOutputStream);
		
		//工具类 , 封装弹出下载框:
		String outFile = "bigdata.xls";
		DownloadBase downloadBase = new DownloadBase();
		HttpServletResponse response = ServletActionContext.getResponse();
		downloadBase.download(byteArrayOutputStream, response, outFile);
		
		
		Long stopTime = System.currentTimeMillis();
		System.out.println((stopTime-startTime)+"ms");

	}
本人实测104万的数据 用时18秒到 excel
注意点:
Workbook wb = new SXSSFWorkbook(1000); //重点在这句 .每1000条写一个临时文件
导入jar包:


核心包:
poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar


依赖包:
commons-beanutils-1.7.0.jar
commons-collections-3.0.jar
commons-lang-2.0.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
standard-1.0.2.jar
stax-api-1.0.1.jar
xmlbeans-2.3.0.jar
mysql-connector-java-5.1.10-bin.jar
junit-4.8.2.jar
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值