将 JSF 数据表导出为 CSV 或 XLS。如何?
我对如何以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论