Reading from Excel sheet

I am trying to read a single column of data from an Excel sheet.

My Excel looks like this:

appnum

100

200

abc201

300

def301

400

500

My Excel sheet is called "appnumbers.xls" and the sheet is also called "appnumbers".

I have created the DSN and called it "appnumbers".

This is my code:

import java.sql.Connection;

import java.sql.Statement;

import java.sql.ResultSet;

import java.sql.DriverManager;

publicclass readExcel

{

publicstaticvoid main( String [] args )

{

Connection c =null;

Statement stmnt =null;

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver" );

c = DriverManager.getConnection("jdbc:odbc:appnumbers","","" );

stmnt = c.createStatement();

String query ="select appnum from [appnumbers$]";

ResultSet rs = stmnt.executeQuery( query );

System.out.println("Found the following app numbers:" );

while( rs.next() )

{

System.out.println( rs.getString("appnum" ) );

}

}

catch( Exception e )

{

System.err.println( e );

}

finally

{

try

{

stmnt.close();

c.close();

}

catch( Exception e )

{

System.err.println( e );

}

}

}

}

The output is as follows:

100.0

200.0

null

300.0

null

400.0

500.0

1. Why do the numbers appear like integers (100 shows up as 100.0)?

2. Why do the aplhanumeric characters appear as null?

[2765 byte] By [hardworkpaysa] at [2007-11-27 9:08:55]
# 1

Yeah, the excel odbc driver tries to gues the type of each column, based on the type of the first "data row" in each column. It's smart enough to ignore column headers, but it's not smart enough to read the whole column of values before deciding the type, so it's treating your alphanumeric values as an integer.

try sticking "abc123" in as the first data row and see what happens.

I believe, though I've never actaully done it, that you can programatically specify the datatype for each column, overriding the default silly guess work.

if that doesn't work try exporting the spreadsheet as XML and parsing it, instead of relying on the dodgy odbc driver at all.

Keith.

Message was edited by: corlettk

corlettka at 2007-7-12 21:48:01 > top of Java-index,Java Essentials,Java Programming...
# 2
Hi, if you wanna get some data from excel i highly recommend use some library like POI http://poi.apache.org/, is easier treat the sheet like a java object, and you manage the rows, columns and so on directly from the file, i hope this works for you.
jorvana at 2007-7-12 21:48:01 > top of Java-index,Java Essentials,Java Programming...
# 3
jorvan,Ta... I've never heard of POI ... looks great... thanx for the tip.keith.
corlettka at 2007-7-12 21:48:01 > top of Java-index,Java Essentials,Java Programming...
# 4
Np, in fact i've used another library in past projects if POI doesnt work as you want let me know and i can give you the other name, just let me find the name and the url, and i will be able to teach with more detailjorvan.
jorvana at 2007-7-12 21:48:01 > top of Java-index,Java Essentials,Java Programming...
# 5
Keith:I tried putting aplhanumeric as first data row and I still get null wherever alphanumeric appears and numbers come out fine! Jorvan:I am going thro the POI documentation now.. Thanks for the tip. Will post my results.Thank you both!
hardworkpaysa at 2007-7-12 21:48:01 > top of Java-index,Java Essentials,Java Programming...
# 6

I used POI and didn't like it (couldn't tell what type a data cell is easily: it seems to return a date cell as numeric. Also, data cell types and sub-types come in way too many types to handle in code). I didn't try JExcel but it might be worth looking into.Here is a like to someone who used both:

http://forum.java.sun.com/thread.jspa?threadID=513505

George123a at 2007-7-12 21:48:01 > top of Java-index,Java Essentials,Java Programming...
# 7
I tried jExcelAPI (reading from spreadsheets) and it worked in the first try. Its awesome! http://www.andykhan.com/jexcelapi/tutorial.html#reading
hardworkpaysa at 2007-7-12 21:48:01 > top of Java-index,Java Essentials,Java Programming...
# 8
You can try jxcell to read,write excel file. http://www.jxcell.net
liqd.oka at 2007-7-12 21:48:01 > top of Java-index,Java Essentials,Java Programming...