简体   繁体   English

如何读取 JXL Excel 工作表的行和列

[英]How to read JXL Excel Sheet Rows and columns

I have iterated rows and columns from reading excel but show Arrayindexoutofbound exception.我从读取 excel 中迭代了行和列,但显示Arrayindexoutofbound异常。 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)

Excel Image Excel 图像在此处输入图片说明

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存储在一个变量中,则会更容易阅读,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM