File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and the fly likes How to read and write data with excel? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to read and write data with excel?" Watch "How to read and write data with excel?" New topic

How to read and write data with excel?

calvin yao

Joined: Jul 05, 2002
Posts: 10
Must use jdbc dbc?
Thanks a lot!
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
JDBC-ODBC Bridges or jdbc itself is normaly used to access for databases.
When you want to access an excel file you should use
POI which is a jakarta framework to read/write excel
files. You can find it under
[ August 13, 2002: Message edited by: Oliver Refle ]
Mahesh swami

Joined: Aug 13, 2002
Posts: 26
Just go through this code
Create an excel file called budget
add a sheet to it and name the sheet as main
write the following data in excel
1 45
1 67
Now compile and run the program

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
public class ExcelReaderWriter {
public static void main( String [] args ) {
Connection c = null;
Statement stmt = null;
try {
// Using the JDBC ODBC Bridge to connect
// to the Excel workbook.
// There needs to be a ODBC DSN created for
// the Excel workbook prior to running the code.
// This uses a DSN called budget.
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
c = DriverManager.getConnection( "jdbc dbc:budget", "", "" );
stmt = c.createStatement();
if (args.length == 0) {
// The worksheet name is the table name - "main" in
// this example. We need to use the dollar sign
// following the table name.
String query = "select * from [main$];";
ResultSet rs = stmt.executeQuery( query );
System.out.println( "Found the following billing information:" );
while( )
System.out.print( rs.getString( "A" ) + "\t");
System.out.print( rs.getString( "B" ) + "\t");
System.out.print( rs.getString }
} }
} // end of main
} // end of ExcelReaderWriter class
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The advantage of POI/HSSF is that you can create your excel file in a more object oriented way. So
you don't call any selects or somthing you have a structure which represent a excel file
e.g. workbook contains worksheets and this contains cell Objects.
So in my opinion this is a better way to handle what you would like to do.
I agree. Here's the link:
subject: How to read and write data with excel?