0

I have a webapplication that runs on websphere on a server from the office. The main page shows a table with data. When I choose export from the menu, it has to export the data to an excel sheet. I use apache poi for this. When I made my workbook, I write it via FileOutputStream. And here, I don't know exactly what I have to do. When I choose new File("C:\Temp\test.xls"), it will write the file on the server site. But I want that the application will export the file to excel on the client side folder. How can I do this?

Thx

Update: I searched a lot on this problem and they all answer the same, so I tried their solution:

    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();
      workbook.write(fileOut);
      fileOut.flush();
      fileOut.close();
    } 
    catch (FileNotFoundException e) {
        System.out.println("... FileNotFoundException ...");
        e.printStackTrace();
    } 
    catch (IOException e) {
        System.out.println("... IOException ...");
        e.printStackTrace();
    }  
    System.out.println("... excel file created ...");
    FacesContext faces = FacesContext.getCurrentInstance();  
    faces.responseComplete();

But still i do not get a popup screen where i can choose the direction on the client. So this solution doesn't work.

BartDepauw
  • 11
  • 1

1 Answers1

0

You need to write the file directly to the responses outputstream

see

OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(my_excel);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0){
    out.write(buffer, 0, length);
}
in.close();
out.flush();
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64