10 Replies Latest reply: Jan 15, 2007 4:51 AM by 807607 RSS

    How to read data from the excel file using java code.

    807607
      Hi to all,
      I am using below code to getting the data from the excel file but I can't get the corresponding data from the specific file. can anyone give me the correct code to do that... I will waiting for your usefull reply......

      advance thanks....

      import java.io.*;
      import java.sql.*;
      
      
          public class sample{
      
      
                   public static void main(String[] args){
                        Connection connection = null;
      
      
                            try{
                                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                 Connection con = DriverManager.getConnection( "jdbc:odbc:Mydsn","","" );
                                 Statement st = con.createStatement();
                                 ResultSet rs = st.executeQuery( "Select * from [Sheet1$]" );
                                 System.out.println("sample:"+rs);
                                    ResultSetMetaData rsmd = rs.getMetaData();
                                    System.out.println("samplersd:"+rsmd);
                                 int numberOfColumns = rsmd.getColumnCount();
      
                                    System.out.println("numberOfColumns:"+numberOfColumns);
      
      
                                     while (rs.next()) {
      
                                              System.out.println("sample1:"+rs);
                                              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("");
                                         }
      
                                         st.close();
                                         con.close();
      
      
                                        } catch(Exception ex) {
                                             System.err.print("Exception: ");
                                             System.err.println(ex.getMessage());
                                        }
                                   }
                          }