将JSF dataTable导出为CSV或XLS。怎么样?

时间:2011-07-12 21:21:51

标签: jsf java-ee datatable export-to-excel export-to-csv

我对如何以CSV或Excel格式导出JSF 2.0 dataTable一无所知。 ( 我已经可以使用primeFaces导出器和openfaces新导出器(Nightly Build版本3.x)。但我需要按照我的命令导出一些JSF dataTables(<h:dataTable />)。 有人可以帮我完成下面的方法C

    package beans;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.openfaces.component.table.CSVTableExporter;
import org.openfaces.component.table.DataTable;
import org.openfaces.util.Faces;

/**
 *
 * @author Kyoshuu
 */
@ManagedBean(name = "exporterBean")
@ViewScoped
public class Exporter implements Serializable {

    public static final long serial = 1L;
    private List tablelist;
    private String componentID;

    public Exporter() {
    }

    public List getTablelist() {
        DataTable mytable = Faces.component("mytableform:mytable", DataTable.class);
        tablelist = mytable.getDisplayedRowDatas();
        return tablelist;
    }

    public void setTablelist(List tablelist) {
        this.tablelist = tablelist;
    }

    public String getComponentID() {
        return componentID;
    }

    public void setComponentID(String componentID) {
        this.componentID = componentID;
    }

    /**
     * Method C
     * Export a JSF core dataTable
     * @param id 
     */
    public void jsfTableExporter(String id){
         UIComponent table = (UIComponent) FacesContext.getCurrentInstance().getViewRoot().findComponent(id);
        // unfortunately UIComponent has no method to get table data.
    }

    /**
     * Method A
     * Export OpenFaces dataTable (version 3.x n.build)
     * @param id 
     */
    public void exportTable(String id) {
        setComponentID(id);
        DataTable mytable = Faces.component(id, DataTable.class);
        mytable.export(new CSVTableExporter());

    }

     /**
     * Method B
     * Export OpenFaces dataTable (version 3.x n.build)
     * @param id 
     */
    public void ofExporter(String id) {
        DataTable mytable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent(id);
        mytable.export(new CSVTableExporter());

    }
}

两种方法都有效:

 <o:commandButton action="#{exporterBean.exportTable('mytableform:mytable')}" value="Export Table"/>
                <o:commandButton action="#{exporterBean.ofExporter('mytableform:mytable')}" value="Export OF Table"/>

0 个答案:

没有答案