Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Unable to download File Attachment with Apache POI

Neliel-Oracle-Newbie-OracleDec 22 2011 — edited Dec 23 2011
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 20 2012
Added on Dec 22 2011
6 comments
1,292 views