aspose file tools*
The moose likes Beginning Java and the fly likes Help with reading from xls file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with reading from xls file" Watch "Help with reading from xls file" New topic
Author

Help with reading from xls file

Laura Morley
Greenhorn

Joined: Sep 30, 2013
Posts: 8
Good morning

I am very new to Java and I need to read from an XLS file into a username and password field.

My xls file is set up like this:

Test Name Username Password
TestCase_01 laura test
TestCase_02 laura TEST



I have a website that I am trying to login to.

What I need to do is read the colomns and put them in to a variable, but I havent got a clue how to do this.

If anyone could help, that would be brilliant

Thank you for reading
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
Welcome to the Ranch.

If you want to read data from a XLS file the easiest way is to use a 3rd party library. There are open source ones available such as Apache POI or JExcel

Laura Morley
Greenhorn

Joined: Sep 30, 2013
Posts: 8
Hi
Thank you very much for your reply.

I have got a little further with this code



With the workbook, its asking me to import, which one do I need to import
a) import org.apache.poi.hssf.model.Workbook;

or

b) import org.apache.poi.ss.usermodel.Workbook;
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
You should stick with the "ss" package - it is designed to work with both XLS and XLSX files. "hssf" works with XLS only, whereas "xssf" works with XLSX only.


Ping & DNS - my free Android networking tools app
Laura Morley
Greenhorn

Joined: Sep 30, 2013
Posts: 8
Thank you very much
Laura Morley
Greenhorn

Joined: Sep 30, 2013
Posts: 8
Thank you guys. Managed to get it working now

Laura Morley
Greenhorn

Joined: Sep 30, 2013
Posts: 8
This whole reading from a excel sheet is much harder than I thought!

I got it reading like this



Problem is that I need some kind of loop as the cells are hard coded and I need it to read down the colomns as I have different usernames and passwords.

Can anyone help me out with the loop and how I would do it. I thought orginally you could do something like this:

for (int x = 0; int c = 1; d = 1; x < 4; x++; c++; d++)

but I dont think would work...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
Yes, that works, but you need to get the syntax right:

for (int x = 0, c = 1, d = 1; x < 4; x++, c++, d++)
Stuie Clarky
Ranch Hand

Joined: Nov 09, 2012
Posts: 76

For an extra bit of safety, it might be worth changing the file location string you specified earlier to use File.separator instead of \\. It might not be an issue, but if it is run or marked on a *NIX machine then it won't work. The File.separator will use the correct separator for the OS.

Probably will make zero difference for the current bit of work you are doing, but it is worth keeping in mind going forward

OCAJP 7
Laura Morley
Greenhorn

Joined: Sep 30, 2013
Posts: 8
Thank you for the replies and help

Now I have the problem of trying to read a number from the excel file. Currently getting this error

Cannot get a text value from a numeric cell

I have tried changing it to an INT but i am not sure how to go about getting it to work
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
If a cell is formatted as a number, then you'd need to use getNumericCellValue instead of getStringCellValue. You can find out the type of the cell by calling getCellType.
Mark Beardsley
Ranch Hand

Joined: Jun 07, 2013
Posts: 32
    
    1
The standard approach to reading from cells is this - http://poi.apache.org/spreadsheet/quick-guide.html#CellContents

There is a lot of help available to you on the poi website and the Quick Guide is just one resource that you may find useful.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with reading from xls file