如何在JavaEclipse中读取excel CSV文件(逗号分隔值)?
目标:创建一个面向对象的图形Java应用程序,该程序将:读取一个CSV(逗号分隔值)文件,该文件由学生姓名(名字、姓氏)、ID以及内容和交付的初始标记组成(对于未经评估的学生,使用-1值) 这是我的代码,但当我点击选择文件时,它会显示“预期的名字、姓氏、ID、内容和传递”,当它真的应该打开文件并读取数据时。但不知为什么它不起作用。 在此处输入图像描述 以下是我的代码: 私有类ChooseFileListener实现ActionListener{如何在JavaEclipse中读取excel CSV文件(逗号分隔值)?,java,eclipse,excel,csv,Java,Eclipse,Excel,Csv,目标:创建一个面向对象的图形Java应用程序,该程序将:读取一个CSV(逗号分隔值)文件,该文件由学生姓名(名字、姓氏)、ID以及内容和交付的初始标记组成(对于未经评估的学生,使用-1值) 这是我的代码,但当我点击选择文件时,它会显示“预期的名字、姓氏、ID、内容和传递”,当它真的应该打开文件并读取数据时。但不知为什么它不起作用。 在此处输入图像描述 以下是我的代码: 私有类ChooseFileListener实现ActionListener{ public void actionPerform
public void actionPerformed(ActionEvent event) {
Section<Student> section;
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"CSV file", "csv");
fileChooser.setFileFilter(filter);
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
if (selectedFile != null) {
section = createNewSection(selectedFile);
loadStudents(section, selectedFile);
System.out.print(selectedFile);
}
}
}
}
public void RandomStudent(){
ArrayList<Student> students = new ArrayList<>();
max = (students.size()-1);
int x = (int) Math.random()* students.size();
System.out.println(x);
}
private Section<Student> createNewSection(File selectedFile) {
String filename = selectedFile.getName();
Section<Student> section = new Section<Student>(filename);
return section;
}
private void loadStudents(Section<Student> section, File selectedFile) {
Scanner in;
int MAX_COMMAS = 5;
try {
in = new Scanner(selectedFile);
String line = "";
String[] studentData;
in.nextLine();
while (in.hasNext()) {
line = in.nextLine();
studentData = line.split(",");
if (studentData.length == MAX_COMMAS) {
section.addStudent(new Student(studentData[0],studentData[1],
Integer.valueOf(studentData[2]),
Integer.valueOf(studentData[3]),
Integer.valueOf(studentData[4])));
} else {
throw new Exception(
"Invalid file format. \nExcepted: firstname, lastname, id, content, delivery");
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}
public void actionPerformed(ActionEvent事件){
部分;
JFileChooser fileChooser=新的JFileChooser();
FileNameExtensionFilter=新FileNameExtensionFilter(
“CSV文件”、“CSV”);
setFileFilter(过滤器);
int returnValue=fileChooser.showOpenDialog(null);
if(returnValue==JFileChooser.APPROVE\u选项){
File selectedFile=fileChooser.getSelectedFile();
如果(selectedFile!=null){
section=createNewSection(selectedFile);
加载学生(部分,所选文件);
系统输出打印(选择文件);
}
}
}
}
公立学校学生(){
ArrayList students=新ArrayList();
max=(students.size()-1);
int x=(int)Math.random()*students.size();
系统输出println(x);
}
私有部分createNewSection(文件选择文件){
字符串文件名=selectedFile.getName();
节=新节(文件名);
返回段;
}
私人void加载学生(部分,文件选择文件){
扫描仪输入;
int MAX_逗号=5;
试一试{
in=新扫描仪(已选择文件);
字符串行=”;
字符串[]studentData;
in.nextLine();
while(在.hasNext()中){
line=in.nextLine();
studentData=line.split(“,”);
if(studentData.length==最大逗号){
节addStudent(新学生(studentData[0],studentData[1],
Integer.valueOf(studentData[2]),
Integer.valueOf(studentData[3]),
Integer.valueOf(studentData[4]);
}否则{
抛出新异常(
无效的文件格式。\n接受:firstname、lastname、id、content、delivery);
}
}
}捕获(例外e){
showMessageDialog(null,e.getMessage());
}
}
}
我现在创建了另一个名为Section的类:
杰南帕特尔包装
导入java.util.ArrayList
公共课组{
private String data;
private String FirstName;
private String LastName;
private int studentID;
private int ContentMark;
private int DeliveryMark;
private ArrayList<Student> students = new ArrayList<>();
public void FileData(String First, String Last, int ID, int Content, int Delivery){
this.FirstName = First;
this.LastName = Last;
this.studentID = ID;
this.ContentMark = Content;
this.DeliveryMark = Delivery;
}
public String setFirst(String First){
return First;
}
public String setLast (String Last){
return Last;
}
public int setstudentID (int ID) {
return ID;
}
public int setContentMark (int Content) {
return Content;
}
public int setDeliveryMark (int Delivery) {
return Delivery;
}
public Section(String data) {
this.data = data;
}
public void addStudent(Student student) {
students.add(student);
}
私有字符串数据;
私有字符串名;
私有字符串LastName;
私立国际学生;
私有int-ContentMark;
私人邮递标志;
private ArrayList students=新建ArrayList();
public void FileData(字符串第一,字符串最后,int-ID,int-Content,int-Delivery){
this.FirstName=First;
this.LastName=Last;
this.studentID=ID;
this.ContentMark=内容;
this.DeliveryMark=交货;
}
公共字符串setFirst(字符串优先){
先返回;
}
公共字符串setLast(字符串Last){
最后返回;
}
公共int setstudentID(int ID){
返回ID;
}
公共int设置内容标记(int内容){
返回内容;
}
公共整数设置交付标记(整数交付){
退货;
}
公共部分(字符串数据){
这个数据=数据;
}
公立学校学生(学生){
学生。添加(学生);
}
}问题是您检查了
最大逗号,但您是根据字段数进行检查的。对于格式正确的文件,此值始终为5。因此,您要确保转到代码的其他部分并始终抛出
您想要表达的是:
if (studentData.length != MAX_COMMAS)
在所有代码之前请注意..我创建了许多按钮和标签以及所有这些,但是我没有包括它,因为它太长了。您的csv文件是否以一个额外的(空白)结尾行?这会导致引发异常。不,不会。我以前尝试过,但不起作用。尝试使用csv文件读取器,,…谢谢,但我不能使用OpenCSV,因为文件在单击“选择文件”时会自动打开按钮。所以它应该只读写除相应标签之外的所有信息。