如何从Java中读取excel中的特定行?

时间:2017-07-25 18:52:46

标签: java excel csv filereader text-parsing

我正在尝试编写一个程序,允许我检索存储在excel文件中的人员的详细信息。我决定使用电子邮件作为识别每个人的一种方式,因为这些是独一无二的。我的程序似乎不起作用,有人可以帮助我吗?

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Reader {

    public static void main(String[] args) {

        String csvFile = "Clients.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        try {

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {
                if(((br.readLine().split(cvsSplitBy))[2]).equals("email@gmail.com")){
                    String[] data = line.split(cvsSplitBy);

                    System.out.println("First Name: "+data[0]+" Last Name: "+data[1]+" Activity Level: "+data[7]);

                }

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

1 个答案:

答案 0 :(得分:1)

以下是修正我问题的更正代码:

        String csvFile = "Clients.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        try {

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {
                String[] data = line.split(cvsSplitBy);
                if(data[2].equals("samuelfairbrass@icloud.com")){


                    System.out.println("First Name: "+data[0]+" Last Name: "+data[1]+" Email: "+data[2]+" Phone Number: "+data[3]+" Activity Level: "+data[7]);

                }


            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }