poi Struts2 导出文档

Struts2 配置文件:

        <action name="excel" class="com.exportexcel.action.ExportExcelAction">
           	<result name="success" type="stream">                
			<param name="contentType">application/vnd.ms-excel</param>    <!-- 注意这里的ContentType -->        
			<param name="inputName">excelStream</param>                   <!-- 这里需要和Action里的变量名一致 -->    
			<param name="contentDisposition">attachment;filename="Excel.xls"</param>    
			<param name="bufferSize">2048</param>               
		</result> 
        </action>

 Struts2 Action 代码:

	private ByteArrayInputStream excelStream;

                //关键代码 Begin
		 ByteArrayOutputStream   fileOut=new   ByteArrayOutputStream();
		  wb.write(fileOut); //wb 为 HSSFWorkbook 对象或者为poi 中的其它文档操作对象
		  fileOut.flush();
		  fileOut.close();
		  ByteArrayInputStream inPut = new ByteArrayInputStream(fileOut.toByteArray());
		  inPut.close();
		  this.setExcelStream(inPut);
              //关键代码 End

	public ByteArrayInputStream getExcelStream() {
		return excelStream;
	}

	public void setExcelStream(ByteArrayInputStream excelStream) {
		this.excelStream = excelStream;
	}

 

你可能感兴趣的:(struts2)