File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Exporting a .csv file in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Exporting a .csv file in JSF" Watch "Exporting a .csv file in JSF" New topic
Author

Exporting a .csv file in JSF

Eugene Abarquez
Ranch Hand

Joined: May 18, 2006
Posts: 211
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.


There's so much to learn in this industry, and not everybody has the necessary interest.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
This might be useful: http://balusc.xs4all.nl/srv/dev-jep-csv.html
Eugene Abarquez
Ranch Hand

Joined: May 18, 2006
Posts: 211
Thanks Bauke! I will look into it.
Eugene Abarquez
Ranch Hand

Joined: May 18, 2006
Posts: 211
Thanks Bauke! I will look into it.
gee sasi
Greenhorn

Joined: Nov 07, 2006
Posts: 5
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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Sep 22, 2008
Posts: 14
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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exporting a .csv file in JSF