我正在尝试使用 POI 和 hashMaps 编写一个 excel 表。我的代码成功创建并填充了一个 Excel 工作表,但并非所有信息都写入工作表。在调试模式下,它似乎写入了 5 个键值对,然后循环到键 [0] 的开头。有人能告诉我逻辑中的错误在哪里吗?
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(make + " sheet");
int rowNum = 0;
ConcurrentHashMap <Integer, String[] > data = new
ConcurrentHashMap<Integer, String[]>();
data.put(rowNum++, new String[] {"VIN", "Make", "Model", "Year",
"Major Group", "Caption", "Cap Filter", "Illustration",
"Illus Filter", "PNC", "Part Name", "Part Number", "Quantity",
"Part Filter"});
for (Part p : plist){
String PNC = p.getPNC();
String quantity = p.getQuantity();
if(vFilterId.contains(p.getId())) {
data.put(rowNum++ , new String[] {vinId, make,
model, year, group, caption, capFilter, illusName,
iFilter, PNC, p.getName(), p.getPartNumber(),
quantity, "NONFILTERED"});
} else {
data.put(rowNum++ , new String[] {vinId, make,
model, year, group, caption, capFilter, illusName,
iFilter, PNC, p.getName(), p.getPartNumber(),
quantity, "NONFILTERED"});
}
}
int rowIndex = 0;
Set<Integer> keyset = data.keySet();
for (Integer key : keyset) {
Row row = sheet.createRow(rowIndex++);
Object[] objArr = data.get(key);
int cellIndex = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellIndex++);
cell.setCellValue((String) obj);
}
}
try {
FileOutputStream fos = new FileOutputStream(outputFile);
workbook.write(fos);
fos.close();
System.out.println("XLS sheet written.");
} catch (IOException e) {
e.printStackTrace();
}
}
return ReturnConstants.SUCCESS;
}
提前致谢!