title: Download Excel file in browser using java
body: <p>I have seen this question so many places, But still not able to solve my requirement.</p>
  
  <p>I have written code to generate excelsheet in Apache POI in ADF using Java and need to download it in browser as application will be in server side not always in my local machine.</p>
  
  <p>Initially I tried code:</p>
  
  <pre><code>Desktop.getDesktop().open(new File(home + "/Downloads/" + "excel1" + filename + ".xls"));
  </code></pre>
  
  <p>It was downloading. But it is downloading only on my machine. It's not downloading on other machine.</p>
  
  <p>Another solution:</p>
  
  <pre><code>file = new File(home + "/Downloads/" + "excel" + filename + ".xls");
  Runtime.getRuntime().exec("cmd.exe /C start " + file);
  </code></pre>
  
  <p>But it is not working..</p>
  
  <p>Another solution:</p>
  
  <pre><code>FacesContext fc = FacesContext.getCurrentInstance();
  HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
  response.reset();
  response.setContentType(“application/vnd.ms-excel”);
  response.setHeader(“Content-Disposition”, “attachment; filename=\"excel.xlsx");
  workbook.write(response.getOutputStream());
  fc.responseComplete();
  </code></pre>
  
  <p>But this solution also does not work.</p>