As the name implies, I need to put some data (which I received from the database) in an excel sheet, and then send it to the client side so that the user can save, open or cancel the action.
I saw several articles regarding this one coming up: How can I get a user to upload my file? (Java, MVC, Excel, POI) . Referring to the links provided by Stevens, I tried the following 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; }
Here, firstly, the WorkbookFactory not defined. Secondly, I could not correctly understand how the code works.
I also found this link: 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 . But here the excel file is saved on the server. I want the file not to be saved on the server side, it must go directly to the client side
(If that helps) I am using: frameworks 2 framework, hibernate
I am open to using other things like POI API, jQuery or any other useful stuff.
For some reason I cannot use displayTag .
Javascript would be my last application (although I implemented it) because it requires changing some default security settings in the browser (if this can be avoided, I am also open for javascript).
Please let me know how to go about this now.
Thanks!!
EDIT:
<result-types> <result-type name="jsp" class="org.apache.struts2.views.jsp"/> <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> </result-types> <action name="myActionName" class="package.myActionClass"> <result type="stream"> <param name="contentType">"application/vnd.ms-excel"</param> <param name="inputName">excelStream</param> <param name="contentDisposition">contentDisposition</param> <param name="bufferSize">1024</param> </result> </action>
Error performing action:
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