• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with reading from xls file

 
Laura Morley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2965
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Laura Morley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much
 
Laura Morley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys. Managed to get it working now

 
Laura Morley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Laura Morley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic