Results 1 to 6 of 6
  1. #1
    nehakuls is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Post How to fetch integer data from excel

    Hi All,

    I have used following query to fetch data from excel file:
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection( "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" + "C:\\file1.xls" +";"+ "DriverID=22;READONLY=false" );
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery( "Select * from [Sheet1$]" );
    ResultSetMetaData rsmd = rs.getMetaData();
    int numberOfColumns = rsmd.getColumnCount();
    while (rs.next()) {
    for (int i = 1; i <= numberOfColumns; i++) {
    if (i > 1) System.out.print(", ");
    String columnValue = rs.getString(i);
    System.out.print(columnValue);
    }
    System.out.println("");
    }
    Its retrieving data from excel. But the problem is it returns decimal data even if u hv integer data in excel file.
    For example I have Year as 2001 in my excel file and it returns 2001.0.

    I tried to type cast it into integer but at run I can not get whether column has ineteger data or deciamal or alphanumeric data. the column id for year may change.

    plz help me.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Use POI or jxl. Otherwise Excel has essentially 3 formats, text, date, and number (which is decimal). If you know what is "suppossed" to be in the column, and you insist on using the Bridge, then call the proper method, i.e. getInteger rather than getString.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Yep.
    Excel is not a database.
    Don't treat it like one...

  4. #4
    nehakuls is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    I am using jxl itself...but as we have to apply some filter criteria at run time....its quite a long process...if we have a choice using sql queries then we can do it very easily.....thats y i asked for this.....

  5. #5
    raqman is offline Member
    Join Date
    Sep 2009
    Posts
    37
    Rep Power
    0

    Default

    In my opinion, you have to change the format of cell.
    RAQ Report: free Java reporting tool.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by nehakuls View Post
    I am using jxl itself...but as we have to apply some filter criteria at run time....its quite a long process
    Not when designed right, but that's another question/problem.

Similar Threads

  1. Upload excel data to access database
    By ravikumar.achi in forum New To Java
    Replies: 20
    Last Post: 01-19-2012, 11:30 AM
  2. How to fetch integer data from excel
    By nehakuls in forum NetBeans
    Replies: 2
    Last Post: 11-18-2009, 05:47 AM
  3. how to fetch 20,000 records from excel sheet & insert into access database using Java
    By santosh_tamse in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-22-2009, 10:24 AM
  4. Recovering data from Excel spreadsheet
    By Anubis in forum SWT / JFace
    Replies: 0
    Last Post: 12-17-2008, 08:28 PM
  5. How to read data from excel and store it in db
    By saran123 in forum New To Java
    Replies: 5
    Last Post: 10-03-2008, 10:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •