3

I'm exporting dataTable (binding dataList) to excel file calling the following method by xlsWorkBookPrepare("c:\\export.xls");

Part of method:

public void xlsWorkBookPrepare(String file) throws IOException
{
  /* prepare of workbook */
  Workbook wb = new HSSFWorkbook();
  Map<String, CellStyle> styles = Style.createStyles(wb);
  ... 

  for (FoodList item : dataList)
  { 
    ...
  }  

  /* create file */
  FileOutputStream fileOut;
  try 
  {
    fileOut = new FileOutputStream(file);
    wb.write(fileOut);
    fileOut.flush();
    fileOut.close();
  } 
  catch (FileNotFoundException e) 
  {
    e.printStackTrace();
  }  
}

But the path is related to the server. How to save it on side of client??

SOLUTION (based on Rangi Lin answer):

HttpServletResponse res = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
res.setContentType("application/vnd.ms-excel");  
res.setHeader("Content-disposition",  "attachment; filename=test.xls"); 

try 
{
  ServletOutputStream fileOut = res.getOutputStream();
  wb.write(fileOut);
  fileOut.flush();
  fileOut.close();
} 
catch (FileNotFoundException e) 
{
  e.printStackTrace();
}  
FacesContext faces = FacesContext.getCurrentInstance();  
faces.responseComplete(); 
gaffcz
  • 3,469
  • 14
  • 68
  • 108
  • So you are generating this workbook server-side in some web-app? You need to persist it somewhere server side and redirect your client to it (with mime type set correctly and all that). Most browsers will pop up a "File Save" dialog thing and your client can choose where to store it. – Bob Kuhar Dec 13 '11 at 07:50
  • Jj, JSF web-app. I click the button and excel is automatically created on server (e.g. c:\export.xls). But I don't know how to start dialog to choose another path.. – gaffcz Dec 13 '11 at 07:57
  • I don't know didly squat about JSF, but there is a SO answer that seems to address your need: http://stackoverflow.com/questions/2914025/forcing-a-save-as-dialogue-from-any-web-browser-from-jsf-application – Bob Kuhar Dec 13 '11 at 08:00

1 Answers1

8

If I get you right, you need to transfer back the file to client through http. Instead of FileOutputStream, you can use getOutputStream() method in HttpServletResponse.

Code should look like this :

String fileName = "excel.xls";
HttpServletResponse response = getResponse(); // get ServletResponse
response.setContentType("application/vnd.ms-excel"); // Set up mime type
response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
OutputStream out = response.getOutputStream()
wb.write(out);
out.flush();

Note : I didn't test it, but you should able to get the idea.

Rangi Lin
  • 9,303
  • 6
  • 45
  • 71
  • Thank you, that's almost all I needed. I've updated my question. Please use it and update your answer and I'll accept it :) – gaffcz Dec 13 '11 at 08:18
  • @gaffcz : I am glad my answer helped. However, since I know nothing about JSF, I am not comfortable to add JSF code in my answer. I just point out you need a different output stream, so it dosen't matter which framework you are in :) – Rangi Lin Dec 13 '11 at 09:05