Java HashMap获取所有值,但只返回最后一个条目
我正在从hashmap获取数据。我想使用POI将其添加到excel文件中。但问题是程序只添加excel中的最后一项。但在调试时,它会给出所有值。 这是我的密码Java HashMap获取所有值,但只返回最后一个条目,java,android,excel,apache-poi,Java,Android,Excel,Apache Poi,我正在从hashmap获取数据。我想使用POI将其添加到excel文件中。但问题是程序只添加excel中的最后一项。但在调试时,它会给出所有值。 这是我的密码 for (int j = 0; j < proList.size(); j++) { HashMap hashMap = proList.get(j); Iterator<Object> iterator = hashMap.entrySet().iterator();
for (int j = 0; j < proList.size(); j++) {
HashMap hashMap = proList.get(j);
Iterator<Object> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
rows=sheet1.createRow(j+1);
cc=rows.createCell(j);
cc.setCellValue(entry.getValue());
}
}
看起来您应该在外部循环中创建行。 您可能需要为这些列创建一个单独的索引
for (int j = 0; j < proList.size(); j++) {
HashMap hashMap = proList.get(j);
rows=sheet1.createRow(j+1);
Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
int col = 0;
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
cc=rows.createCell(col++);
cc.setCellValue(entry.getValue());
}
}
您正在使用j作为索引,它在while循环中从不递增。因此它总是只创建一行
注意:您使用的索引“j”来自while循环外部的for循环声明。其限制将仅为prolist.size!我认为您应该在while循环中使用单独的索引。返回是什么意思?你确定它正在保存所有的值吗?谢谢你。。这正是我想要的