简体   繁体   English

如何从csv文件读取特定数据并将其删除?

[英]How to read specific data from a csv file and delete it?

I am coding a program for a personal trainer to manage clients, I want to be able to remove clients off of the database (an Excel spreadsheet) and I also want to be able to read their plans from another database ( another Excel Spreadsheet). 我正在为个人教练编写程序来管理客户,我希望能够从数据库(一个Excel电子表格)中删除客户,并且我还希望能够从另一个数据库(另一个Excel电子表格)中读取他们的计划。 I am unsure how to delete people from a spreadsheet using Java. 我不确定如何使用Java从电子表格中删除人员。 Secondly, I am unsure how to read a section from a spreadsheet, for example, if the client's plan starts on row 11 and ends on row 21, how do I read this data from the sheet? 其次,我不确定如何从电子表格中读取某个部分,例如,如果客户的计划从第11行开始并在第21行结束,那么如何从工作表中读取此数据? The thing is in the program we won't actually know what rows the client's plan are stored on, so I know that I will have to use if statements to compare the data to what I am looking for. 问题是在程序中,我们实际上并不知道客户计划存储在哪些行上,因此我知道我将不得不使用if语句将数据与所需内容进行比较。 Once I have read the data from the file, I also want to delete it. 从文件中读取数据后,我也希望将其删除。 Below is an example of what the plan looks like: 以下是该计划的示例:

在此处输入图片说明

Below is the current code I have: 下面是我目前的代码:

if(workoutPlan.isVisible()==true){
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader("Client Workout Plans.csv"));
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            String line = "";
            String cvsSplitBy = ",";
            String fullName = null;
            StringBuilder sb = new StringBuilder();

                br = new BufferedReader(new FileReader("Clients.csv"));
                boolean complete = false;
                while(complete!=true){
                    try {
                        line = br.readLine();
                        String[] data = line.split(cvsSplitBy);
                        String Name = existingClient.getFirst()+" "+existingClient.getLast();
                        int fullNameLength = Name.length(); 
                        if((data.length>0)&&((data[0].substring(0, fullNameLength)).equals(Name))){

                        }
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }

    }

要读取和写入csv文件,您可以使用Commons CSV: http : //commons.apache.org/proper/commons-csv/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM