如何使用 POI 检查 excel 中的重复记录?
How to check for duplicate records in excel using POI?
下面是使用 poi 读取 excel 文件的代码:工作正常
public class ReadExcelDemo {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("demo.xlsx"));
List sheetData = new ArrayList();
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
ArrayList<Form> vipList = new ArrayList<Form>();
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
List data = new ArrayList();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t");
break;
}
}
}
}
现在,如果 excel 包含重复记录,我应该能够打印一条简单的错误消息。我该怎么做?
示例:
ID Firstname Lastname Address
1 Ron wills Paris
1 Ron wills London
现在我只想检查 3 列的重复项:ID、名字和姓氏。如果这些列一起包含与上例所示相同的数据,则需要将其视为重复。
我有一个 pojo class 表单由带有 getter 的 id、名字和姓氏组成
和 setters。使用 setter 方法将读取的每个记录写入 pojo class。然后我使用 getter 获取值并将它们添加到 arraylist 对象。现在列表对象包含所有记录。我如何比较它们?
将数据放入一个集合中,并在每个新条目之前检查包含。如果您使用 HashSet,它会非常快。你可以假装一切都是字符串来进行比较。
Set data = new HashSet();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(data.contains(cell.getStringCellValue())
trow new IllegalDataException()
data.add(cell.getStringCellValue();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t");
break;
}
}
如果您需要实际比较整行,您可以创建一个包含所有字段的 class,然后只需重写 equals 方法。然后将其放入一组并进行比较。
public class ProcessAction extends DispatchAction {
String dupValue = null;
ArrayList<String> dupList = new ArrayList<String>();
private String validateDuplicateRecords(ProcessForm process) {
String errorMessage = null;
dupValue = process.getId.trim()+" "+process.getFirstname().trim()+" "+process.getLastanme().trim();
mLogger.debug("order id,ctn,item id: "+dupValue);
if (dupList.contains(dupValue)){
mLogger.debug("value not added");
errorMessage = "Duplicate Record Exists";
} else {
dupList.add(dupValue);
}
return errorMessage;
}
}
不要忘记清除重复的数组列表。我的情况是在执行某些任务后,例如将 arraylist 写入文件,我正在使用以下方法清除重复的 arraylist:
dupList.clear();
如果你不这样做,那么当你再次上传相同的数据时,即使记录不重复,它也会说重复,因为 dupList arraylist 包含以前上传的数据。
这是一个提示。当你循环时,在哈希图中添加你的 id(检查重复的值)。如果地图的大小没有改变,那么它是一个重复的记录,因为如果键已经存在,它们会相互覆盖。这是我的代码中的一个示例:
switch(cellType)
{
case 0:
your_id = cell1.getNumericCellValue();
mapSize = map.size();
map.put(your_id, your_id);
mapSizeAfterPut = map.size();
if(mapSize == mapSizeAfterPut)
{
duplicatedRecordsList.add(index);
}
break;
case 1:
your_id = cell1.getStringCellValue();
mapSize = map.size();
map.put(your_id , your_id);
mapSizeAfterPut = map.size();
if(mapSize == mapSizeAfterPut)
{
duplicatedRecordsList.add(index);
}
break;
default:break;
}
下面是使用 poi 读取 excel 文件的代码:工作正常
public class ReadExcelDemo {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("demo.xlsx"));
List sheetData = new ArrayList();
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
ArrayList<Form> vipList = new ArrayList<Form>();
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
List data = new ArrayList();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t");
break;
}
}
}
}
现在,如果 excel 包含重复记录,我应该能够打印一条简单的错误消息。我该怎么做?
示例:
ID Firstname Lastname Address
1 Ron wills Paris
1 Ron wills London
现在我只想检查 3 列的重复项:ID、名字和姓氏。如果这些列一起包含与上例所示相同的数据,则需要将其视为重复。
我有一个 pojo class 表单由带有 getter 的 id、名字和姓氏组成
和 setters。使用 setter 方法将读取的每个记录写入 pojo class。然后我使用 getter 获取值并将它们添加到 arraylist 对象。现在列表对象包含所有记录。我如何比较它们?
将数据放入一个集合中,并在每个新条目之前检查包含。如果您使用 HashSet,它会非常快。你可以假装一切都是字符串来进行比较。
Set data = new HashSet();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(data.contains(cell.getStringCellValue())
trow new IllegalDataException()
data.add(cell.getStringCellValue();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t");
break;
}
}
如果您需要实际比较整行,您可以创建一个包含所有字段的 class,然后只需重写 equals 方法。然后将其放入一组并进行比较。
public class ProcessAction extends DispatchAction {
String dupValue = null;
ArrayList<String> dupList = new ArrayList<String>();
private String validateDuplicateRecords(ProcessForm process) {
String errorMessage = null;
dupValue = process.getId.trim()+" "+process.getFirstname().trim()+" "+process.getLastanme().trim();
mLogger.debug("order id,ctn,item id: "+dupValue);
if (dupList.contains(dupValue)){
mLogger.debug("value not added");
errorMessage = "Duplicate Record Exists";
} else {
dupList.add(dupValue);
}
return errorMessage;
}
}
不要忘记清除重复的数组列表。我的情况是在执行某些任务后,例如将 arraylist 写入文件,我正在使用以下方法清除重复的 arraylist:
dupList.clear();
如果你不这样做,那么当你再次上传相同的数据时,即使记录不重复,它也会说重复,因为 dupList arraylist 包含以前上传的数据。
这是一个提示。当你循环时,在哈希图中添加你的 id(检查重复的值)。如果地图的大小没有改变,那么它是一个重复的记录,因为如果键已经存在,它们会相互覆盖。这是我的代码中的一个示例:
switch(cellType)
{
case 0:
your_id = cell1.getNumericCellValue();
mapSize = map.size();
map.put(your_id, your_id);
mapSizeAfterPut = map.size();
if(mapSize == mapSizeAfterPut)
{
duplicatedRecordsList.add(index);
}
break;
case 1:
your_id = cell1.getStringCellValue();
mapSize = map.size();
map.put(your_id , your_id);
mapSizeAfterPut = map.size();
if(mapSize == mapSizeAfterPut)
{
duplicatedRecordsList.add(index);
}
break;
default:break;
}