Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Printing data into Excel sheet
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


prashant
Printing data into Excel sheet
1 Answer(s)      5 years and 8 months ago
Posted in : Java Beginners



Advertisement
View Answers

April 9, 2009 at 12:33 PM


Hi prashant,

Follow these steps:

step1) Create a simple 'excelFile.jsp' page:

<html>
<form method="post" action="file.jsp">
<input type = "submit" value="Submit">
</form>
</html>

step2) Create 'file.jsp' page that calls the Java Bean:

<jsp:useBean id="sample" class="form.ExcelBean" >
<jsp:setProperty name="sample" property="*"/>
</jsp:useBean>
<%
sample.insertExcelData();
out.println("Your excel file has been generated");
%>

step3) Create a Java Bean that will retrieve the data from the database and insert it into excel file:

package form;

import java.io.*;
import java.util.* ;
import java.sql.* ;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;

public class CreateExcelFile{
public CreateExcelFile(){
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";;
String dbName = "register";
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String userPassword = "root";
String filename = "c:\\excel\\file.xls" ;
try{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,username,userPassword);
Statement stmt = conn.createStatement();
String strQuery = "select * from registertable ";
ResultSet rs = stmt.executeQuery(strQuery);

HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
HSSFRow rowhead = sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("User ID");
rowhead.createCell((short) 1).setCellValue("First Name");
rowhead.createCell((short) 2).setCellValue("Last Name");
rowhead.createCell((short) 3).setCellValue("Address");
rowhead.createCell((short) 4).setCellValue("E-mail");

int index=1;
int id=0;
String name="";
while(rs.next())
{
id++;

HSSFRow row = sheet.createRow((short)index);
row.createCell((short) 0).setCellValue(id);
row.createCell((short) 1).setCellValue(rs.getString(2));
row.createCell((short) 2).setCellValue(rs.getString(3));
row.createCell((short) 3).setCellValue(rs.getString(4));
row.createCell((short) 4).setCellValue(rs.getString(5));
index++;
}
FileOutputStream fileOut = new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated");
}
catch(Exception ex) {}
}
}
Thanks


Related Tutorials/Questions & Answers:
Printing data into Excel sheet - Java Beginners
Printing data into Excel sheet  Hi all i am writing one application where there is need to bring the data into "Excel Sheet" with proper format.My... database then showing it(data) to in "Excel Sheet" is it possible to do ? if yes
read data from Excel sheet
read data from Excel sheet  Hi, How to read data from an excel sheet using JSP. Thank you
Advertisements
how to display data in excel sheet?
how to display data in excel sheet?  According to the user Id,some... the button and to display the data in excel sheet... there will be a button called excel download,and if we click the button data
jsp with excel sheet data uploading into database
jsp with excel sheet data uploading into database  how to upload data in excel sheet with jsp into oracle 10g database
exporting data to excel sheet - JDBC
exporting data to excel sheet  Sir i have already send request about... = wb.createSheet("Excel Sheet"); try{ Connection con = null... query in java swing program,i want to print the result about the query in excel
extract data from excel sheet to mysql
extract data from excel sheet to mysql  sir, i want to extract data from excel sheet and save the data in mysql5.0 database in the form of table
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 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
How to get data from Excel sheet - Struts
How to get data from Excel sheet  Hi, I have an excel sheet with some data(including characters and numbers). Now i want read the data from excel sheet and display in console first then later insert this data into database
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
export data from database to excel sheet - JDBC
export data from database to excel sheet  I am facing a problem about exporting required data from database table to ms-excel sheet.i mean whenever I execute a query then result will be display in MS-EXCEL sheet. please give me
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 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
excel sheet reading and using that data - JSP-Servlet
excel sheet reading and using that data  i have to do a read a excel sheet file of a employee record and then i have to use a employee details to send mail to those employees how to do in jsp sir please help me sir.. Thanks
Convert the excel sheet data into oracle table through java or jsp
Convert the excel sheet data into oracle table through java or jsp  Hi Friends, Let me help this issue i am phasing Convert the excel sheet data into oracle table through java or jsp
How to Get The Data from Excel sheet into out jsp page???
How to Get The Data from Excel sheet into out jsp page???  How to Get The Data from excel sheet to out jsp page in webApp
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
Uploading Excel sheet record in JSP to insert data in MySql
Uploading Excel sheet record in JSP to insert data in MySql  Need Help how to upload Excel (.xls) file and insert data in Mysql using JSP it wil be wonder for me if any help me
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 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 information from it in mysql database's Table Am using struts2,hibernate for making
How to save data to excel with a 2.1 work sheet format without changing it?
How to save data to excel with a 2.1 work sheet format without changing... and save it to excel file then read it again .. the problem is , when data... as ... excel 2.1 work sheet at first then read it back .. Whats the solution
How to read every cell of an excel sheet using Apache POI and insert those data into a DB?
How to read every cell of an excel sheet using Apache POI and insert those data into a DB?   i have an excel sheet, whose data in each cell has... the rows of an excel sheet. Need the source code
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
Hi how to transfer table data from html page to excel sheet by using javascript .
Hi how to transfer table data from html page to excel sheet by using javascript .  html page to excel sheet by using javascript and i dont want to transfer all rows in table, i want to hide some rows in excel sheet. Please send
update excel sheet using jsp::
update excel sheet using jsp::   Hi Sir,... I have a excel... given excel sheet and display it into another excel sheet using jsp... page to delete the data in excel but it not working.. it shows null point
reading excel sheet in java
reading excel sheet in java  can anyone tell me a proper java code to read excel sheet using poi   Here is a java code that reads an excel file using POI api and display the data on the console. import java.io.
insert excel sheet into mysql as a table
insert excel sheet into mysql as a table  sir, i want to import an excel sheet into mysql5.0 database as in the table format using tomcat 6.0 by jsp
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... database to excel sheet by using java swing in standalone project.I get solution from... this api in order to create an excel sheet. We are providing you another code
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... and POI-HSSF api. Here we are using JExcel api to fetch the data from excel sheet
Excel sheet image reading issue
Excel sheet image reading issue   Hello every one.I?m trying to read images from an excel sheet using OleDbDataReader. My excel file has 6 columns of data, the first 5 are all text but the last is image. While I?m
browse and submit the excel sheet into mysql as a table
browse and submit the excel sheet into mysql as a table  sir, i want to browse and after clicking a submit button, the data in the excel sheet should be stored in the mysql5.0 as a table
 

 

 

DMCA.com