Hi,
I wanted to create an excel file using apache POI but I cant get this simple code to work.
I basically have this button in a page fragment
<af:commandButton text="Export" id="cb1"
actionListener="#{pageFlowScope.hrBean.handleExport}"/>
And my code in managed bean that handles the export.
public void handleExport(ActionEvent actionEvent) {
// Add event code here...
ExternalContext ectx =
FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response = (HttpServletResponse)ectx.getResponse();
try {
OutputStream out = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition",
"attachment; filename=" + "sample.xls");
HSSFWorkbook workbook = createExcelWorkbook();
workbook.write(out);
out.flush();
out.close();
FacesContext.getCurrentInstance().responseComplete();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public HSSFWorkbook createExcelWorkbook() {
HSSFWorkbook empWorkBook = new HSSFWorkbook();
Sheet empSheet = empWorkBook.createSheet("Employee List");
org.apache.poi.ss.usermodel.Row header = empSheet.createRow((short)0);
CreationHelper createHelper = empWorkBook.getCreationHelper();
header.createCell(0).setCellValue(createHelper.createRichTextString("Employee ID"));
header.createCell(1).setCellValue(createHelper.createRichTextString("First Name"));
header.createCell(2).setCellValue(createHelper.createRichTextString("Last Name"));
return empWorkBook;
}
What happens is that, when I click the button nothing happens but the page just refreshes.
Theres no pop-up on the browser that wants me to save or open the attached file.
Any idea please?
I have been following this blog http://technology.amis.nl/blog/1762/exporting-to-excel-from-any-adf-table
and checked that the code mathces mine.
JDEV 11G PS3