有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用POI检查excel中的重复记录?

下面是使用poi:读取excel文件的代码,poi:工作正常

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、Firstname和Lastname。如果这些列一起包含与上述示例中所示相同的数据,则需要将其视为重复

我有一个pojo类表单,由id、firstname和lastname以及getter组成

和二传手。每次读取的记录都使用setter方法写入pojo类。然后我使用getter获取这些值,并将它们添加到arraylist对象中。现在列表对象包含所有记录。我如何比较它们


共 (3) 个答案

  1. # 1 楼答案

    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写入文件)后,我会使用以下方法清除重复的arraylist:

    dupList.clear();
    

    如果不这样做,那么当您再次上传相同的数据时,即使记录不重复,也会说是重复的,因为dupList arraylist包含以前上传的数据

  2. # 2 楼答案

    给你个建议。在循环时,将id(用于检查复制的值)添加到hashmap中。如果地图的大小没有改变,那么它是一个重复的记录,因为如果键已经存在,它们会相互覆盖。下面是我的一段代码中的一个例子:

    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;
    }
    
  3. # 3 楼答案

    将数据放入一个集合,并在每个新条目之前检查包含的内容。如果你使用哈希集,它会非常快。你可以假装一切都是字符串来进行比较

            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;     
            }           
        }
    

    如果需要实际比较整行,可以创建一个包含所有字段的类,然后重写equals方法。然后把它放在一组中进行比较