java.util.NoSuchElementException-在XSSFSheet上读取
我正在努力克服读取xlsx文件的错误。我正在使用poi 3.9-2012。 我的构造函数和其他方法代码是:java.util.NoSuchElementException-在XSSFSheet上读取,java,apache-poi,readfile,Java,Apache Poi,Readfile,我正在努力克服读取xlsx文件的错误。我正在使用poi 3.9-2012。 我的构造函数和其他方法代码是: public students(String studentsDb){ this.location = studentsDb; this.location = studentsDb; studentInfoDB = new HashSet<Student>(); DBSetUp(location); } pri
public students(String studentsDb){
this.location = studentsDb;
this.location = studentsDb;
studentInfoDB = new HashSet<Student>();
DBSetUp(location);
}
private void DBSetUp(String location) {
try {
this.location = location;
FileInputStream file = new FileInputStream(new File(location));
workbook = new XSSFWorkbook(file);
setUpStudent();
setUpTeam();
} catch (FileNotFoundException e) {
System.out.println("Failed To Find The File!");
} catch (IOException e) {
System.out.println("Failed To Create Workbook!");
}
}
private void setUpTeam() {
XSSFSheet sheet = workbook.getSheetAt(1);
Iterator<Row> rowIterator = sheet.iterator();
rowIterator.next();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
Cell cell = cellIterator.next();
String tempTeam = cell.getStringCellValue();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
String tempName = cell.getStringCellValue();
for (Student s : studentInfoDB) {
if (s.getName().equals(tempName)) {
s.setTeam(tempTeam);
}
}
}
}
}
公立学生(字符串学生SDB){
this.location=studentsDb;
this.location=studentsDb;
studentInfo=new HashSet();
数据库设置(位置);
}
私有void数据库设置(字符串位置){
试一试{
这个位置=位置;
FileInputStream文件=新FileInputStream(新文件(位置));
工作簿=新XSSF工作簿(文件);
设置学生();
设置团队();
}catch(filenotfounde异常){
System.out.println(“找不到文件!”);
}捕获(IOE异常){
System.out.println(“创建工作簿失败!”);
}
}
私人团队(){
XSSFSheet sheet=workbook.getSheetAt(1);
迭代器rowIterator=sheet.Iterator();
roweiterator.next();
while(roweiterator.hasNext()){
行=行迭代器。下一步();
迭代器cellIterator=row.cellIterator();
Cell=cellIterator.next();
String testeam=cell.getStringCellValue();
while(cellIterator.hasNext()){
cell=cellIterator.next();
字符串tempName=cell.getStringCellValue();
用于(学生s:StudentInfo数据库){
如果(s.getName().equals(tempName)){
s、 塞塔姆;
}
}
}
}
}
当我打电话给构造器的学生(位置)。EclipseJava总是生成以下错误消息
位于的java.util.NoSuchElementException
java.util.TreeMap$PrivateEntryIterator.nextEntry(unknon-source)位于
java.util.TreeMap$ValueIterator.next(未知源代码)位于
Students.setUpTeam(Students.java:77)位于
DBSetUp(Students.java:56)在
学生们。(学生们,java:39)
每当枚举中没有下一个元素时,都会抛出NoTouchElementException,因此您调用的第一个rowIterator.next()就是问题所在。我认为这是因为xsl文档中只有一张工作表,所以您应该得到第一张工作表,它是getSheetAt(0)而不是one。谢谢您的回答。我已经改变了。它正在工作。然而。当我在excel中添加更多工作表并添加名为setUpStudent(1)的附加方法时。收到了相同的错误。有什么问题吗?