将 JSF 数据表导出为 CSV 或 XLS。如何?

发布于 2024-11-19 20:56:48 字数 2530 浏览 2 评论 0原文

我对如何以 CSV 或 Excel 格式导出 JSF 2.0 数据表一无所知。 ( 我已经可以使用 primeFaces 导出器和 openfaces 新导出器(Nightly Build 版本 3.x)来做到这一点。但我需要按照命令导出一些 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"/>

I am clueless about how to export a JSF 2.0 dataTable in CSV or Excel format. (
I already can do it, using primeFaces exporter and openfaces new exporter (Nightly Build version 3.x). But I need to export some JSF dataTables (<h:dataTable />) as I was ordered to.
Could someone please help me complete the Method C below.

    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());

    }
}

Both methods work:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文