4 Replies Latest reply: Oct 17, 2006 8:00 AM by 807607 RSS

    Find no of sheets in any Excel File

    807607
      Hello Experts

      How to find no of sheets in any Excel File .
      I am reading Excel sheet .
      Please give solutions .

      Regards,
      Nitin Jain
        • 1. Re: Find no of sheets in any Excel File
          807607
          How are you reading the Excel file ?
          Vague questions will get vague answers.

          POI is an open source project to read Excel files.

          regards,
          Owen
          • 2. Re: Find no of sheets in any Excel File
            807607
            Hello Friends

            I am using this code
            But I am not getting solution .
            I do'nt want to use third party lib.
            I am reading excel sheet .by this code and i am giving name explicitly .
            But sometimes I do'nt know what is name of sheet in any excel file .

            So,

            How to know no of sheets in any Excel file . and what is the name of sheets .


            import java.io.*;
            import java.sql.*;


            public class ReadExcelLocation{


                 public static void main(String[] args){
                      Connection connection = null;
            String fileName = "c:/a.xls";
            DatabaseMetaData dm = null;
            try
            {       
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection( "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" + fileName );
            dm=con.getMetaData();

            try{
            ResultSet tableName =dm.getCatalogs();
            System.out.print("Hello");
            while (tableName.next())
            {               
                                System.out.println(tableName.getObject(1).toString());     
            System.out.print("Hello");
                 }
            }catch(Exception e)
            {
                 
            }

                      try{
                           
                           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("");     
                           }
                 
                           st.close();
                           con.close();
            }catch(Exception e)
            {
                 
            }


                      } catch(Exception ex) {
                           System.err.print("Exception: ");
                           System.err.println(ex.getMessage());
                      }
                 }
            }
            • 3. Re: Find no of sheets in any Excel File
              807607
              lass.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              Connection con =
              DriverManager.getConnection(
              "jdbc:odbc:Driver={Microsoft Excel Driver
              (*.xls)};DBQ=" + fileName );
              dm=con.getMetaData();
              Seriously, look at POI. It allows you to load up a spreadsheet and work with it in a natural, object-oriented way.

              What you're doing may not be wrong per-se, but it seems a little... odd.

              And yes I realise you didn't want to use a third party library but you don't need to reinvent the wheel on this.
              • 4. Re: Find no of sheets in any Excel File
                807607
                I'm trying to do the same,
                I'm doing querys to a excel file, but I wold like to get the list of the sheets.
                If anyone knows how to do that.

                thanks

                Carlos