JDBC to EXcel

JDBC to EXcel

How to insert data in excel sheet from java?

View Answers

January 4, 2012 at 12:00 PM

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){
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");

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;
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"));
FileOutputStream fileOut =  new FileOutputStream(filename);
System.out.println("Your excel file has been generated!");

} catch ( Exception ex ) {


For the above code, you need apache poi library.

Related Tutorials/Questions & Answers:
JDBC to EXcel
JDBC to EXcel  How to insert data in excel sheet from java
connect jdbc to an Excel spreadsheet
connect jdbc to an Excel spreadsheet  hello, How can I connect to an Excel spreadsheet file using jdbc?   Hello Friend,ADS_TO_REPLACE_1... Select Microsoft Excel Driver(*.xls) Select work book or excel file and Create
Java and excel - JDBC
to a Jtable from where i can export to excel, i will like to format the column headers in excel using code. plz i nd help becos it is my project, as a serious student i tried using the following code but it gives me a distorted records in excel
excel file using JDBC java.?
excel file using JDBC java.?  hey, i am a telecomm. engineer , and i am try to develop a tool which reads from a excel file and then appends the same... used JDBC ODBC to read from excel file. i am not able to append it using
how to connect xlsx(2007 excel) - JDBC
how to connect xlsx(2007 excel)  i am not able connect to office 2007 excel file from jdbc
exporting data to excel sheet - JDBC
exporting data to excel sheet  Sir i have already send request about... query in java swing program,i want to print the result about the query in excel... = wb.createSheet("Excel Sheet"); try{ Connection con = null
java + excel data +graph - JDBC
java + excel data +graph  i am doin' a project in which i need to take up data from excel sheets and work upon them mathematically and finally draw a graph with the facility of drawing a trendline(in the graph)...also i need
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
Access Excel file through JDBC
Access Excel file through JDBC In this section, you will learn how to access excel file through Jdbc and display records in JTable. As you know Excel comes... = DriverManager.getConnection("jdbc:odbc:excel","", ""); Statement st
excel  how to save excel sheet data into the database using poi api... = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root...(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test
How to Retrieve Excel data into mysql using type 2 JDBC-ODBC driver
How to Retrieve Excel data into mysql using type 2 JDBC-ODBC driver  Can any please help it is very urgent. I tried to read excel sheet using ODBC..."); Connection conn = DriverManager.getConnection("jdbc:odbc:excel
excel  how to read columns values of excel file in java and store in array list in java like col1 col2 2 3 1 7 7 9 then list1 have values of col1 and list2 have values of col2...  
Excel to database
Excel to database  Hi I want to update SQL database with the values from excel using java code. Can you help me
JAVA EXCEL  How to read the contents of an excel, perform some calculation and wrote the calculated values to another excel using poi
XLS JDBC Example
.style1 { background-color: #FFFFCC; } XLS JDBC XlS JDBC driver is used to access xls file from java application. It is read only JDBC driver...- At First make an Excel Sheet of name 'student' as given below-ADS
Query On JDBC - JDBC
Query On JDBC  Hello , I am trying to migrate from xls TO mysql in Linux platform.. I'm using JDBC ODBC Bridge Driver..String url = "jdbc:excel:./personal1.xls";System.out.println
download excel
download excel  hi i create an excel file but i don't i know how to give download link to that excel file please give me any code or steps to give download link
JAVA_EXCEL  I need to read the all the contents of excel which has... content another excel. For example if I have three tabs in an excel with some contents... to another excel. Please help me out with this problem
Sql to excel
Sql to excel  How to convert a Sql Data into excel Doc with Using Netbeans
JDBC  why we use batch in jdbc
jdbc  display the records using index in jdbc
jdbc  Hai , Give a steps for jdbc connectivity
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... = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia","root", "root
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
JDBC  How to add set of queries in a single query in JDBC
jdbc  please tell me sir.i dont know JDBC connection and how to create table in database
jdbc   how to write program to save data and retrieve data from the form in Java
jdbc  why do we need to load jdbc drivers before connecting to database
JDBC  how to set classpath for eclipse, java, where to copy jdbc connector
JDBC  in class.forname which driver name we are writing for the connection from jdbc to sqlserver 2008
jdbc  is it possible to use doget & dopost method with jdbc to call in a servlet programe
Jdbc  A java program with jdbc connectivity with insert,delete,update options for name,regno,mark1,mark2,total
JDBC  can u send me the code of jdbc how to join two tables that are in relation
jdbc define batch updates  define batch updates?exp   JDBC... links: http://www.roseindia.net/jdbc/Jdbc-batch-update.shtml http://www.roseindia.net/tutorial/java/jdbc/batchupdateresultsetexample.html
jdbc - JDBC
Why JDBC   JDBC used for what
JDBC - limitations of jdbc  What are the limitations of JDBC
jdbc  Write a web based student registration application where the students can register online with their enrollment no. You are required to use JSP, Servelet and JDBC
To Generate Excel
With EXCEL in it has to open up how can i achieve this pls explain
convert excel into pdf
convert excel into pdf  How to convert excel into pdf files
uploading and reading the excel daa
uploading and reading the excel daa  uploading the excel file not getting the data of the excel
JDBC save a data in the database  I need a code to save a data... between java and mysql using JDBC and saves the data into the database. import... con = null; String url = "jdbc:mysql://localhost:3306/"; String db
JDBC code to save a data in the database  I need a code to save... the connection between java and mysql using JDBC and saves the data into the database...!"); Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db
how can we set transaction level through jdbc api  how can we set transaction level through jdbc api   Use Connection.setTransactionIsolation(int) to set the desired tansaction level, which takes one of the 5 arguments
how can we call stored procedure using jdbc  how can we call stored procedure using jdbc   Call a Stored Procedure using...(); For more information, visit the following link: JDBC call Stored Procedure
jdbc  hey sir i just wanna have some ppt on jdbc coz have my exams next week and i have not attended any classes coz of job... I m studyng frm niit
jdbc  I can't run my jdbc program because it has error in this line: public static void main(String[] args) the error is:illegal static declaration in inner class would you please let me whats the problem? regards
using jdbc connection   import java.sql.*; public class CreateTable... = DriverManager.getConnection("jdbc:odbc:student"); Class.forName
jdbc  i had written jdbc connection in method and i need to get connection in another class?   import java.sql.*; class... driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test
(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost... information, visit the following link: JDBC Tutorials
how can we set transaction level through jdbc api  how can we set transaction level through jdbc api   Use Connection.setTransactionIsolation(int) to set the desired tansaction level, which takes one of the 5 arguments