• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

POI api to read MSExcel sheet

 
shukla raghav
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a small code segment that i have extracted from a program to read MS excel file using POI api. I am getting following error messages in NetBeans at the lines i have colored corresponding to the code lines

ERROR ENCOUNTERED:
1. incompatible types
Found : org.apache.poi.hssf.usermodel.HSSFSheet.Iterator
Required : java.util.Iterator<org.apache.poi.hssf.usermodel.HSSFRow>


2. incompatible types
Found : java.util.Iterator<org.apache.poi.ss.usermodel.Cell>
Required : java.util.Iterator<org.apache.poi.[b]hssf
.usermodel.HSSFCell>

[/b]
[code]//instantiate POIFSFileSystem encapsulating inputstream
fileSystem = new POIFSFileSystem(inputStream);

//create a java usable(High level) worksheet from the POIFSFileSystem
HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);

//obtain worksheet from high level WorkBook object
HSSFSheet sheet = workBook.getSheetAt(0);


//Create Iterator object to get Rows
Iterator<HSSFRow> rows = sheet.Iterator();

//Iterate to get Rows
while (rows.hasNext ())
{
//Get High Level Row Object
HSSFRow row = rows.next ();

//Display row number in the console.
System.out.println ("Row No.: " + row.getRowNum ());

//Create Iterator to get Cells
Iterator<HSSFCell> cells = row.cellIterator();


what could be the reason?

 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shukla raghav wrote::
1. incompatible types
Found : org.apache.poi.hssf.usermodel.HSSFSheet.Iterator
Required : java.util.Iterator<org.apache.poi.hssf.usermodel.HSSFRow>


2. incompatible types
Found : java.util.Iterator<org.apache.poi.ss.usermodel.Cell>
Required : java.util.Iterator<org.apache.poi.[b]hssf
.usermodel.HSSFCell>

[/b]what could be the reason?

Use the API properly. HSSFSheet.iterator() returns an Iterator<Row>, not an Iterator<HSSFRow>, and Row.cellIterator() returns an Iterator<Cell>, not an Iterator<HSSFCell>. I think this will solve both your problems.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic