2

I have the following code. Its purpose is to run through an xls file using POI and write all the data to a txt file.

for ( sheetNumber in 0..numberOfSheets-1) {
    HSSFSheet sheet = workBook.getSheetAt(sheetNumber)
    Iterator<HSSFRow> rows = sheet.rowIterator()
    while(rows.hasNext()){
        row = rows.next()
        Iterator<HSSFCell> cells = row.cellIterator();
        println "cell:" + cells.toString()
        while(cells.hasNext()){
            cell = cells.next()
            allEntityFile << cell.toString()
        } 
    allEntityFile << "\n" 
    }
}

On my machine this code works fine, but on another computer it seems to have trouble. I narrowed it down to this. When I try to create the cells iterator

Iterator<HSSFCell> cells = row.cellIterator();

my system returns

org.apache.poi.hssf.usermodel.HSSFRow$CellIterator@156b386

Which is what I would expect. While on another system it is returning the following

java.util.HashMap$ValueIterator@38fff7

Any ideas about this discrepancies?

Danilo Piazzalunga
  • 7,590
  • 5
  • 49
  • 75
KevinO
  • 970
  • 2
  • 13
  • 31

2 Answers2

2

Are you sure you're running the same version of POI on both systems? And are you using HSSF on both?

Recent versions of HSSF should always return a org.apache.poi.hssf.usermodel.HSSFRow$CellIterator to you.

With XSSF, the iterator you get back is taken from a TreeMap (it's the values iterator), so I wouldn't expect a HashMap iterator but I would expect a java.util one

That makes me think that you're possibly not using the same version of POI in both places

See the POI FAQ for how to check which jar file you're using for POI

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
2

I agree with @Gagravarr...you have a different version of something somewhere

And FYI, a more 'groovy' version of your code would be:

(0..<numberOfSheets).each { sheetNumber ->
  HSSFSheet sheet = workBook.getSheetAt( sheetNumber )
  sheet.rowIterator().each { row ->
    row.cellIterator().each { cell ->
      allEntityFile << cell.toString()
    } 
    allEntityFile << "\n" 
  }
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Great groovification. I've been working with some Java veterans who are a bit cautious of the Groovy syntax. So I have to sneak it in piece by piece. – KevinO May 11 '11 at 14:58