I'm trying to implement downloading a dynamically created Excel spreadsheet, made with Apache POI. In JSF I'm using PrimeFaces, but for some reason I can't get it work with their FileDownload method. I've tried a few other things, but nothing seems to work for me.
This is currently my whole method:
public void generateExcel() throws IOException, InvalidFormatException {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
HttpServletResponse response = (HttpServletResponse) ec.getResponse();
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"" + "Spreadsheet.xls" + "\"");
OutputStream output = response.getOutputStream();
Workbook excelTabela = new HSSFWorkbook();
//Building the xls content here.
excelTabela.write(output);
excelTabela.close();
fc.responseComplete();
output.flush();
output.close();
}
It runs through this block of code (I checked with print statements) but nothing happens at all.
And this is the button I use for running this method:
<h:form>
<p:remoteCommand name="gen" actionListener="#{PrevoziGeneratorBean.generateExcel}" />
<h:commandButton type="button" onclick="gen()" value="Execute" />
</h:form>
I wrote this code based on the answer to this question but I can't get it to work.