gpt4 book ai didi

java - 如何在导出到数据库 spring 时从 Excel 文件中删除重复项

转载 作者:行者123 更新时间:2023-11-30 06:14:27 25 4
gpt4 key购买 nike

我正在从事一个项目,该项目必须将数据从 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;
}

最佳答案

您应该覆盖 Contact 中的 equals 方法并执行

if(!contactList.contains(contact))
{
contactList.add(contact);
}

关于java - 如何在导出到数据库 spring 时从 Excel 文件中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49532742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com