1

我对如何以 CSV 或 Excel 格式导出 JSF 2.0 数据表一无所知。(我已经可以做到了,使用 primeFaces 导出器和 openfaces 新导出器(Nightly Build 版本 3.x)。但是我需要<h:dataTable />按照我的命令导出一些 JSF 数据表()。有人可以帮我完成下面的方法 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"/>
4

0 回答 0