Groovy/POI 在不同系统上返回不同迭代器
我有以下代码。其目的是使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定在两个系统上运行相同版本的 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
我同意@Gagravarr...你在某处有不同版本的东西
仅供参考,你的代码的更“常规”版本将是:
I agree with @Gagravarr...you have a different version of something somewhere
And FYI, a more 'groovy' version of your code would be: