I am generating a xls
in memory and it should appear for download , but at the end of the method the download dialog is not appearing.
xhtml
<a4j:commandButton styleClass="Button" value="Download"
execute="@form" render="@form"
action="#{lotesEnvBean.ImprimirLotesEnviados()}" >
</a4j:commandButton>
Bean
public void ImprimirLotesEnviados() {
try {
SimpleDateFormat simpledate = new SimpleDateFormat("ddMMyyyyHHMMSS");
java.util.Date dateFile = new java.util.Date();
//FileOutputStream outFile = new FileOutputStream (new File("/home/joao/app/tmp/envLote"+simpledate.format(dateFile)+".xls"));
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Lote Enviados");
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
HSSFCellStyle my_style_0 = workbook.createCellStyle();
my_style_0.setAlignment(HSSFCellStyle.ALIGN_CENTER);
my_style_0.setFont(font);
my_style_0.setBorderLeft( (short) 100);
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("ID Familia");
header.getCell(0).setCellStyle(my_style_0);
header.createCell(1).setCellValue("Famailia");
header.getCell(1).setCellStyle(my_style_0);
header.createCell(2).setCellValue("ID Produto");
header.getCell(2).setCellStyle(my_style_0);
header.createCell(3).setCellValue("Produto");
header.getCell(3).setCellStyle(my_style_0);
Integer count=1;
for (LoteEnvDetalheEntity dto :this.lotesEnvDetalhe ) {
Row dataRow = sheet.createRow(count);
dataRow.createCell(0).setCellValue(dto.getSeqfamilia());
dataRow.createCell(1).setCellValue(dto.getFamilia());
dataRow.createCell(2).setCellValue(dto.getSeqproduto());
dataRow.createCell(3).setCellValue(dto.getProduto());
count++;
}
//workbook.write(outFile);
//outFile.close();
System.out.println("Excel written successfully..");
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
ec.setResponseContentType("application/download");
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"lotes_enviados_" + simpledate.format(dateFile) + ".xls");
System.out.println("attachment; filename=lotes_enviados_" + simpledate.format(dateFile) + ".xls");
OutputStream output = ec.getResponseOutputStream();
output.write(workbook.getBytes());
output.flush();
output.close();
fc.responseComplete();
}catch (Exception e) {
e.printStackTrace();
}
}