title: JAVA : Exporting Data (from database) to excel and sending it to client side
body: <p>As the title suggest, I need to put some data (which I have got from database) into an excel sheet, and then send it to client side so that user can save , open or cancel the action.</p>
  
  <p>I have seen some articles regarding this, the closest one being : <a href="https://stackoverflow.com/q/4834446/870767">How can I get the user to download my file? (Java, MVC, Excel, POI)</a>. Referring to links provided by Stevens I tried out the following code : </p>
  
  <pre><code>public String execute(){
      setContentDisposition("attachment; filename=\"" + ename + "\"");
      try{
          ServletContext servletContext = ServletActionContext.getServletContext();
          String filePath = servletContext.getRealPath("/WEB-INF/template/excel/mytemplate.xls");
          File file = new File(filePath);
          Workbook wb = WorkbookFactory.create(new FileInputStream(file));
          Sheet sheet = wb.getSheetAt(0);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          wb.write(baos);
          InputStream excelStream;
          excelStream = new ByteArrayInputStream(baos.toByteArray());
      }catch(Exception e){
          System.out.println(e.getMessage());
      }
      return SUCCESS;
  }
  </code></pre>
  
  <p>Here firstly <code>WorkbookFactory</code> is not defined. Secondly, I could not understand properly how the code is working.</p>
  
  <p>I also found this link : <a href="http://www.roseindia.net/answers/viewqa/Java-Beginners/14930-How-to-export-data-from-database-to-excel-sheet-by-using-java--in-standalone-project.html" rel="nofollow noreferrer">http://www.roseindia.net/answers/viewqa/Java-Beginners/14930-How-to-export-data-from-database-to-excel-sheet-by-using-java--in-standalone-project.html</a>. But here the excel file gets saved on the server. I want that the file should not be saved on the server side, it should directly go to client side</p>
  
  <p>(If it helps) I am using : struts 2 framework, hibernate</p>
  
  <p>I am open to using other things like POI API, jQuery or any other good stuff.</p>
  
  <p>I can not use <code>displayTag</code> for some reason.</p>
  
  <p>Javascript would be my last resort (although I have implemented with it) because it requires changing some default security settings of the browser (If this can be avoided I am open to javascript as well).</p>
  
  <p>Please advise how should I go about this now.</p>
  
  <p>Thanks!!</p>
  
  <p><strong>EDIT :</strong></p>
  
  <pre><code>    &lt;result-types&gt;
          &lt;result-type name="jsp" class="org.apache.struts2.views.jsp"/&gt;
          &lt;result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/&gt;
          &lt;result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/&gt;
      &lt;/result-types&gt;
      &lt;action name="myActionName" class="package.myActionClass"&gt;
           &lt;result type="stream"&gt;
              &lt;param name="contentType"&gt;"application/vnd.ms-excel"&lt;/param&gt;
              &lt;param name="inputName"&gt;excelStream&lt;/param&gt;
              &lt;param name="contentDisposition"&gt;contentDisposition&lt;/param&gt;
              &lt;param name="bufferSize"&gt;1024&lt;/param&gt;
           &lt;/result&gt;
      &lt;/action&gt;
  </code></pre>
  
  <p>The error while executing action: </p>
  
  <pre><code>java.lang.reflect.InvocationTargetException
  
  java.lang.IncompatibleClassChangeError: Class org.apache.poi.hssf.usermodel.HSSFWorkbook does not implement the requested interface org.apache.poi.ss.usermodel.Workbook
  </code></pre>