Reading Excel files in Java --

Hi,
I am trying to read an excel file from Java. It only reads the column, but I want to print the rows, which intersect with the SQL query. The following source displays only values of a column. No problem, but I would like to see the corresponding row numbers or simply number of rows. Can anyone suggest me what to do ASAP?
Thanks,
Gunter
import java.io.*;
import java.sql.*;
import java.util.*;
public class ExcelReadTest1{
public static void main(String[] args){
Connection connection = null;
try{
//Vector vector = new Vector();
String myString="";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection( "jdbc dbc ata-list" );
Statement st = con.createStatement();
ResultSet rs = st.executeQuery( "Select caseid from [sa3$] where iSchool=2" );
//ResultSet rs = st.executeQuery( "Select * from [Sheet1$]" );
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
int row_num=1;
while ((rs !=null) && rs.next()) {
myString += "\n Row " + row_num++ + " ";
for (int i = 1; i <= numberOfColumns; i++)
//myString += "\n " + rsmd.getColumnName(i);
myString += " : " + rs.getString(i);
System.out.println(myString);
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());

I took your code and reworked it to access a little database I keep of students at a small private school where my wife works. The Student table has columns "studentID", "firstname", "lastname", and "gradyear".
Running your code for the output got the following. I cut and pasted the output after it had run through the first 4 students, so you can see what it's doing.
Row 1 : 1
1
Row 1 : 1 : Audrey
, Audrey
Row 1 : 1 : Audrey : Budington
, Budington
Row 1 : 1 : Audrey : Budington : 2015
, 2015
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2
2
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2 : Cecilia
, Cecilia
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2 : Cecilia : DeRigo
, DeRigo
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2 : Cecilia : DeRigo : 2015
, 2015
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2 : Cecilia : DeRigo : 2015
Row 3 : 3
3
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2 : Cecilia : DeRigo : 2015
Row 3 : 3 : Serena
, Serena
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2 : Cecilia : DeRigo : 2015
Row 3 : 3 : Serena : Downes
, Downes
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2 : Cecilia : DeRigo : 2015
Row 3 : 3 : Serena : Downes : 2015
, 2015
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2 : Cecilia : DeRigo : 2015
Row 3 : 3 : Serena : Downes : 2015
Row 4 : 4
4
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2 : Cecilia : DeRigo : 2015
Row 3 : 3 : Serena : Downes : 2015
Row 4 : 4 : Dylan
, Dylan
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2 : Cecilia : DeRigo : 2015
Row 3 : 3 : Serena : Downes : 2015
Row 4 : 4 : Dylan : Gellert
, Gellert
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2 : Cecilia : DeRigo : 2015
Row 3 : 3 : Serena : Downes : 2015
Row 4 : 4 : Dylan : Gellert : 2015
, 2015
You need to figure out the flow of the thing and your println() calls and your looping. Eventually if you straighten it out you will get something like
Row 1 : 1 : Audrey : Budington : 2015
Row 2 : 2 : Cecilia : DeRigo : 2015
Row 3 : 3 : Serena : Downes : 2015
Row 4 : 4 : Dylan : Gellert : 2015
Row 5 : 5 : Benjamin : Miller : 2015
Row 6 : 6 : Timothy : Bolan : 2014
Row 7 : 7 : Javin : deMello-Folsom : 2014
Row 8 : 8 : Elizabeth : Eppolito : 2014
Row 9 : 9 : Harrison : Evans : 2014
Row 10 : 10 : Emily : Fay : 2014
*** etc***
which is what you want, isn't it?

Similar Messages

  • How to read Excel file in java

    Respected sir/madam
    I want to read the values from Excel file in Java program. How can I do that. When I searched over the net I came to know that you can treat Excel file as a Database and write the code as u write for making DB connections .
    I did that but i am getting the following error ..can anybody please help..
    This is the code what i have written
    import java.io.*;
    import java.sql.*;
    public class ReadExcelFile {
    Connection c;
    Statement stmnt;
    public void checkABA_Number()
    try
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    c = DriverManager.getConnection("jdbc:odbc:ExcelTest","",""); // ExcelTest is the DSN name
    stmnt = c.createStatement();
    ResultSet rs = stmnt.executeQuery("select * from abadata"); // abadata is my Excel file name
    while(rs.next())
    System.out.println(rs.getString(1)+" "+rs.getString(2));
    }catch(Exception e)
    System.out.println(""+e.toString());
    finally
    try
    stmnt.close();
    c.close();
    catch( Exception e )
    System.err.println( e );
    public static void main(String[] args)
    new ReadExcelFile().checkABA_Number();
    My Excel file starts from the first row and first column and also the first row contains the names of the column.
    It give me the following error..
    java.sql.SQLException: [Microsoft][ODBC Excel Driver] The Microsoft Jet database engine could not find the object 'abadata'. Make sure the object exists and that you spell its name and the path name correctly.
    How can I deal with this.?I have properly selected the worksheet while giving the DSN . Is there any versionig problem with Excel or some drivers are in appropriate..and yes i chose Microsoft Excel Driver (.xsl) from ODBC .
    I created System DSN.
    Can anybody please help me with this ? I will be very gratefull for replies
    Thanks in advance

    here is the code to read excel file
    public void readexel(String filename)
    Connection c = null;
    Statement stmnt = null;
    try
    Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
    c = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" + filename);
    stmnt = c.createStatement();
    String query = "Select * from [Sheet1$]" ;
    ResultSet rs = stmnt.executeQuery( query );
    while( rs.next() )
    System.out.println( rs.getString(1) );
    catch( Exception e )
    System.err.println( e );
    }

  • Adapter module to read excel file -java code required for PI 7.1

    Hi PI experts,
    I am working on PI 7.1 SP 08.
    I am trying to develope an adapter module to read excel file
    http://wiki.sdn.sap.com/wiki/display/ABAP/AdapterModuleToReadExcelFilewithMultipleRowsandMultiple+Columns
    but here in this wiki , given java code is for pi 7.0 and it is using jar file from PI 7.0
    I tried with using corresponding PI 7.1 files
    com.sap.aii.af.lib.mod.jar:
    sap.comtcloggingjavaimpl.jar:
    com.sap.aii.af.svc_api.jar:
    com.sap.aii.af.cpa.svc_api.jar:
    com.sap.aii.af.ms.ifc_api.jar:
    jave program is not throwing any error in NWDS but After deploying file on server.
    i am getting this errot in communication channel
    2009-12-15 15:47:08 Information AO: Now calling the Convert Method to convert Excel to XML.
    2009-12-15 15:47:08 Error MP: exception caught with cause javax.ejb.TransactionRolledbackLocalException: nested exception is: java.lang.RuntimeException: java.lang.UnsupportedClassVersionError: Bad version number in .class file; nested exception is: javax.ejb.EJBException: nested exception is: java.lang.RuntimeException: java.lang.UnsupportedClassVersionError: Bad version number in .class file; nested exception is: javax.ejb.EJBTransactionRolledbackException: nested exception is: java.lang.RuntimeException: java.lang.UnsupportedClassVersionError: Bad version number in .class file; nested exception is: javax.ejb.EJBException: nested exception is: java.lang.RuntimeException: java.lang.UnsupportedClassVersionError: Bad version number in .class file
    since i am not a JAVA expert so i am unable to resolve this error
    if some one has already deployed this module for PI 7.1, then please provide me java code for PI 7.1
    Thanks
    sandeep sharma

    hi,
    please try this:
    obj = inputModuleData.getPrincipalData();
    msg = (Message) obj;
    amk = new MessageKey(msg.getMessageId(),msg.getMessageDirection());
    XMLPayload xpld = msg.getDocument();
    Workbook wb = Workbook.getWorkbook((InputStream) xpld.getInputStream());
    xmldata ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"+ "<ns0:"msgType" ""xmlns:ns0=\""nameSpace+"\">";
    Cell[] cells ;
    Cell[] cellNames ;
    cellNames = wb.getSheet(0).getRow(0);
    for(int j=1;j<wb.getSheet(0).getRows();j++)
    xmldata = xmldata+"<Record>";
    cells = wb.getSheet(0).getRow(j);
    for(int i=0;i<wb.getSheet(0).getColumns();i++)
    xmldata = xmldata"<"cellNames<i>.getContents()">"cells<i>.getContents()"</"cellNames<i>.getContents()+">";
    xmldata = xmldata+"</Record>";
    xmldata = xmldata"</ns0:"msgType+">"; 
    wb.close();
    byte byt[] = xmldata.getBytes();
    xpld.setContent(byt);
    inputModuleData.setPrincipalData(msg);
    Thanks,
    Mayank

  • Java API for reading Excel Files.

    Hi,
    Can you please suggest me any api for reading excel files.
    Right now i am using jxl.jar for this purpose but i am searching for an open source java api better than this.
    Whether POI gives better than this ?
    Thanks,
    Amit Shah.

    Can you please suggest me any api for reading excel
    files.
    Right now i am using jxl.jar for this purpose
    but i am searching for an open source java api better
    than this.
    Whether POI gives better than this ?i don't know about jxl but poi has several features which an excel can contain and it has been vastly improved in the past few versions.

  • Read/Write MS Excel file in Java

    Hi,
    I want to read Excel file & displau excel sheet data on HTML format.
    I was tried with POI.
    But i m now aware with POI.
    thanks in advance.

    Greetings.
    As I know, POI is one of the best solutions in this direction. You may try some other packages listed on http://schmidt.devlib.org/java/libraries-excel.html.
    Sincerely,
    wanderlust

  • Read&write Excel file using java

    Hi everybody,
    I have an assignment about methods that read and write an excel file using java (Eclipse SDK), so if anyone know about that please post the solution as soon as possible.
    Thanks
    Sendbad

    http://onesearch.sun.com/search/onesearch/index.jsp?qt=read+write+excel&subCat=siteforumid%3Ajava31&site=dev&dftab=siteforumid%3Ajava31&chooseCat=javaall&col=developer-forums

  • Error reading excel file using POI.

    Hi ,
    I am having some problem when reading the excel file.
    While reading excel file I am getting error-java.io.IOException: Unable to read entire block; 4 bytes read; expected 512 bytes.
    part of my code:-
    InputStream stream = objFormFile.getInputStream();
    POIFSFileSystem fs = new POIFSFileSystem(stream);//getting above error here.
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    Case 1:
    I download the file on the system ,save it ,and then directly upload it to the system ,I get the above error .
    Case 2:
    It works fine if I download the excel and manually open and save it and then upload it .
    I am using JDK1.4.1 and poi-2.5.1-final-20040804.jar.
    Can any one please help me out for the above problem?
    Edited by: hruday on Jul 31, 2008 3:20 AM

    Instead of using POIFSFileSystem, try to directly create the HSSFWorkbook
    InputStream stream = objFormFile.getInputStream();
    //POIFSFileSystem fs = new POIFSFileSystem( stream );//getting above error here.
    HSSFWorkbook wb = new HSSFWorkbook( stream );

  • How to save html as Excel file im java

    Hi to all
    How can i save html file as Excel file in Java
    If any solutions plz help Me

    Thanks for ur reply
    Source URL is::http://www.sttpc.com/reseller/price.htm
    Target is to save table data into Excel Sheet.
    Upto saving that file as Excel file its working (using I/O).
    When iam trying to read excel sheet data then iam getting OLE Exception.
    plz suggest me other Alternate solutions to overcome this problem.
    I tried htmlparser from sourceforge.net i got partial ouput.But i want dynamic solution.
    Thanks.

  • How to compare two excel files in java ?

    how do i compare two excel files in java.?
    I have two excel files stored on my computer in d: drive.
    Ex:
    D:\\file a
    D:\\file b
    How to compare the contents of these two files and print " files are equal " or "files not equal "

    Javamastermahe wrote:
    I mean i want to print on the console "files are equal " or any message like " both the files match "If this is your requirement, this program satisfies it...
    import java.util.Random;
    public class SuperExcelTester {
        public static void main(String[] args) {
            Random rnd = new Random();
            String[] messages = {
                "files are equal",
                "files are not equal",
                "unexpected error"
            int index = rnd.nextInt(messages.length);
            System.out.println(messages[index]);
    }

  • Customised module for reading Excel files.

    Hi Gurus,
    I have to develop a customsed java module for reading Excel files from PI. I downloaded NWDS 7.31 from following location NWDS Download links - Java Development - SCN Wiki. I have installed it on my local laptop. For developing the module I need following Jar files.
    com.sap.aii.af.ms.svc_api
    com.sap.aii.proxy.svc_api.jar
    com.sap.aii.sec.svc_api.jar
    com.sap.aii.utilxi.server.jar
    com.sap.aii.utilxi.core.jar
    com.sap.xi.mapping.tool.lib_api.jar
    sap.com~tc~bl~txmanagerimpl~plb~impl.jar
    sap.com~tc~je~appcontect_api~API.jar
    com.sap.aii.af.cpa.svc.api
    com.sap.aii.af.lib.mod
    com.sap.aii.af.ms.ifc_api
    com.sap.aii.af.svc_api
    com.sap.xpi.ib.mapping.lib
    1. Where can i get these jar files. I checked these files in my downloaded NWDS 7.31  package, but cold not find them.
    Thanks in advance

    Hi.
    Check this thread.
    Where to get the libraries for XI development - Process Integration - SCN Wiki
    Regards
    Lucho

  • How do I produce an excel file using java code

    How do i produce an excel file using java code.
    If it is possible would ne one have ne source code as an example.
    I have tried looking up information on outputing the information to a *.csv file but unfortunality i have had no luck in finding any information. To be exact ne source code.
    Thanks
    Lee

    That's what I use and I'd be the first to admit that it's very messy. But it works. Go use Google and see what else you can find.

  • How to Write data in Excel File using java

    Hi
    can anybody help me to write data in excel file
    using java code
    Thankx In Advance

    How much are you willing to pay for that?
    If you want it for free, http://jexcelapi.sourceforge.net/

  • Upload and Reading Excel File in Web Dynpro

    Hi all,
    I have a requirement in my application( in 04s), where in, i need to upload an excel from a client through a web dynpro application(using fileupload UI) and read each and every the content of that excel file in web dynpro and process the data accordingly.
    The format of the excel is fixed and pre-defined.
    I went through a lot of blogs, but could not find a direct and exact solution to this requirement.
    Please help me.
    Looking forward to your contribution
    Thank you,
    Gita KC.

    Reading Excel Sheet from Java without using any Framework
    Enhanced File Upload - Uploading and Processing Excel Sheets
    Reading Multiple Sheets of Excel Sheet from Java
    nikhil

  • How to connect XLSX(MS Office 2007 Excel file) in java

    i successfully connected ms office 2003 *.xls* file with the help of JXL library
    can any one tell me how can i connect XLSX (MS Office 2007 Excel file) in java.

    http://www.smartxls.com/indexj.htm is commercial library (not free) also a poor documentation
    can you give me a sample code of reading and writing xlsx file
    http://poi.apache.org/spreadsheet/index.html free library.
    but sample code are difficult to understand .
    if you have a good and simple example code please give me OR any other reffrences

  • How to format a excel file use java.

    hi,
    everyone, i meet a problem.i want to write a excel file use java.i can use .csv input the date to excel.but i want to know how to format it.
    thanks
    Jove

    hi,
    everyone, i meet a problem.i want to write a excel
    el file use java.i can use .csv input the date to
    excel.but i want to know how to format it.
    thanks
    JoveIf I understand your question correctly you need to know the format of a .csv file for use in an Excel spreadsheet. A .csv file is a comma-delimited file so all that you need to do is write your data elements to the file with commas in between them. Having done something similar in the past, let me warn you that when Excel is reading your file, it uses some characters other than commas as field delimiters. for example, / : and ; are treated as commas. To defeat this, you will need to surround fields containing those characters in double quotes.
    Hope that helps
    Mark

Maybe you are looking for

  • I've tried it all but still slow internet

    I've been suffering from very slow connection speeds for months now. At first I thought it was my DSL ISP so I switch to Comcast HSI and it's a little faster but still nowhere near acceptable. I just got off the line with Apple support and I was stun

  • How do I add album artwork in itunes 11.1.3.8? Copy and paste in artwork window on bottom right does not work.

    How do I add album artwork in itunes 11.1.3.8? Copy and paste to artwork box on bottom right in get info screen does not work.

  • Introscope - How to Question

    I need to configure an alert that will fire when a metric stops reporting.  So, the metric is not at a zero value... it sometimes stops.  I can'f figure out how to configure that. Thanks!

  • Best Way to import from iPhoto to Aperture?

    I am working with an iPhoto library of 40,000 images, which is about several hundred GB in size. The direct import of the iPhoto library doesn't seem to work - i.e. - Aperture shows in the activity window that it is processing 40,000 images, but it h

  • How reduce window size?

    I'm using a Mac Pro, OS X 10.7.2, Picasa 3: the window can't be reduced in size and the hatch marks are behind the dock, positioned on the bottom.  How do I make the window smaller?  I've tried moving the dock to the side and still the hatch marks do