Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to fetch integer data from excel

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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( "jdbcdbcriver={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
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to fetch integer data from excel

    Could this be because the xls file has that cell formatted as a decimal number?

    // Json

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to fetch integer data from excel

    then how to change the cell format in excel file.....

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to fetch integer data from excel

    Is either make sure that the value you are reading is infact going to be an integer or if you want to be sneaky you can use a NumberFormat to parse the String.

    NumberFormat.getIntegerInstance().parse("2001.0")

    // Json

  5. #5
    Junior Member
    Join Date
    May 2010
    Location
    India
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to fetch integer data from excel

    I think it is better to use the JXL api for excel rather than using jdbc.
    A link to the API tutorial - Java Excel API Tutorial

  6. #6
    Junior Member
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to fetch integer data from excel

    Hi use POI API. That is very good API for excel sheet Readign/Writing. I have used it so many time in the past.

Similar Threads

  1. How to Connect to an Excel Spreadsheet using JDBC in Java
    By JavaPF in forum JDBC and Database Tutorials
    Replies: 14
    Last Post: August 27th, 2013, 11:57 AM
  2. Importing excel data
    By supriya ramjee in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: October 20th, 2012, 09:39 AM
  3. check if a number is an integer
    By rsala004 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 15th, 2009, 03:51 PM
  4. [SOLVED] How to make a integer negative if it meets a certain criteria?
    By Lizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 14th, 2009, 02:27 PM
  5. Export to excel
    By ebosysindia in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: May 14th, 2009, 06:25 AM