Home Answers Viewqa JSP-Servlet How to export web page to excel using java or jsp or servlets

 
 


lissy
How to export web page to excel using java or jsp or servlets
3 Answer(s)      2 years ago
Posted in : JSP-Servlet

Hi

I am trying to export web page(jsp page ) to excel using jsp or servlets. I am retrieving records from database and displaying in the jsp page, In this page I have a save as excel button, when I click this button I need these displayed records to excel file. I tried with vbscript and javascript but am getting some errors. Please can anyone tell me how to do this using java or jsp or servlets??

Thanks in advance, Lissy.

View Answers

May 23, 2011 at 10:55 AM


1)retrieve.jsp:

<%@page import="java.sql.*"%>
<form method="post" action="excelFile.jsp">
<table border=1>
<tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();  
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from employee");
while(rs.next()){
    %>
<tr><td><input type="text" name="name" value="<%=rs.getString("name")%>"></td><td><input type="text" name="address" value="<%=rs.getString("address")%>"></td><td><input type="text" name="contact" value="<%=rs.getString("contactNo")%>"></td><td><input type="text" name="email" value="<%=rs.getString("contactNo")%>"></td></tr>
<%
}
%>
</table>
<input type="submit" value="Export To Excel">
</form>

2)excelFile.jsp:

<%@page import="  java.io.*"%>  
<%@page import="  org.apache.poi.hssf.usermodel.*"%>  
<%
String name[]=request.getParameterValues("name");
String address[]=request.getParameterValues("address");
String contact[]=request.getParameterValues("contact");
String email[]=request.getParameterValues("email");

try{
String filename="c:/data.xls" ;
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet =  hwb.createSheet("sheet");

HSSFRow rowhead=   sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("Name");
rowhead.createCell((short) 1).setCellValue("Address");
rowhead.createCell((short) 2).setCellValue("Contact No");
rowhead.createCell((short) 3).setCellValue("E-mail");
for(int i=0;i<name.length;i++){
    int j=i+1;
HSSFRow row=   sheet.createRow((short)j);
row.createCell((short) 0).setCellValue(name[i]);
row.createCell((short) 1).setCellValue(address[i]);
row.createCell((short) 2).setCellValue(contact[i]);
row.createCell((short) 3).setCellValue(email[i]);
}
FileOutputStream fileOut =  new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
out.println("Your excel file has been generated!");
} catch( Exception ex ) {
    System.out.println(ex);
}
%>

May 23, 2011 at 10:56 AM


You need poi api for the above code.


May 25, 2011 at 2:40 PM


Masterlist_fetch.jsp
[code]
<%@ page import="java.sql.*" %>
<% Class.forName("oracle.jdbc.driver.OracleDriver");%>
<HTML>

    <BODY bgcolor="#99CCFF">



    <br><br>

<h2 align="center">Records for the selected Part No</h2>
        <form name="fetchform" action="/UserInvenApplication/excelFile" method="post">
        <%
                    String connectionURL = "jdbc:oracle:thin:@localhost:1521:xe";
                    String driver = "oracle.jdbc.driver.OracleDriver";
                    String user = "root";
                    String pass = "root";
                    Connection connection = null;
                    PreparedStatement pst;
                    try {
                        Class.forName(driver);
                        connection = DriverManager.getConnection(connectionURL, user, pass);
                        String PartNo = request.getParameter("PartNo");
                        int ibl = PartNo.length();
                       String Last_char = PartNo.substring(ibl-1,ibl);
                       String X;
                       if (Last_char.equals("*"))
                       {

                           X = PartNo.substring(0,(ibl-1)) + '%';
                           pst = connection.prepareStatement("select SerialNo,PartNo,Material_Number,Material_Desc from InventoryDB_Main where PartNo like '"+X+"'");

                        }   else {
                               X = PartNo;
                               pst = connection.prepareStatement("select SerialNo,PartNo,Material_Number,Material_Desc from InventoryDB_Main where PartNo = '"+X+"'");
                             }                                      

                        //PreparedStatement pst = connection.prepareStatement("select SerialNo,PartNo,Material_Number,Material_Desc from InventoryDB_Main where PartNo = '"+ PartNo +"'");
                        ResultSet rs = pst.executeQuery();
                       // while(rs.next()){
                       if (!rs.next()) {
                            out.println("<br>");
                            out.println("<table align=\"center\" font=\"16\">");
                            out.println("<tr><th>Sorry, Could not find data</th></tr>");
                            out.println("</table>");

                        } else { 

        %>

        <TABLE cellpadding="15" border="1" style="background-color:#6699CC" align="center">
            <TR>
                <TH>Serial No</TH>
                 <TH>Part No</TH>
                <TH>Material Number</TH>
                <TH>Material Description</TH>


                <TH>Update</TH>

            </TR>
            <%
                             do {
            %>
           <style type="text/css">
        a:link {color:#FF0000;}    /* unvisited link */
        a:visited {color:#FF0000;} /* visited link */
        a:hover {color:#FF00FF;}   /* mouse over link */
        a:active {color:#0000FF;}  /* selected link */
    </style>
            <TR style="background-color:white">
                <TD> <%= rs.getString(1)%> </TD>
                <TD> <%= rs.getString(2)%> </TD>
                <TD> <%= rs.getString(3)%> </TD>
                <TD> <%= rs.getString(4)%> </TD>
                <TD><a href="masterServlet?SerialNo=<%= rs.getString(1)%>">Update</a></TD>

            </TR>
            <%                        rs.next();
                             } while (rs.isAfterLast() != true);

            %>
        </TABLE>
        <BR>
        <%
                        }
                        rs.close();
                        pst.close();
                        connection.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

        %>
        <center>
            <input type="submit" value="Save as Excel"/>
        </center>
        </form>
        </BODY>
</HTML>
[/code]

I have created Servlet named ExcelFile.java

ExcelFile.java

[code]
package saveasexcel;

import  java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import  org.apache.poi.hssf.usermodel.*;

public class ExcelFile extends HttpServlet{

   public void doPost(HttpServletRequest request, HttpServletResponse response)
                                   throws ServletException,IOException{
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();

String SerialNo[]=request.getParameterValues("SerialNo");
String PartNo[]=request.getParameterValues("PartNo");
String Material_Number[]=request.getParameterValues("Material_Number");
String Material_Desc[]=request.getParameterValues("Material_Desc");

try{
String filename="c:/data.xls" ;
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet =  hwb.createSheet("sheet");

HSSFRow rowhead=   sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("SerialNo");
rowhead.createCell((short) 1).setCellValue("PartNo");
rowhead.createCell((short) 2).setCellValue("Material_Number");
rowhead.createCell((short) 3).setCellValue("Material_Desc");
for(int i=0;i<SerialNo.length;i++){
    int j=i+1;
HSSFRow row=   sheet.createRow((short)j);
row.createCell((short) 0).setCellValue(SerialNo[i]);
row.createCell((short) 1).setCellValue(PartNo[i]);
row.createCell((short) 2).setCellValue(Material_Number[i]);
row.createCell((short) 3).setCellValue(Material_Desc[i]);
}
FileOutputStream fileOut =  new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
out.println("Your excel file has been generated!");
} catch( Exception ex ) {
    System.out.println(ex);
}
   }
}
[/code]

This servlet is not generating any excel file, Please help me how to export the web page to excel, once I display the records in jsp page, when i click save as excel button, I want a new excel file open with all the web content in it. Please help me out. I am stuck here.

Thanks in advance
Lissy.









Related Pages:
How to export web page to excel using java or jsp or servlets
How to export web page to excel using java or jsp or servlets  Hi I am trying to export web page(jsp page ) to excel using jsp or servlets. I am... errors. Please can anyone tell me how to do this using java or jsp or servlets
How to export data from jsp to excel sheet by using java
How to export data from jsp to excel sheet by using java   How to export data from jsp to excel sheet by using java
How to download web page table data, export the table records in an excel file and save
How to download web page table data, export the table records in an excel file and save  i have a web page(.jsp) which contains the table of 4 to 5 columns. i m displaying the table using in my jsp page. below this table i want
How to export the table content from an webpage to excel using java?
How to export the table content from an webpage to excel using java?  How to export the table content from an webpage to excel using java? The table contents are generated dynamically in that java page
How to export the table content from an webpage to excel using java?
How to export the table content from an webpage to excel using java?  How to export the table content from an webpage to excel using java? The table contents are generated dynamically in that java page
export
export  how to export tabled data in jsp into excel
export java to excel - Java Beginners
export java to excel  How do you export java to excel?  You mean to say accessing Microsoft files in java? Then you can go for Apache POI. Its an API to access MS-Office files.Try using it. - Ramesh A.V
How to export data from html to excel sheet by using java
How to export data from html to excel sheet by using java   How to export data from html to excel sheet by using java
How to export data from html file to excel sheet by using java
How to export data from html file to excel sheet by using java    How to export data from html file to excel sheet by using java
How to export data from html file to excel sheet by using java
How to export data from html file to excel sheet by using java   reading the data from Html file
export to excel - JSP-Servlet
export to excel  Hi this is priti. I need a help. What is my question is i have a report page here i need a button cllaed "Export to excel". After clicking the button all the values from the jsp page will save to a excel file
servlets
application, and they can be applied to any resources like HTML, graphics, a JSP page...what are filters in java servlets  what are filters in java  ... functionality to the servlets apart from processing request and response paradigm
Export data in excel sheet in java in struts - Struts
Export data in excel sheet in java in struts  Hello, All how can i export data in excel file i java  Hi friend, For solving the problem visit to : http://www.roseindia.net/jsp/excelfile.shtml Thanks
Export data in excel sheet via Browse and upload button into mysql database
Export data in excel sheet via Browse and upload button into mysql database  how to create a Browse & Upload Buttons and then save... the Java web-application
How to Create Excel Page Using JSP
how to create excel page using jsp   ... using pure word files using Java. We can create, read or write MS Excel file using... using java .By going through the steps of  this example we can create any
How to export data from database to excel sheet by using java in standalone project
How to export data from database to excel sheet by using java in standalone project  How to export data from database to excel sheet by using java in standalone project
how do i provide down a pdf document fecility on my web page using jsp and servlets?
how do i provide down a pdf document fecility on my web page using jsp... on my web page,the pdf file contains retrieved data from mysql table. I need this program by using jsp-servlets. any one can help me please?? Thanks&
servlets
authentication In FORM-based the web container invokes a login page. The invoked... in servlets   There are four ways of authentication:- HTTP basic... authenticationLet?s try to understand how the above four ways work. HTTP basic
how to update values of a html form into an excel table using java servlets?
how to update values of a html form into an excel table using java servlets?  i have written a java servlet program, which has a html form... be loaded into an excel table automatically. i have created a dsn for excel
Register page using servlets,jsp and java beans
Register page using servlets,jsp and java beans  i want code for register page using jsp,serlets and java beans.iam getting error for my code in java...://www.roseindia.net/jsp/user-registration-form-using-jsp.shtml Thanks
How to export grid into excel
How to export grid into excel  Hi, i created a grid panel i have to export it to the excel. please help me by some sample code. thanks in advance. cool day dude
export data to excel sheet - JSP-Servlet
export data to excel sheet  Hi.. how to export data to excel sheet from jsp? and how to update the excel sheet from jsp? and how to get the data from excel sheet? and how to make calculations in excel sheet like total avg
Java swing: Export excel sheet data to JTable
Java swing: Export excel sheet data to JTable In this tutorial, you will learn how to read data from excel file and export it to JTable. In swing.... Now to read these excel files, Java has provide two api's JExcel api
servlets
servlets  how can I run java servlet thread safety program using...'. 2)Go to the webapps folder of your apache tomcat and create a web... web.xml and classes folder inside the WEB_INF folder of web application folder. 4
how to present the excel to the web browser in jsp
how to present the excel to the web browser in jsp  How to present the content of the newly created excel file in the following jsp to the web...="org.apache.poi.xssf.usermodel.XSSFWorkbook"%> <%@ page contentType="application/vnd.ms-excel
How to export data from database to excel sheet by using java swing in standalone project - Java Beginners
How to export data from database to excel sheet by using java swing... from u to solve my problem. my problem is i don know how to export data from database to excel sheet by using java swing in standalone project.I get solution from
JSP TO EXCEL
JSP TO EXCEL  Hi sir/mam, How to import data to excel using jsp without retrieving database.   friend, you can't import excel data into the middle of an HTML pages (your JSP will result in an HTML page
Export data to Excel Sheet in STRUTS 1.3
Export data to Excel Sheet in STRUTS 1.3  hi. how can i export content present on the jsp to excel sheet on button click..... and challenging part is i have to merge few rows and columns for a particular field(Value
Servlets errors in same page.
Servlets errors in same page.  How do I display errors list in the same page where a form field exists using servlets...........i.e. without using JSP? Please explain with a simple username password program
Function data from web in MS excel
data from one webpage to Excel using function Data - From Web. The problem is that this web page has at the end .jsp . When I open it via Excel, click to data... believe that the reason is format of .jsp because another web pages are working
how to create a reminder app using threads in Servlets?
how to create a reminder app using threads in Servlets?  I want... (threads will be required!), a "pop-up window or a web-page should automatically get re-directed!". I have used threads for core java, but never used for Servlets
Refresh a Web Page Using In Servlet
.style1 { font-weight: bold; } Refresh a Web Page Using... example we develop an application to Refresh a web Page using Servlet. We... on the browser as a output. Step 1: Create a web page(timer.html) to call a Servlets
Servlets Vs Jsp - JSP-Servlet
JSP is a Presentation Layer. A Java Server Page is a slightly more complicated...Servlets Vs Jsp  In servlets and Jsp's which one is important? and also tell me the Is Servlets are best or jsp's are best? give me the(reason also
Servlets Books
to program dynamic Web content using Java Servlets, with a fine introduction..., conference speaker on servlets and JSP (JavaOne, International Conference for Java... that any Java web developer who uses JavaServer Pages or servlets will use every day
How to Create New Excel Sheet Using JSP
How to create new excel sheet using jsp  ... a new  excel sheet using java .You can create any number of  new excel... the web server.  Create folder into C drive with the name of C:\excel
excel to database
excel to database   How to export data from excel sheet to mysql database by using java with request parameter Here q=40 is a request parameter ..request parameter using i export the all data to database   <
servlets - Java Beginners
, The Java Servlet is the fundamental component to using Java for web... to respond to HTTP requests. A JSP layered on top of Java Servlets. Whereas...servlets  what is the difference b/w servlets and JSP, what servlets
Excel - JSP-Servlet
Excel  How to export data from jsp to excel sheet. I am using struts1.2 in my application.  Hi friend, Code to data from Jsp to excel...: "success.jsp" For more information on excel sheet Using JSP visit
servlets
servlets  How do u display the list of employee object in JSP page
servlets
pages that are evaluated by the Web server when the Web page is being served. SSI are not supported by all web servers. So before using SSI read the web server... pages to add one or more files into a web page and come out with given directives
Use Java Bean In Servlets
Use Java Bean In Servlets In this you will learn how to use Java Bean in Servlets. For this purpose, we have created a Bean named 'Person' and defined three... available there. In jsp page, we have used EL to display the attribute values
How to Upload a file directly to Oracle database using JSP or Servlets?
How to Upload a file directly to Oracle database using JSP or Servlets?  Hi I want to upload a file(csv or excel) to Oracle 10g Database using JSP, here is my code which gives me an error.. Addfile.jsp <%@ page import
Java Count word occurrence and export it to excel file
Java Count word occurrence and export it to excel file Here is an example... and display the result the result into excel file in the table form. Example...;Your excel file has been generated!"); } catch(Exception e
Export Extjs Gridview data to excel in jsp
Export Extjs Gridview data to excel in jsp  i need to export the extjs girdview data to excel can you please help me thanks in advance
how to execute jsp and servlets with eclipse
how to execute jsp and servlets with eclipse  hi kindly tell me how to execute jsp or servlets with the help of eclipse with some small program... to create a java project in Eclipse and how can you add additional capabilities
How to print contents of a web page in jsp?
How to print contents of a web page in jsp?  I have generated a pay slip using jsp.How do I print the contents of the slip
export jsp page - JSP-Servlet
export jsp page  i want to export jsp page in word and pdf format... code that export my jsp page in word and pfd format....  Hi Friend, Try the following code: 1)pdf.jsp: Name Address Contact
export value in csv from jsp
export value in csv from jsp  Hi! i have creted a html form using javascript. i hv taken the array values in jsp. nw i want to pass these array values from jsp to csv file. so please help me how to send
Export data into CSV File using Servlet
Export data into CSV File using Servlet  ... to Export data into  CSV file using Servlet. We have created  file "JdbcCsvFile.java" to export data from this .java file.. Brief
Export chart to JPEG file
Export chart to JPEG file  Hi. I have one problem. I have generated piecharts and barcharts using chartlib.js and jquery in jsp. Now i want to export this charts to jpeg file . how can i do this? Thank q in advance

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.