我是编程新手。我有一个程序可以迭代 excel 文件并将值放入哈希映射中 HashMapbut 哈希映射中的对象总是有最后一条记录。我会发布我的代码,也许你可以告诉我我的错误,或者更好地发布代码应该如何完成。请帮忙,真的卡在这里。
我的excel文件:
doc seq acc status notes
1111 2 account test value1
22222 3 account test2 value2
我的对象....
public class Account{
int docNumber;
int docSequence;
String docAccCode;
String docStatus;
String docNotes;
//getters and setters bellow
......
}
我的迭代方法...
public void readFile() throws IOException{
InputStream ExcelFileToRead = new FileInputStream(inputFile);
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row = null;
XSSFCell cell = null;
int recordNumber = 0;
Iterator rows = sheet.rowIterator();
while (rows.hasNext()){
row=(XSSFRow) rows.next();
int rowNum = row.getRowNum();
int cellNum = 0;
if(rowNum != 0){
Iterator cells = row.cellIterator();
while (cells.hasNext()){
cell=(XSSFCell) cells.next();
switch(cellNum){
case 0:
docMap.setDocNumber((int)cell.getNumericCellValue());
break;
case 1:
docMap.setDocSequence((int)cell.getNumericCellValue());
break;
case 2:
docMap.setDocAccCode(cell.getStringCellValue());
break;
case 3:
docMap.getDocStatus(cell.getStringCellValue());
break;
case 4:
docMap.getDocNotes(cell.getStringCellValue());
}
cellNum ++;
}
// docMap overwrites it's self and has last record only.
records.put(recordNumber, docMap);
recordNumber ++;
}
}
}