• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exporting a .csv file in JSF

 
Eugene Abarquez
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am developing an application that will have a functionality of exporting the data from an <h:datatable> to a .csv file (Microsoft Excel). Does anybody know how to do this in JSF? Helpful links, sample code, and suggestions will be greatly appreciated.

Thanks in advance.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be useful: http://balusc.xs4all.nl/srv/dev-jep-csv.html
 
Eugene Abarquez
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bauke! I will look into it.
 
Eugene Abarquez
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bauke! I will look into it.
 
gee sasi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pass your content to this method and call the method on click

If you specify the file name like test.xls you can directly view using xls(a dialog always appear click open to view the content in the xls format).

if you leave it blank user can only download the xls file.

public void exportExcelData(StringBuffer sb) {
byte[] csvData = sb.toString().getBytes();
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) FacesContext
.getCurrentInstance().getExternalContext().getResponse();
response.setHeader("Content-disposition",
"attachment; filename= test.xls");
response.setContentLength(sb.length());
response.setContentType("application/vnd.ms-excel");
try {

response.getOutputStream().write(csvData);
response.getOutputStream().flush();
response.getOutputStream().close();
context.responseComplete();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
csv != xls

And the application attached is dependent of the client's operating system environment and the content type set in response.setContentType().
 
E Paisley
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried teh above, but got the following error when I tried to change the response content type. Any suggestions?

javax.faces.el.EvaluationException: java.lang.IllegalArgumentException: text/csv

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic