posts - 68, comments - 19, trackbacks - 0, articles - 1

利用poi开源控件导出excel文件

Posted on 2012-03-05 16:37 viery 阅读(813) 评论(0)  编辑  收藏 所属分类: JavaEE
1在apache http://poi.apache.org/download.html上下载poi开源控件包。目前最稳定的版本:The latest stable release is Apache POI 3.7
2将poi.jar引入项目
3更改业务逻辑,将业务逻辑返回的查询到的对象集合存入session
request.getSession().setAttribute("pclist", list);
4建立ToExcelAction
action返回了一个excel报表对象后跳转到struts中配置的页面
    <action path="/toExcel"
                type
="com.vle.oa.action.ToExcelAction"
                parameter
="command"
                 scope
="request">
            
<forward name="excel" path="/pc/excel.jsp"/>
        
</action>

在这里我使用的是struts1,struts2同样原理一样最后编写跳转的jsp
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

    response.reset();
    out.clear();
    out
=pageContext.pushBody();

    response.setContentType(
"application/msexcel");   
     response.setHeader(
"Content-disposition","inline; filename=myexcel.xls"); //attachment   
        
      HSSFWorkbook wb 
= (HSSFWorkbook)request.getAttribute("wb");   
     OutputStream os 
= response.getOutputStream();   
     wb.write(os);   
     os.flush();   
     os.close();   
%>
效果如下



 



只有注册用户登录后才能发表评论。


网站导航: