I have iterated rows and columns from reading excel but show Arrayindexoutofbound
exception. I have given my code and error below
Code :
for (int i = 0; i <= wb.getSheet(0).getRows(); i++) {
for (int j = 0; j <= wb.getSheet(0).getColumns(); j++) {
String testData = wb.getSheet(0).getCell(i, j).getContents();
System.out.println(testData);
}
}
Stackrace
User id
en0063
en0070
Psw
en0063
THEROCKZ1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
at ExcelRead.main(ExcelRead.java:29)
You are exceeding the number of rows and columns due to you for statments - change <=
to <
for (int i = 0; i < wb.getSheet(0).getRows(); i++) {
for (int j = 0; j < wb.getSheet(0).getColumns(); j++) {
String testData = wb.getSheet(0).getCell(i, j).getContents();
System.out.println(testData);
}
}
By the way it would be a lot easier to read if you stored you sheet
in a variable such as
Sheet firstSheet = wb.getSheet(0);
and re-use it as
for (int i = 0; i < firstSheet.getRows(); i++) {
// etc
edit
I suggest that you print out the number of rows and cells, as it seems that the library you are using seems to include blank ones as well. Maybe you need to test for that.
Consider that you could have something like
X X X
X X X
X X X X
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.