开发者

Groovy/POI Returning Different Iterators on Different Systems

开发者 https://www.devze.com 2023-03-05 10:52 出处:网络
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.

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 syste开发者_JAVA百科m it is returning the following

java.util.HashMap$ValueIterator@38fff7

Any ideas about this discrepancies?


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


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" 
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号