First, ace datatable needs to bound to the backing bean:
 
  <ace:dataTable id="summaryTable" var="summary"   
          value="#{summaryReportBean.summaryList}"   
         binding="#{summaryReportBean.summaryDataTable}">  
  . . .  
  </ace:dataTable>  
And the corresponding bean:
 
 import java.util.List 
 import org.springframework.beans.factory.annotation.Autowired; 
 import org.springframework.context.annotation.Scope;  
 import org.springframework.stereotype.Component;  
 import com.jv.service.SummaryReportService;
 import org.icefaces.ace.component.datatable.DataTable;
 public class SummaryReportBean {  
 @Autowired
 private SummaryReportService reportService;      
 private DataTable summaryDataTable;  
      
 public List<ReportLine> getSummaryList() {  
  return reportService.generateReport();  
 } 
     
 public DataTable getSummaryReportTable() {  
  return summaryReportTable;  
 }  
        
 public void setSummaryReportTable(DataTable summaryReportTable) {  
  this.summaryReportTable = summaryReportTable;  
 }     
}
Now, we'll add a button which will trigger the CSV export file to be generated:
 
  <h:commandButton id="SummaryReportExportButton"
      value="Export Summary Report to CSV" 
      rendered="#{not empty summaryReportBean.summaryList}"
      actionListener="#{summaryReportBean.exportSummaryReportToCSV}" /> 
And here is the code to generate the CSV file:
 
import javax.faces.context.FacesContext;
import org.apache.commons.io.IOUtils;
import org.icefaces.ace.component.dataexporter.CSVExporter;
import org.icefaces.ace.component.datatable.DataTable;
import org.icefaces.application.ResourceRegistry;
...
public void generateReport(ActionEvent event) throws IOException {
 CSVExporter exporter = new CSVExporter();
 String resourcePath = 
      exporter.export(FacesContext.getCurrentInstance(), 
                 summaryDataTable, null, false, 
                 null, null, null, null, true, false, false);
     InputStream inputStream = 
         ResourceRegistry.getResourceByPath(
      FacesContext.getCurrentInstance(), resourcePath)
    .getInputStream();
  
 String report = IOUtils.toString(inputStream));
 
 saveReportToFile(report);
}
CSVExporter is a class ICEFaces uses to generate CSV exports. Here is the export method signature:
public String export(FacesContext facesContext, 
                     DataTable table, 
                     String filename, 
                     boolean pageOnly, 
                     int[] excludeColumns, 
                     String encodingType, 
                     MethodExpression preProcessor,
                     MethodExpression postProcessor, 
                     boolean includeHeaders, 
                     boolean includeFooters, 
                     boolean selectedRowsOnly) throws IOException { 
CVSExporter wraps the resulting CSV text into org.icefaces.ace.component.dataexporter.ExporterResource and puts it into org.icefaces.application.ResourceRegistry. So all this is needed to do is to fetch the resource from the registry, get the InputStream and write it to a desired server location.
No comments:
Post a Comment