使用POI下载Excle文件

开发环境  spring MVC4 + spring 4 + mybatis3.2 

知识点 

后台使用response.getInputStream() 流 ,返回页面 报错。

使用POI下载数据库的数据到本地电脑。


详细:

问题一:后台使用response.getInputStream() 流 ,返回页面 文件下载成功,页面显示报错。

原因:因为后台使用response.getInputStream() 流,二跳转jsp 显示jsp默认调用 out 对象,相当于responce.writer() 流,jsp不允许同时使用字节流个字符流,所以会报错。

解决:前台使用 单独一次请求 下载Execl文档,并且该方法 返回值 为 void。(我才用的方法)

方法二:将下载Excle文档写在jsp页面,在使用完response.getInputStream()之后 清除他,就可以(我没采用这种方法!)



java WEB 使用EXcel获取数据库信息步骤

1.导包  中央仓库下载 poi.jar

2.获取数据库数据  ,一般使用 List<Object>存储

3.创建Excel文件,设置一般属性,并添加数据,代码如下

	public HSSFWorkbook makeXls(List<AuthCode> lists){
		/**        * 以下为生成Excel操作        */     
		// 1.创建一个workbook,对应一个Excel文件       
		HSSFWorkbook wb = new HSSFWorkbook();
		// 2.在workbook中添加一个sheet,对应Excel中的一个sheet       
		HSSFSheet sheet = wb.createSheet("authCode表");       
		// 3.在sheet中添加表头第0行,老版本poi对excel行数列数有限制short      
		HSSFRow row = sheet.createRow((int) 0);       
		// 4.创建单元格,设置值表头,设置表头居中       
		HSSFCellStyle style = wb.createCellStyle();       
		// 居中格式       
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 设置表头       
		HSSFCell cell = row.createCell(0);       
		cell.setCellValue("个数");       
		cell.setCellStyle(style);         
		cell = row.createCell(1);       
		cell.setCellValue("授权码");       
		cell.setCellStyle(style);         
		cell = row.createCell(2);       
		cell.setCellValue("密码");       
		cell.setCellStyle(style);         
		cell = row.createCell(3);       
		cell.setCellValue("添加时间");       
		cell.setCellStyle(style);         
		cell = row.createCell(4);       
		cell.setCellValue("状态");       
		cell.setCellStyle(style); 
		cell = row.createCell(5);       
		cell.setCellValue("结束时间");       
		cell.setCellStyle(style); 
		//生成excel格式后要将数据写入excel:
		// 循环将数据写入Excel       
		for (int i = 0; i < lists.size(); i++) {         
			row = sheet.createRow((int) i + 1);         
			AuthCode ac= lists.get(i);         
			// 创建单元格,设置值         
			row.createCell(0).setCellValue(i+1);         
			row.createCell(1).setCellValue(ac.getScode());         
			row.createCell(2).setCellValue(ac.getPassword());         
			row.createCell(3).setCellValue(ac.getAddTimeStr());         
			row.createCell(4).setCellValue(ac.getFlag()==0?"未使用":ac.getFlag()==1?"已使用":"已作废");       
			row.createCell(5).setCellValue(ac.getEndTimeStr());         
			}
		return wb; 
		
	}


4.将内存中的Excel缓冲到request中传入浏览器端弹出下载页面。

	private void downloadXls(HSSFWorkbook wb, String daybatch,HttpServletResponse res) {
		// 之后将生成的Excel以流输出。
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			String fileName = "授权码" + daybatch + "批量导出";
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			wb.write(os);
			byte[] content = os.toByteArray();
			InputStream is = new ByteArrayInputStream(content);
			// 设置response参数,可以打开下载页面
			res.reset();
			res.setContentType("application/vnd.ms-excel;charset=utf-8");
			res.setHeader("Content-Disposition", "attachment;filename="
					+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
			ServletOutputStream out = res.getOutputStream();
			
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(out);
			byte[] buff = new byte[2048];
			int bytesRead;
			// Simple read/write loop.
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bis != null)
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (bos != null)
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

		}
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值