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
View Answers
January 5, 2011 at 11:04 AM
Hi Friend,
Try the following code:
import java.io.*;
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 static void main(String[]args){
try{
String filename="c:/data.xls" ;
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
HSSFRow rowhead= sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("SNo");
rowhead.createCell((short) 1).setCellValue("Name");
rowhead.createCell((short) 2).setCellValue("Address");
rowhead.createCell((short) 3).setCellValue("Contact No");
rowhead.createCell((short) 4).setCellValue("E-mail");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from employee");
int i=1;
while(rs.next()){
HSSFRow row= sheet.createRow((short)i);
row.createCell((short) 0).setCellValue(Integer.toString(rs.getInt("id")));
row.createCell((short) 1).setCellValue(rs.getString("name"));
row.createCell((short) 2).setCellValue(rs.getString("address"));
row.createCell((short) 3).setCellValue(Integer.toString(rs.getInt("contactNo")));
row.createCell((short) 4).setCellValue(rs.getString("email"));
i++;
}
FileOutputStream fileOut = new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated!");
} catch ( Exception ex ) {
System.out.println(ex);
}
}
}
For the above code, you need apache poi library.
Thanks
July 9, 2013 at 7:15 PM
Thanks a lot for this helpful example. :D
July 9, 2013 at 7:15 PM
Thanks a lot for this helpful article
September 8, 2012 at 11:02 AM
September 8, 2012 at 11:04 AM
but the above code working but the data is not inserted in excel sheet.....
can you give me the reason for this?
March 31, 2013 at 3:25 PM
an error in the code,to make it work correctly replace '/' by '\\' in the filename string.
March 27, 2013 at 8:05 AM
Hi friends May i know which version of jar should be used .I am unable to create excel file..Pleasehelp me.(I am using jdk 1.5)
Thank You
January 24, 2014 at 2:59 PM
i changed the it '/'to '\' but data is not inserted to excel
please help me on this
Related Tutorials/Questions & Answers:
Advertisements
export data from database to excel sheet - JDBCexport 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 get data from Excel sheet - StrutsHow 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 Java swing: Export excel sheet data to JTableJava 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 export data to excel sheet - JSP-Servletexport 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
Export data to Excel Sheet in STRUTS 1.3Export 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
how to display data in excel sheet?how to display
data in
excel sheet? According to the user Id,some links will be generated
from the
database,and if we click a link,
data would be retrieved
from the
database and
data will be displayed in a new page.in this page
excel sheet reading and using that data - JSP-Servletexcel 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
Store data in HTML forms from Excel sheetStore
data in HTML forms
from Excel sheet I have a
excel file having multiple coloums, and having one Html page having forms corrospoding to
excel coloums.
I want to auto fill up the
excel data into html forms,
Any one please
Printing data into Excel sheet - Java Beginners a
Java Bean that will retrieve the
data from the
database and insert it into
excel...
database then showing it(
data) to in "
Excel Sheet" is it possible to do ? if yes... where there is need to bring the
data into "
Excel Sheet" with proper format.My
Retrieve Data into JTable and export it to Excel File
Java Retrieve
Data into JTable and
export it to
Excel File
In this section, you will learn
how to retrieve the
data from the
database... it
into Vector which is then added to table. To
export the table
data to
excel
file, we
update excel sheet using jsp:: update
excel sheet using jsp:: Hi Sir,...
I have a
excel sheet for employee details
"i retrieve particular datas
from given
excel sheet and display it into
another
excel sheet using jsp
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 sheets in a
excel file.
To create a
excel sheet we can use third party APIs
Insert data in Excel File from Database using JSP
Insert
data in
Excel File
from Database
using
JSP ... will retrieve the
data from database, create an
excel file and
data
insert... developed a application to
insert data in
excel file
from database in JSP. We
Create Bar Graph from reading excel sheet in JavaCreate Bar Graph
from reading
excel sheet in Java I'm New to
Java and I have to create a
java program where I have to read
data from multiple
excel... in the different
excel sheets (
data can change and based on that graph should also get