473,840 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export jsp output to excel

2 New Member
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 = "applicatio n/vnd.ms-excel";
response.setCon tentType(mimeTy pe);
response.setHea der("content-disposition","a ttachment; filename=totalp urgeexcel.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 18391
Dököll
2,364 Recognized Expert Top Contributor
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
864
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 that contains a number of labeled cells and sheets. When someone clicks the "Export to Excel" button on my asp page, I want to export the required data from my sql database to the Excel spreadsheet (putting the data in the appropriate cells and...
6
13150
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 spreadsheet. I'm having trouble with my code at the point at which it hits ".ReadRecords" -- the module just runs and runs without generating anything. I've gotten this code to correctly save .rpt files without any data, but not with data, nor have I been...
1
2168
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 manual export the query to excel, but when I run the macro, access said "There are too many rows to output, based on limitation specified by the output Format". In the macro I am using the "Output To" "Query" to "Excel". I do not see where I...
8
2864
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 ID="Button5" ONCLICK="Button5_Click" NAME="Button5" TYPE="button" VALUE="Export to Excel"> Sub Button5_Click()
0
2938
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 certain columns When I export to excel, I do not have the colspan. As a result, the subtotals appear over the wrong columns in Excel.
13
13264
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" HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=ABC.xls") HttpContext.Current.Response.Write(strHTML) HttpContext.Current.Response.End() However when the user tries to save it the Default File Type is Web Page(*.htm; *.html)
1
7213
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 problem. The problem is that I want to add text above the data grid in the excel sheet. The text already exists on the web page in various panels (there is different text depending on the parameters of the datagrid
0
463
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: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common cause for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is...
0
1572
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 address and aspx pagename like 192.xxx.xxx.xx/exporttoexcell.aspx then popup page is not working. the popup page is being closed automatically , and export to excel option is not comming. for some reason when it opens within a scripted popup (js:...
4
5172
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 CAN export to excel. I've tried reinstalling Access, but it doesn't make any difference. I've also installed the compatability file (as the version of excel is 2003), but to no avail. What am I missing?
0
9863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10665
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10302
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7845
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7023
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4080
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3139
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.