jsf Hssf 创建并下载 M$ Excel

jsf Hssf 创建并下载 M$ Excel

在jsf中通过FacesContext访问上下文;
Hssf通过 HSSFWorkbook 向输出流写入;
具体代码:

Bean:

public   class  TestBean  {
    
public String action() {
        HSSFWorkbook wb 
= new HSSFWorkbook();
        FacesContext context
=FacesContext.getCurrentInstance();
        HttpServletResponse response 
= (HttpServletResponse) context.getExternalContext().getResponse();
        response.reset();
        response.setContentType(
"application/ms-excel");
        
try {
            response.setHeader(
"Content-disposition""attachment;filename="+new String("下载Excel.xls".getBytes(),"iso-8859-1"));
            wb.write(response.getOutputStream());
        }
 catch (Exception e1) {
            
// TODO Auto-generated catch block
            e1.printStackTrace();
        }

        FacesContext.getCurrentInstance().responseComplete();
        
return null;
    }

}

以上只是一个空的简单的Excel 具体怎么创建符合项目要求的参看:HSSF GUIDE!
faces-config.xml:
< managed-bean >
        
< managed-bean-name > test </ managed-bean-name >
        
< managed-bean-class > yds.study.web.TestBean </ managed-bean-class >
        
< managed-bean-scope > session </ managed-bean-scope >
    
</ managed-bean >
jsp:
< f:view >
    
< h:form >
        
< h:commandButton  id ="button"  value ="下载"  rendered ="true"
            action
="#{test123.action}"   />
    
</ h:form >
</ f:view >

ok这样就可以了;)

你可能感兴趣的:(jsf Hssf 创建并下载 M$ Excel)