ShowTable of Contents
Basic export
You can write data directly to the browser and tell him to download the data instead of displaying it.
Here an example with an CSV-file:
var exCon = facesContext.getExternalContext();
var writer = facesContext.getResponseWriter();
var response = exCon.getResponse();
response.setContentType("application/csv-tab-delimited-table;charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Disposition","attachment; filename="export.csv"");
var s = "name;lastname;officestreetaddress";
writer.write(s);
writer.endDocument();
Export to Excel
You can export data as a table in a HTML file and tell the browser to open it with Excel, simply by faking a filename with ".xsl" extension instead of ".html":
var exCon = facesContext.getExternalContext();
var writer = facesContext.getResponseWriter();
var response = exCon.getResponse();
response.setContentType("application/csv-tab-delimited-table;charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Disposition","attachment; filename="export.xsl"");
s = "<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"/></head><body>";
var s = "<table>";
s += "<tr><td>name</td><td>lastname</td><td>officestreetaddress</td></tr>";
s += "</table>";
s += "</body></html>";
writer.write(s);
writer.endDocument();