Groovy/POI 在不同系统上返回不同迭代器

发布于 2024-11-06 11:27:58 字数 937 浏览 0 评论 0原文

我有以下代码。其目的是使用 POI 运行 xls 文件并将所有数据写入 txt 文件。

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

在我的机器上这段代码运行良好,但在另一台计算机上似乎有问题。我把范围缩小到这个。当我尝试创建单元格迭代器时,

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

我的系统返回

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

这就是我所期望的。在另一个系统上它返回以下内容

java.util.HashMap$ValueIterator@38fff7

关于这种差异有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

憧憬巴黎街头的黎明 2024-11-13 11:27:58

您确定在两个系统上运行相同版本的 POI 吗?您在两者上都使用 HSSF 吗?

最新版本的 HSSF 应始终向您返回 org.apache.poi.hssf.usermodel.HSSFRow$CellIterator

使用 XSSF,您返回的迭代器是从 TreeMap 中获取的(它是值迭代器),因此我不会期望 HashMap 迭代器,但我会期望 java.util 迭代器,

这让我认为您可能没有使用两个地方的 POI 版本相同

请参阅POI 常见问题解答了解如何检查哪个位置您用于 POI 的 jar 文件

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

腻橙味 2024-11-13 11:27:58

我同意@Gagravarr...你在某处有不同版本的东西

仅供参考,你的代码的更“常规”版本将是:

(0..<numberOfSheets).each { sheetNumber ->
  HSSFSheet sheet = workBook.getSheetAt( sheetNumber )
  sheet.rowIterator().each { row ->
    row.cellIterator().each { cell ->
      allEntityFile << cell.toString()
    } 
    allEntityFile << "\n" 
  }
}

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" 
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文