473,699 Members | 2,566 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 18343
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
13133
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
2160
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
2851
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
2926
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
13226
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
7204
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
1561
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
5154
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
8615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9173
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...
0
9033
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8882
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
6533
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
5872
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
4375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.