473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 18278
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.