472,798 Members | 1,271 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,798 software developers and data experts.

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
1 18131
Dököll
2,364 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

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

Similar topics

0
by: Jason | last post by:
Can anyone point me to some sample code or tutorial that shows how I can export data from a database to an Excel spreadsheet that is already created on the server? I have an Excel spreadsheet...
6
by: Robin Cushman | last post by:
Hi all, I need some help -- I'm working with an A2K database, using DAO, and am trying to read records into a Crystal Report and then export it to a folder on our network as an Excel...
1
by: David | last post by:
(dhl)Using 2K. I have a query I want to export to excel. There are 35,000+ lines for this query. I created a macro to run on a schedule to export the excel file to a network shared drive. I can...
8
by: DC Gringo | last post by:
I have a simple button that should open another window and export a datagrid to an Excel file. I'm getting: "Name 'window' is not declared." What do I need to declare or import? <INPUT...
0
by: kieran | last post by:
Hi, I have an 'export to excel' button on my Datagrid. All works well except that on my Datagrid, I have sub total rows. The subtotal rows use colspan so that the subtotals are given above...
13
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"...
1
by: JawzX01 | last post by:
Hello All, First, thank you for any help you can provide. I'm trying to do a simple export to excel. I've used the classic code that is all over the internet, and of course it worked without a...
0
by: Mike | last post by:
On my page I'm using an updatepanel, updateProgress, and a ModalProgress, everything works great But when I try to export my GridView to excel I'm getting the following error message: ...
0
by: mustaqueahmad | last post by:
Hi All, I am getting export to excel problem when I am giving IPAddress in URL, but it is working correct when am giving localhost and page name. eg : when I am writing My machine IP...
4
by: Tennotrumps | last post by:
I am trying to export a report to excel but the "excel" option is greyed out. I can export the query the report is based on, but not the report. However, if I open the database on another PC I...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.