Contact Us
  
 
 
    SearchSearch   RegisterRegister   Log inLog in 

Printing in jsp through excel

Printing in jsp through excel

 
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans
View previous topic :: View next topic  
Author Message
jeetu



Joined: 21 Jun 2007
Posts: 2

PostPosted: Thu Jun 21, 2007 5:36 am    Post subject: Printing in jsp through excel Reply with quote




Hi,
Is there is way to take print out in excel from jsp or servlet page. I am make a software where i have to make reports for print out and view purpose. So, i want to save my effort to not make html css type print out. It is not full solution as my experience. It needs lots of printing setting on client side. So i want to do in excel that is easy and reliable printing and view option. Please Help me if any one know how to take or change jsp to ms-excel content. thanks in advance
Back to top
View user's profile Send private message
hotjava



Joined: 25 Mar 2007
Posts: 14

PostPosted: Tue Nov 25, 2008 4:39 pm    Post subject: Reply with quote

Change Content type to excel and get output in excel file instead of html/text file

Code:
<%@ page language="java" import="java.io.*" %>
<%
  response.setContentType("application/vnd.ms-excel");
  response.setHeader("Content-Disposition","inline;filename=" + "excel_sheet"+".xls");
  response.setHeader("Cache-Control","no-cache");
 
  PrintWriter pout = response.getWriter();


  pout.print("\nA");
  pout.print("\tB");
  pout.print("\tC");
  pout.print("\tD");
  pout.print("\tE");
  pout.print("\tF");

  pout.close();
 
%>


This is simple example of excel in jsp to print
and easily save at client machine and take print out


Last edited by hotjava on Tue Nov 25, 2008 4:58 pm; edited 1 time in total
Back to top
View user's profile Send private message
jeetu



Joined: 21 Jun 2007
Posts: 2

PostPosted: Tue Nov 25, 2008 4:42 pm    Post subject: Reply with quote

According this example of excel which is in jsp, i have do more work.
I have to make same file and execute multiple times and it increase load on server.

I want a technique by i can generate excel through html page and get content inside of jsp page and convert into excel file.
Back to top
View user's profile Send private message
hotjava



Joined: 25 Mar 2007
Posts: 14

PostPosted: Tue Nov 25, 2008 4:52 pm    Post subject: Reply with quote

You have to work more if you want to make excel through jsp. I have to work double time. Either before executing jsp page or after executing jsp page.

If you are using database and fetching database, you can save ResultSet in session and reuse again in next excel jsp file and clear session in excel file

main.jsp

Code:
String Sql ="select * from table";
ResultSet rs=Sql;
session.setAttribute("rs",rs);
while(rs.next())
{
 
}



and excel.jsp file

Code:
ResultSet rs=(ResultSet)session.getAttribute("rs");
while(rs.next())
{

}
session.removeAttribute("rs");

something

this can save double query to database server
and create your excel
Back to top
View user's profile Send private message
stonebreak



Joined: 22 Jan 2007
Posts: 4

PostPosted: Thu Nov 27, 2008 5:47 am    Post subject: Reply with quote

If you want to export html content through JSP, you can store that content in cookies or in implicit object or vector
After that you can get object in next window
Back to top
View user's profile Send private message
hotjava



Joined: 25 Mar 2007
Posts: 14

PostPosted: Thu Nov 27, 2008 6:24 am    Post subject: Reply with quote

I am giving example of export excel in JSP

check this

Code:
<%@ page contentType="text/html; charset=iso-8859-1" language="java"%>
<%
String excelValue="tab1\ttab2\ttab3\ttab4";
session.setAttribute("excel",excelValue);
%>
<html>
<head>
<title>JSP File</title>
</head>

<body>
<div id="excelId"><%=excelValue%></div>
<a href="excel.jsp">Export to Excel</a>
</body>
</html>


next excel file for export content

