gpt4 book ai didi

java - 如何用Java读取Excel中的特定行?

转载 作者:行者123 更新时间:2023-12-02 12:30:08 26 4
gpt4 key购买 nike

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

}

}

最佳答案

这是我修正后的代码,解决了我的问题:

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

关于java - 如何用Java读取Excel中的特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45311429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com