我正在一个项目中工作,该项目必须将数据从excel文件导入和导出到数据库,否则。我必须从数据库中获取数据然后。但是如何根据id,电话号码,用户名检查excel文件中的这些数据是否不重复。 这是从excel文件中读取的方法
@Override
public List<Contact> read(String FilePath) throws IOException {
List<Contact> contactList = new ArrayList<>();
FileInputStream inputStream = new FileInputStream(new File(FilePath));
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
HSSFSheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
iterator.next();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
Contact contact = new Contact();
while (cellIterator.hasNext()) {
Cell nextCell = cellIterator.next();
int columnIndex = nextCell.getColumnIndex();
switch (columnIndex) {
case 0:
// contact.setId((String) nextRow.getCell(nextCell));
contact.setId((long) (nextCell.getNumericCellValue()));
break;
case 1:
contact.setFirstName(nextCell.getStringCellValue());
break;
case 2:
contact.setLastName(nextCell.getStringCellValue());
break;
case 3:
contact.setPhoneNumber(nextCell.getStringCellValue());
break;
case 4:
contact.setAddress(nextCell.getStringCellValue());
break;
case 5:
contact.setCity(nextCell.getStringCellValue());
break;
case 6:
contact.setEmail(nextCell.getStringCellValue());
break;
case 7:
contact.setZipCode((int) (nextCell.getNumericCellValue()));
break;
}
}
contactList.add(contact);
}
workbook.close();
inputStream.close();
return contactList;
}
答案 0 :(得分:1)
你应该在接触中覆盖equals方法并执行
if(!contactList.contains(contact))
{
contactList.add(contact);
}