Java Excel搜索和数组列表工作不正常

Java Excel搜索和数组列表工作不正常,java,excel,for-loop,arraylist,apache-poi,Java,Excel,For Loop,Arraylist,Apache Poi,我的代码有问题 它没有按我希望的方式工作。 对不起,我解释得这么糟糕( (我使用ApachePOI版本4.1.1) 我的问题是:我有一个包含多个条目的excel表格。每个条目包括第1列中的装运日期、第2列中的客户名称、第15列中的托盘ID和第16列中的装运状态。 最重要的是,各专栏都有标题 excel包含多个客户,其中一些客户多次访问,唯一的区别是托盘ID。 除其他外,我的客户对象类包含一个数组列表,其中可能包含多个不同的托盘ID 我想列出每个客户,其中航运是今天,只有一次,但与所有他的托盘ID

我的代码有问题

它没有按我希望的方式工作。 对不起,我解释得这么糟糕(

(我使用ApachePOI版本4.1.1)

我的问题是:我有一个包含多个条目的excel表格。每个条目包括第1列中的装运日期、第2列中的客户名称、第15列中的托盘ID和第16列中的装运状态。 最重要的是,各专栏都有标题

excel包含多个客户,其中一些客户多次访问,唯一的区别是托盘ID。 除其他外,我的客户对象类包含一个数组列表,其中可能包含多个不同的托盘ID

我想列出每个客户,其中航运是今天,只有一次,但与所有他的托盘ID

我的客户类如下所示:

import java.util.ArrayList;
import java.util.Date;

public class Customer {
    
    private String customername;
    private ArrayList<String> pallet = new ArrayList<>();
    private String date;
    private String status;
    
    public Customer(){
        
    }

    public String getCustomerName() {
        return customername;
    }

    public void setCustomerName(String customername) {
        this.customername = customername;
    }

    public String getFromPallet(int i) {
        return pallet.get(i);
    }
    
    public ArrayList<String> getPallet() {
        return pallet;
    }
    
    public ArrayList<String> get(int j) {
        return pallet;
    }

    public void setPallet(ArrayList<String> pallet) {
        this.pallet = pallet;
    }
    
    public void addPallet(String string) {
        this.pallet.add(string);
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
    
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String toString(){
        return String.format("%1s - %2s - %3s", customername, date, status);
    }    
    
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class NewTrueSimpleExcelReader {
    
    SimpleDateFormat SDF = new SimpleDateFormat("dd.MM.yyyy");
    
    Date today = new Date();
    Date BBB;
    int lastfound = 0;
    
    public List<Customer> readCustomersFromExcelFile(String excelFilePath) throws IOException, ParseException {
        
        List<Customer> listCustomers = new ArrayList<>();
        FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
        XSSFWorkbook wb = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = wb.getSheetAt(0);
        
        XSSFRow firstrow = sheet.getRow(1);
        
        int fr = firstrow.getRowNum();
        XSSFRow row;
        
        while(fr < sheet.getLastRowNum()){
            
            row = sheet.getRow(fr);
            if(!row.getCell(0).getStringCellValue().equals("")){
                String AAA = row.getCell(0).getStringCellValue().trim();
                BBB = SDF.parse(AAA);
            
                if((SDF.format(BBB)).equals(SDF.format(today))){
                    Customer aCustomer = new Customer();
                    
                    System.out.println("Customer: "+row.getCell(1).getStringCellValue().trim());
                    
                    if(listCustomers.size() > 0 && row.getCell(16) == null){
                        
                        for(int i=0; i<listCustomers.size(); i++){
                            
                            System.out.println("i is: "+i);
                            String string1 = listCustomers.get(i).getCustomerName();
                            String string2 = row.getCell(1).getStringCellValue();
                            
                            if(string1.equals(string2) && row.getRowNum() != lastfound){
                                lastfound = row.getRowNum();
                                System.out.println("Dupl found");
                                for(int j=0; j<listCustomers.get(i).get(j).size(); j++){
                                    if(!row.getCell(15).getStringCellValue().trim().equals(listCustomers.get(i).getFromPallet(j))){
                                        listCustomers.get(i).addPallet(row.getCell(15).getStringCellValue());
                                    }
                                    
                                }
                            } else{
                                if(row.getRowNum() != lastfound){
                                System.out.println("second+: "+row.getCell(0).getStringCellValue().trim());
                                lastfound = row.getRowNum();
                                aCustomer.setDate(row.getCell(0).getStringCellValue().trim());
                                aCustomer.setCustomerName(row.getCell(1).getStringCellValue());
                                aCustomer.setStatus("unused");
                                if(row.getCell(15) != null){ //&& !string1.equals(string2)
                                aCustomer.addPallet(row.getCell(15).getStringCellValue());
                                }
                                listCustomers.add(aCustomer);
                            }
                            }
                        }
                        
                    } else{ 
                        if(row.getCell(16) == null){
                        lastfound = row.getRowNum();
                        System.out.println("first: "+row.getCell(0).getStringCellValue().trim());
                        aCustomer.setDate(row.getCell(0).getStringCellValue().trim());
                        aCustomer.setCustomerName(row.getCell(1).getStringCellValue());
                        aCustomer.setStatus("unused");
                        if(row.getCell(15) != null){
                        aCustomer.addPallet(row.getCell(15).getStringCellValue());
                        }
                        listCustomers.add(aCustomer);
                    }
                    }
                    
                    
                    
                }
            
            }
           
          fr++;  
        }
        
        
        System.out.println(listCustomers.size());
        wb.close();
        inputStream.close();
        return listCustomers;
    }
    
}
import java.util.ArrayList;
导入java.util.Date;
公共类客户{
私有字符串客户名称;
私有ArrayList托盘=新ArrayList();
私有字符串日期;
私有字符串状态;
公众客户(){
}
公共字符串getCustomerName(){
返回客户名称;
}
public void setCustomerName(字符串customername){
this.customername=客户名称;
}
公共字符串getFromPallet(int i){
返回托盘。获取(i);
}
公共阵列列表getPallet(){
返回托盘;
}
公共ArrayList get(int j){
返回托盘;
}
公共无效设置托盘(ArrayList托盘){
这个托盘=托盘;
}
公共空托盘(字符串){
此.托盘.添加(字符串);
}
公共字符串getDate(){
返回日期;
}
公共无效设置日期(字符串日期){
this.date=日期;
}
公共字符串getStatus(){
返回状态;
}
公共无效设置状态(字符串状态){
这个状态=状态;
}
公共字符串toString(){
返回字符串。格式(“%1s-%2s-%3s”、客户名称、日期、状态);
}    
}
我的主要课程是这样的:

import java.util.ArrayList;
import java.util.Date;

public class Customer {
    
    private String customername;
    private ArrayList<String> pallet = new ArrayList<>();
    private String date;
    private String status;
    
    public Customer(){
        
    }

    public String getCustomerName() {
        return customername;
    }

    public void setCustomerName(String customername) {
        this.customername = customername;
    }

    public String getFromPallet(int i) {
        return pallet.get(i);
    }
    
    public ArrayList<String> getPallet() {
        return pallet;
    }
    
    public ArrayList<String> get(int j) {
        return pallet;
    }

    public void setPallet(ArrayList<String> pallet) {
        this.pallet = pallet;
    }
    
    public void addPallet(String string) {
        this.pallet.add(string);
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
    
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String toString(){
        return String.format("%1s - %2s - %3s", customername, date, status);
    }    
    
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class NewTrueSimpleExcelReader {
    
    SimpleDateFormat SDF = new SimpleDateFormat("dd.MM.yyyy");
    
    Date today = new Date();
    Date BBB;
    int lastfound = 0;
    
    public List<Customer> readCustomersFromExcelFile(String excelFilePath) throws IOException, ParseException {
        
        List<Customer> listCustomers = new ArrayList<>();
        FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
        XSSFWorkbook wb = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = wb.getSheetAt(0);
        
        XSSFRow firstrow = sheet.getRow(1);
        
        int fr = firstrow.getRowNum();
        XSSFRow row;
        
        while(fr < sheet.getLastRowNum()){
            
            row = sheet.getRow(fr);
            if(!row.getCell(0).getStringCellValue().equals("")){
                String AAA = row.getCell(0).getStringCellValue().trim();
                BBB = SDF.parse(AAA);
            
                if((SDF.format(BBB)).equals(SDF.format(today))){
                    Customer aCustomer = new Customer();
                    
                    System.out.println("Customer: "+row.getCell(1).getStringCellValue().trim());
                    
                    if(listCustomers.size() > 0 && row.getCell(16) == null){
                        
                        for(int i=0; i<listCustomers.size(); i++){
                            
                            System.out.println("i is: "+i);
                            String string1 = listCustomers.get(i).getCustomerName();
                            String string2 = row.getCell(1).getStringCellValue();
                            
                            if(string1.equals(string2) && row.getRowNum() != lastfound){
                                lastfound = row.getRowNum();
                                System.out.println("Dupl found");
                                for(int j=0; j<listCustomers.get(i).get(j).size(); j++){
                                    if(!row.getCell(15).getStringCellValue().trim().equals(listCustomers.get(i).getFromPallet(j))){
                                        listCustomers.get(i).addPallet(row.getCell(15).getStringCellValue());
                                    }
                                    
                                }
                            } else{
                                if(row.getRowNum() != lastfound){
                                System.out.println("second+: "+row.getCell(0).getStringCellValue().trim());
                                lastfound = row.getRowNum();
                                aCustomer.setDate(row.getCell(0).getStringCellValue().trim());
                                aCustomer.setCustomerName(row.getCell(1).getStringCellValue());
                                aCustomer.setStatus("unused");
                                if(row.getCell(15) != null){ //&& !string1.equals(string2)
                                aCustomer.addPallet(row.getCell(15).getStringCellValue());
                                }
                                listCustomers.add(aCustomer);
                            }
                            }
                        }
                        
                    } else{ 
                        if(row.getCell(16) == null){
                        lastfound = row.getRowNum();
                        System.out.println("first: "+row.getCell(0).getStringCellValue().trim());
                        aCustomer.setDate(row.getCell(0).getStringCellValue().trim());
                        aCustomer.setCustomerName(row.getCell(1).getStringCellValue());
                        aCustomer.setStatus("unused");
                        if(row.getCell(15) != null){
                        aCustomer.addPallet(row.getCell(15).getStringCellValue());
                        }
                        listCustomers.add(aCustomer);
                    }
                    }
                    
                    
                    
                }
            
            }
           
          fr++;  
        }
        
        
        System.out.println(listCustomers.size());
        wb.close();
        inputStream.close();
        return listCustomers;
    }
    
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.text.ParseException;
导入java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.List;
导入org.apache.poi.xssf.usermodel.XSSFRow;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公共类NewTrueSimpleExcelReader{
SimpleDataFormat SDF=新的SimpleDataFormat(“dd.MM.yyyy”);
今天日期=新日期();
日期BBB;
int lastfound=0;
公共列表读取器CustomersFromExcelFile(字符串excelFilePath)引发IOException、ParseException{
List listCustomers=new ArrayList();
FileInputStream inputStream=新FileInputStream(新文件(excelFilePath));
XSSF工作簿wb=新XSSF工作簿(inputStream);
XSSFSheet-sheet=wb.getSheetAt(0);
XSSFRow firstrow=sheet.getRow(1);
int fr=firstrow.getRowNum();
XSSFRow行;
而(fr0&&row.getCell(16)==null){
对于(int i=0;i在下面创建一个映射

Map<String, Customer> map = new HashMap<String, Customer>();

对于具有不同托盘Id的同一客户,状态是否相同?嘿!感谢您的回复:)如果excel中的第16列为空,则状态将为“未使用”。如果第16列不为空,则必须跳过该条目,因为我只想列出发货日期为今天且第16列为空的客户。嘿!谢谢你的hep!我已获得你的代码。:)但我的问题是:如何防止客户多次使用相同的托盘ID?如果我正确理解您的代码,如果客户是相同的,它只会将托盘ID添加到现有的托盘ID中,但不会检查ID是否已经存在。嘿@Hades!我让它为我工作了!即使在cust中检查重复项omer object!Woohoo!非常感谢您的帮助。我想,如果没有它,我就不会找到解决问题的方法!非常感谢!:)(我用我的工作功能更新了我的帖子!)@Shathos我很高兴:)