【问题标题】:How to read a excel CSV file (comma separated value) in Java Eclipse?如何在 Java Eclipse 中读取 excel CSV 文件(逗号分隔值)?
【发布时间】:2016-01-30 00:07:18
【问题描述】:

目标:创建一个面向对象的图形 Java 应用程序,该应用程序将: 读取包含学生姓名(名字、姓氏)、ID 和初始分数的 CSV(逗号分隔值)文件对于内容和交付(对未评估的学生使用 -1 值。

这是我的代码,但是当我单击“选择文件”时。它确实应该打开文件并读取数据时显示“预期的名字、姓氏、ID、内容和交付”。但不知何故它不起作用。 在此处输入图片说明

下面是我的代码:

私有类ChooseFileListener实现ActionListener {

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

我现在已经创建了另一个名为 Section 的类:

包patel.Jainam;

导入 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);
 }

}

【问题讨论】:

  • 注意在所有代码之前..我创建了许多按钮和标签以及所有这些,但我没有包括它,因为它太长了。
  • 您的 csv 文件是否以额外的(空白)行结尾?这将导致抛出异常。
  • 不。它没有。我以前试过,但没有用。
  • 尝试使用 csv 文件阅读器之一,OpenCSVapache-commons-csv,...
  • 谢谢,但我不能使用 OpenCSV,因为当点击“选择文件”按钮时文件应该会自动打开。所以它应该只读取和写入除相应标签之外的所有信息。

标签: java eclipse excel csv


【解决方案1】:

问题是您检查了MAX_COMMAS,但您正在根据字段数进行检查。对于格式正确的文件,该值始终为 5。因此,您要确保访问代码的 else 部分并始终抛出异常。

你想表达的是:

if (studentData.length != MAX_COMMAS)

【讨论】:

    猜你喜欢
    • 2012-11-20
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    相关资源
    最近更新 更多