Code:
<%@ page language="java" import="java.io.*" %>
<%
  response.setContentType("application/vnd.ms-excel");
  response.setHeader("Content-Disposition","inline;filename=" + "excel_sheet"+".xls");
  response.setHeader("Cache-Control","no-cache");
 
  String excel= (String)session.getAttribute("excel");
 
  PrintWriter pout = response.getWriter();

  pout.print("\n"+excel);
 
  pout.close();
  session.removeAttribute("excel");
%>
Back to top
View user's profile Send private message
sageeta



Joined: 11 Jun 2007
Posts: 2

PostPosted: Thu Dec 04, 2008 12:33 pm    Post subject: Reply with quote

Exporting excel through JSP can do with changing contentType in JSP file

simply see this example of excel and export by jsp

Excel should be installed on client where accessing this jsp file.

Code:
<%@ page contentType="application/vnd.ms-excel" %>
<html>
<head>
<title>Excel</title>
</head>
<body>
 \tA\tB\tC\tD
</body>
</html>


It will ask opening with windows
open or save

can open in excel
Back to top
View user's profile Send private message
hotjava



Joined: 25 Mar 2007
Posts: 14

PostPosted: Thu Dec 04, 2008 12:41 pm    Post subject: Reply with quote

In above code, request object is good option. I am modifying code little bit

check out this and one line can reduce from this code easily

Code:
<%@ page contentType="text/html; charset=iso-8859-1" language="java"%>
<%
String excelValue="tab1\ttab2\ttab3\ttab4";
request.setAttribute("excel",excelValue);
%>
<html>
<head>
<title>JSP File</title>
</head>

<body>
<div id="excelId"><%=excelValue%></div>
<a href="excel.jsp">Export to Excel</a>
</body>
</html>


excel.jsp
file

in excel jsp file no need of session remove

Code:
<%@ page language="java" import="java.io.*" %>
<%
  response.setContentType("application/vnd.ms-excel");
  response.setHeader("Content-Disposition","inline;filename=" + "excel_sheet"+".xls");
  response.setHeader("Cache-Control","no-cache");
 
  String excel= (String)request.getAttribute("excel");
 
  PrintWriter pout = response.getWriter();

  pout.print("\n"+excel);
 
  pout.close();
%>
Back to top
View user's profile Send private message
hotjava



Joined: 25 Mar 2007
Posts: 14

PostPosted: Thu Dec 04, 2008 12:45 pm    Post subject: Reply with quote

I listened about POI of apache project which can read excel and write in excel from java servlet. This API is good for microsoft office product.
I don't have code, but can read this website and example given in this API about excel also

http://poi.apache.org/
Back to top
View user's profile Send private message
hotjava



Joined: 25 Mar 2007
Posts: 14

PostPosted: Mon Dec 22, 2008 11:10 am    Post subject: Reply with quote

request object should have send
if you want to use request object instead of session
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans All times are GMT
Page 1 of 1

 
Jump to:  
  Quick Post a Reply Topic
     
  Name (required)  
     
  Email Address (will not be published) (required)  
     
  Subject of topic (required)  
     
   
     
   
     
  Advertisement  
     
 
     


  More Links  
 
JSP Introduction
JSP Setup
Simple JSP Example
JSP Variable
Array Declaration in JSP
JSP Forms and User Input
Form Validation
JSP Cookies
JSP Session Object
Loop, content collection, iterator, conditional check in JSP
JSP Application Object
JSP Include
JSP Response Object
JSP Request Object
JSP Error Handling
JSP File Handling Object
JSP Action Tag
JSP Implicit Objects
JSP Declaration
JSP Directive
JDBC Introduction
JDBC Driver
JDBC Connection
JDBC Prepared/Statement
JDBC ResultSet
JDBC Close Connection object
JDBC Insert
JDBC Display Records
JDBC Update
JDBC Delete
JDBC Sorting
JDBC Query
JDBC Exception
JDBC Add Batch
JDBC Transaction/RollBack