-
1. Re: excel export from richfaces datatable does not contain column names
nickarls Mar 11, 2010 9:09 AM (in response to nor340)Show some code.
-
2. Re: excel export from richfaces datatable does not contain column names
nklashok Jul 20, 2010 10:06 AM (in response to nor340)I need some sample code to generate CSV file from Datatable
-
3. Re: excel export from richfaces datatable does not contain column names
deiver2005 Jul 17, 2012 7:15 PM (in response to nklashok)maybe it; late but here the code you need:
you need a commandButton in your front
<h:commandButton value="Exportar a CSV" id="exportar"
action="#{consultaMasivaMBean.doExportSelectedDataToCSV}"
rendered="#{consultaMasivaMBean.showBotonExportar}"
styleClass="buttonl"></h:commandButton>and in your bean soeting like this:
public final void doExportSelectedDataToCSV() {
final StringBuffer sb = new StringBuffer();
for( ConsultaMasivaTable data : listConsultamasiva){
sb.append(data.getCm_barrio());
sb.append(SEPARATOR);
//All your fields ...sb.append("\n");
}
byte[] csvData = null;
// in case you need some specific charset :
// here is an exemple with some standard utf-8try {
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.setHeader("Content-disposition", "attached; filename=\"consultaMasiva.csv\"");
// 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");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
}