我正在使用Apache POI从电子表格的第1页填充3个值的HashSet。由于我还需要访问电子表格的表2以获取其他值,我再次迭代它:
public class Students {
private int numStudents;
HashSet<Student> studentsRoster1 = new HashSet<Student>();
HashSet<Student> studentsRoster;
public Students(String studentsDb) {
try {
FileInputStream file = new FileInputStream(new File(studentsDb));
XSSFWorkbook workbook = new XSSFWorkbook(file);
DataFormatter fmt = new DataFormatter();
// Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
String name = null;
String email = null;
int id1 = 0;
String id = null;
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Student student = new Student();
Row row = rowIterator.next();
// Skip the first row
if (row.getRowNum() == 0) {
continue;
}
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
Cell cell0 = row.getCell(0);
Cell cell1 = row.getCell(1);
String formatValue = fmt.formatCellValue(cell1);
Cell cell2 = row.getCell(2);
name = cell0.getStringCellValue();
id1 = (int) cell1.getNumericCellValue();
email = cell2.getStringCellValue();
id = String.valueOf(id1);
student.setName(name);
student.setid(id);
student.setEmail(email);
studentsRoster1.add(student);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**** access second sheet for team info ****/
try {
FileInputStream file = new FileInputStream(new File(studentsDb));
XSSFWorkbook workbook = new XSSFWorkbook(file);
// Get second sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(1);
String team = null;
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Student student = new Student();
Row row = rowIterator.next();
// Skip the first row
if (row.getRowNum() == 0) {
continue;
}
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
Cell cell0 = row.getCell(0);
team = cell0.getStringCellValue();
student.setTeam(team);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.studentsRoster = studentsRoster1;
}
public HashSet<Student> getStudents() {
return studentsRoster;
}
}
正如你所看到的,我正在创建HashSet studentsRoster1;但最后,我需要回归学生名册。
我还没有想出一种方法可以正确地将第4个值student.setTeam(team);
添加到HashSet中。我想创建另一个HashSet并使用union吗?
我还需要从另一个类添加到studentsRoster HashSet,该类正在迭代另一个电子表格以添加另一个值student.setXXX(xxx);
。
我也无法做到。
非常感谢任何建议。
答案 0 :(得分:1)
你可以在一个循环中完成,如下所示:
private static final int NAME_INDEX = 0;
private static final int ID_INDEX = 1;
private static final int EMAIL_INDEX = 2;
private static final int TEAM_INDEX = 0;
private int numStudents;
HashSet<Student> studentsRoster = new HashSet<Student>();
public Students(String studentsDb) {
try {
HashSet<Student> newStudentsRoster = new HashSet<Student>();
FileInputStream file = new FileInputStream(new File(studentsDb));
XSSFWorkbook workbook = new XSSFWorkbook(file);
// Get first sheet from the workbook
XSSFSheet sheet0 = workbook.getSheetAt(0);
// Get second sheet from the workbook
XSSFSheet sheet1 = workbook.getSheetAt(1);
Iterator<Row> rowIterator0 = sheet0.iterator();
Iterator<Row> rowIterator1 = sheet1.iterator();
while (rowIterator0.hasNext() && rowIterator1.hasNext()) {
Row row0 = rowIterator0.next();
Row row1 = rowIterator1.next();
// Skip the first row
if (row0.getRowNum() > 0) {
Student student = new Student();
Iterator<Cell> cellIterator0 = row0.cellIterator();
Iterator<Cell> cellIterator1 = row1.cellIterator();
if (cellIterator0.hasNext()) {
student.setName(row0.getCell(NAME_INDEX).getStringCellValue());
Integer id = row0.getCell(ID_INDEX).getNumericCellValue();
if (id != null){
student.setId(id.toString());
}
student.setEmail(row0.getCell(EMAIL_INDEX).getStringCellValue());
}
if (cellIterator1.hasNext()) {
student.setTeam(row1.getCell(TEAM_INDEX).getStringCellValue());
}
newStudentsRoster.add(student);
}
}
numStudents = newStudentsRoster.size();
studentsRoster = newStudentsRoster;
} catch (FileNotFoundException e) {
e.printStackTrace(); // <- this hides the errors, you must avoid it
} catch (IOException e) {
e.printStackTrace(); // <- this hides the errors, you must avoid it
}
}