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



Home Answers Viewqa JDBC JDBC to EXcel
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Mahesh Kolhe
JDBC to EXcel
1 Answer(s)      3 years ago
Posted in : JDBC


How to insert data in excel sheet from java?


Advertisement
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){
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.



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, Follow..."); Connection conn = DriverManager.getConnection("jdbc:odbc:excel
Advertisements
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 = conn.createStatement
excel
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
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...  
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
JAVA EXCEL
JAVA EXCEL  How to read the contents of an excel, perform some calculation and wrote the calculated values to another excel using poi
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
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
Sql to excel
Sql to excel  How to convert a Sql Data into excel Doc with Using Netbeans
JAVA_EXCEL
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
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
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
XLS JDBC Example
driver therefore you should use JDBC - ODBC bridge driver to connect your excel... .style1 { background-color: #FFFFCC; } XLS JDBC XlS JDBC driver is used to access xls file from java application. It is read only JDBC driver
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
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
jdbc
jdbc  display the records using index in jdbc
JDBC
JDBC  why we use batch in jdbc
jdbc
jdbc  Hai , Give a steps for jdbc connectivity
JDBC
JDBC  How to add set of queries in a single query in JDBC
JDBC
JDBC  can u send me the code of jdbc how to join two tables that are in relation
Jdbc
Jdbc  A java program with jdbc connectivity with insert,delete,update options for name,regno,mark1,mark2,total
jdbc
jdbc  is it possible to use doget & dopost method with jdbc to call in a servlet programe
 

 

 

DMCA.com