欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POI 导出Excel表格 输出数据流 博客分类: Java POI 导出Excel表格 输出数据流

程序员文章站 2024-03-15 13:32:11
...

 

 FileOutputStream fileOut = new FileOutputStream("E:/workbook.xls");    
 workbook.write(fileOut);    
 fileOut.close();

 

 pName="*****";   
 response.reset();   
 response.setContentType("application/x-msdownload");   
 response.setHeader("Content-Disposition","attachment;filename="+new String(pName.getBytes("gb2312"),"ISO-8859-1")+".xls");   
 ServletOutputStream outStream=null;   
	  
try{   
	outStream = response.getOutputStream();   
	wb.write(outStream);   
    }catch(Exception e)   
	    {   
	     e.printStackTrace();   
	    }finally{   
	        outStream.close();   
	    }   

 

{
HSSFWorkbook workbook = ExportToExcel.userExportToExcel(list);
			
			HttpServletResponse response = ServletActionContext.getResponse();

	    	String fileName = "***.xls";
	    	
		    response.setContentType("application/octet-stream");
		    response.setHeader("name", fileName);
    		response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
			response.setHeader("Pragma", "public");
			response.setDateHeader("Expires", 0);
			response.setHeader("Content-disposition","attachment; filename=\""+URLEncoder.encode(fileName, "UTF-8")+ "\"");

			workbook.write(response.getOutputStream()); // 输出流控制workbook

			response.getOutputStream().flush();

			response.getOutputStream().close();
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}