467,145 Members | 944 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,145 developers. It's quick & easy.

Export jsp output to excel

I am having reports generated in jsp with HTML codings in tabular format. On report displayed page I have to use one button , which on click has to export to excel sheet.

I tried using below code.

String mimeType = "application/vnd.ms-excel";
response.setContentType(mimeType);
response.setHeader("content-disposition","attachment; filename=totalpurgeexcel.xls");

But the problem I am facing is , in the report page, header and footer are also their. So while exporting header and footer are also saved in excel.

Another problem is I have a main page where link to particular report is displayed. When I click on one report link itself its showing me popup for saving, its not displaying the report generated page, all datas in that are saving in excel.

Please help me with suitable solution.
Mar 14 '08 #1
  • viewed: 16954
Share:
1 Reply
Dököll
Expert 2GB
Greetins!

You can try something like this, pretty generic code but you will need Apache's API called POI to b able to relate to Microsoft's file types, a few simple jar files. You will also need Tomcat Server for this to work. Add the jar files in Tomcat lib directory:

Expand|Select|Wrap|Line Numbers
  1. ....Call this jsp file whatever ou want for starters
  2.  
  3. <%@ page language="java" import="java.io.*" %>
  4.  
  5. ...Begin your form post to AddToExcel.jsp page
  6. ...A simple textfield t record the actuall excel file name
  7.  
  8.         <form method="post" action="AddToExcel.jsp">
  9.         <p><font color="#800000" size="5">Enter File Name Here:</font>
  10.         <input type="text" name="excel_sheet_name" size="20"></p>
  11.         <p><input type="submit" value="Submit"         
  12.                onclick="document.location='AddToExcel.jsp';"/>
  13.          </p>
  14.  
  15.         </form>
  16.  
  17.  
Here is your AddToExcel.jsp:

Expand|Select|Wrap|Line Numbers
  1.  
  2. ...import necessary libraries
  3. <%@ page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
  4.     <%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
  5.       <%@ page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
  6.        <%@ page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
  7.  
  8. ...import io, tells system to recieve a file
  9.  
  10.           <%@ page import="java.io.*" %>
  11.  
  12. ...grab te excel file name
  13.  
  14. <%String name=request.getParameter("excel_sheet_name");%>
  15.  
  16. ...create a workbook, a couple of cells, name yoursheet<%try{
  17.  
  18.            HSSFWorkbook wb = new HSSFWorkbook();
  19.            HSSFSheet sheet = wb.createSheet("MyFirstExcelSheet");
  20.            HSSFRow row = sheet.createRow((short)0);
  21.            HSSFCell cell = row.createCell((short)0);
  22.  
  23. ...add values to your cells, notice you are adding to 4 cells here
  24.  
  25.            cell.setCellValue(1);
  26.            row.createCell((short)1).setCellValue("AddSomeTextHere");
  27.            row.createCell((short)2).setCellValue("AddSomeTextHere");
  28.            row.createCell((short)3).setCellValue("AddSomeTextHere");
  29.            FileOutputStream fileOut = new FileOutputStream("C:\\"+name+".xls");
  30.  
  31.           wb.write(fileOut);
  32.           fileOut.close(); 
  33.           }catch ( Exception ex ){ 
  34.  
  35. %>
  36.  
  37. Excel File added...
  38.  
and there you have it... Hope this gives you a idea. You can then grab data from say an Access database and load to excel ta way, as needed:-)

Happy coding!
Dec 13 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Jason | last post: by
1 post views Thread by David | last post: by
8 posts views Thread by DC Gringo | last post: by
13 posts views Thread by Hemant Sipahimalani | last post: by
1 post views Thread by JawzX01 | last post: by
reply views Thread by Mike | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.