Apache的POI indexoutofbound例外 [英] apache poi indexoutofbound exception
问题描述
-
Excel文件(test1.xlsx)
Name Gender Age Resign
Ali M 20
Abu M 25
Siti F 30
Excel File (test1.xlsx)
Name Gender Age Resign Date
Ali M 20
Abu M 25
Siti F 30
code
public class ReadExcel {
public static ArrayList<String> record;
public static void main(String[] args) throws FileNotFoundException, IOException {
//---Read file---
FileInputStream in = new FileInputStream("test1.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(in);
XSSFSheet spreadsheet = workbook.getSheetAt(0);
XSSFRow row;
Cell cell;
Iterator<Row> rowIterator = spreadsheet.iterator();
while(rowIterator.hasNext()){
record = new ArrayList<String>();
row = (XSSFRow)rowIterator.next();
if(row.getRowNum()==0) {
continue;
}
for(int k = 0; k < row.getLastCellNum();k++){
cell = row.getCell(k, Row.CREATE_NULL_AS_BLANK);
}
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()){
cell = cellIterator.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
switch(cell.getCellType()){
case Cell.CELL_TYPE_STRING:
record.add(cell.getStringCellValue());
System.out.print(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
Double value = cell.getNumericCellValue();
Long longValue = value.longValue();
record.add(Double.toString(cell.getNumericCellValue()));
System.out.print(cell.getNumericCellValue());
break;
}
}
System.out.println();
String name = record.get(0);
String gender = record.get(1);
String age = record.get(2);
String dateLeave = record.get(3); //[ERROR]
System.out.println(name + gender + age + dateLeave);
}
}
}
不过,从我上面的程序,我得到这个异常:
However, from my above program, I get this exception:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at ibguobform.ReadExcel.main(ReadExcel.java:66)
Java Result: 1
什么是我所犯的错误?
What is the errors that I made?
推荐答案
您code尝试引用一个只有三个要素集合的第四个元素:
Your code is trying to reference the fourth element of a collection that has only three elements:
record.get(3)
由于只有三个要素,试图引用第四个产生错误。
Since there's only three elements, trying to reference the fourth one produces an error.
为什么只有三个元素?
好吧,看数据:
Ali M 20
Abu M 25
Siti F 30
每行三要素。
这似乎是发生的是code动态检查最后的元素:
What appears to be happening is the code is dynamically checking for the last "element":
for(int k = 0; k < row.getLastCellNum(); k++){
cell = row.getCell(k, Row.CREATE_NULL_AS_BLANK);
}
这似乎是 row.getLastCellNum()
讲的是code,有只有三个单元。 (因为,只有三个单元的数据在其中。)如果第四个单元是有效的,即使没有数据,明确指出在code。通过使用始终四个要素:
It would seem that row.getLastCellNum()
is telling the code that there are only three cells. (Because, well, there are only three cells with data in them.) If the fourth cell is valid even when there's no data, explicitly note that in the code by always using four elements:
for(int k = 0; k < 4; k++){
cell = row.getCell(k, Row.CREATE_NULL_AS_BLANK);
}
这篇关于Apache的POI indexoutofbound例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!