• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read and write data with excel?

 
calvin yao
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Must use jdbc dbc?
Thanks a lot!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
http://jakarta.apache.org/poi/
[ August 13, 2002: Message edited by: Oliver Refle ]
 
Mahesh swami
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
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
A B
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( rs.next() )
{
System.out.print( rs.getString( "A" ) + "\t");
System.out.print( rs.getString( "B" ) + "\t");
System.out.print( rs.getString }
rs.close();
} }
} // end of main
} // end of ExcelReaderWriter class
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic