【问题标题】:How to read a particular row from excel in Java?如何从 Java 中的 excel 中读取特定行?
【发布时间】:2017-07-25 18:52:46
【问题描述】:

我正在尝试编写一个程序,该程序允许我检索存储在 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();
                }
            }
        }

    }

}

【问题讨论】:

  • 仅供参考:CSV 文件不是 Excel 文件。你可以用记事本这样简单的东西来阅读 CSV。
  • 请给出你的csv文件的结构...

标签: java excel csv filereader text-parsing


【解决方案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();
                }
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多