export dataTable to a csv file problems with RichFaces 4.0
deiver2005 Jul 17, 2012 7:25 PMHi everybody,
I have a problem exporting a dataTable in a CSV file. it's looks fine but when I run my application, sometimes the window for export is showed and sometines dont, anyway, when I click the export button (in my jsf page), the screen freeze and then I have to shot down the IExplorer, and I dont know what is happening, because sometimes it work's but sometimes don't....
PLease help me....
here is my page:
<!-- DATA TABLE TO EXPORT -->
<h:panelGrid id="rtaSencilla" styleClass="panelInfo" style="text-align: center" columns="2">
<rich:dataTable var="consulta" value="#{consultaMasivaMBean.listConsultamasiva}"
rowKeyVar="numeroRegistro"
styleClass="altrowstable"
rendered="true">
<f:facet name="header">
<rich:columnGroup>
<rich:column styleClass="titulotabla"><h:outputLabel value="Ciudad"/></rich:column>
<rich:column styleClass="titulotabla"><h:outputLabel value="Barrio"/></rich:column>
</rich:columnGroup>
</f:facet>
<rich:column>
<h:outputText value="#{consulta.cm_ciudad}" />
</rich:column>
<rich:column>
<h:outputText value="#{consulta.cm_barrio}" />
</rich:column>
</rich:dataTable>
</h:panelGrid>
<!-- Button to xport -->
<h:commandButton value="Exportar a CSV" id="exportar"
action="#{consultaMasivaMBean.doExportSelectedDataToCSV}"
rendered="#{consultaMasivaMBean.showBotonExportar}"
styleClass="buttonl"></h:commandButton>
And here is my Bean:
public final void doExportSelectedDataToCSV() {
final StringBuffer sb = new StringBuffer();
for( ConsultaMasivaTable data : listConsultamasiva){
sb.append(data.getCm_ciudad());
sb.append(SEPARATOR);
sb.append(data.getCm_barrio());
sb.append(SEPARATOR);
//All my fields....
sb.append("\n");
}
byte[] csvData = null;
// in case you need some specific charset :
// here is an exemple with some standard utf-8
try {
csvData = sb.toString().getBytes("utf-8");
} catch (final UnsupportedEncodingException e1) {
// manage your encoding exception error exception here
}
final FacesContext context = FacesContext.getCurrentInstance();
final HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
//response.reset();
response.setHeader("Content-disposition", "attached; filename=\"consultaMasiva.csv\"");
//response.setHeader("Content-Length", String.valueOf(sb.length()));
// provided you want to ensure the file will be downloaded
// Some navigator may open CSV file directly if you specify this format ;
// up to you to do it another way if you don't mind that the navigator
// manages the file on itself
response.setContentType("application/force.download");
//response.setContentType("application/octet-stream");
try {
response.getOutputStream().write(csvData);
response.getOutputStream().flush();
response.getOutputStream().close();
context.responseComplete();
//Telling the framework that the response has been completed.
FacesContext.getCurrentInstance().responseComplete();
} catch (final IOException e) {
// mange another exception
}
}
any idea what is wrong??????
Thx.