在jxl.read.biff.SheetImpl.getCell上获取java.lang.ArrayIndexOutOfBoundsException(SheetImpl.java:356)

时间:2013-08-16 12:32:35

标签: java jexcelapi

import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class SpreadsheetRead {
    /**
     * @param args
     * @throws IOException 
     * @throws BiffException 
     */
    public static void main(String[] args) throws BiffException, IOException {
        // TODO Auto-generated method stub
        Workbook workbook = Workbook.getWorkbook(new File("Book1.xls"));
        Sheet sheet = workbook.getSheet(0);

        Cell name = sheet.getCell(0, 0);
        Cell name1 = sheet.getCell(1, 0);
        try {
            Cell name2 = sheet.getCell(2, 0);
            Cell name3 = sheet.getCell(3, 0);
        }catch (Exception e){
            e.printStackTrace();
        }

        Cell value = sheet.getCell(0, 1);
        Cell value1 = sheet.getCell(1, 1);
        Cell value2 = sheet.getCell(2, 1);
        Cell value3 = sheet.getCell(3, 1);

        System.out.println(sheet.getRows());
        System.out.println(sheet.getColumns());
        System.out.println(name.getContents());
        System.out.println(name1.getContents());
        System.out.println(value.getContents());
        System.out.println(value1.getContents());
    }
}

Excel表格内容(Book1.xls)...它包含4行和2列,如下所述。

Name Value
A    1
B    2
C    3

此代码适用于单元格[(0,0),(0,1),(1,0)& (1,1)]和其余的单元格给出ArrayIndexOutOfBoundException ...请帮助

堆栈跟踪

java.lang.ArrayIndexOutOfBoundsException: 2
4
2
Name
Value
A
    at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
    at SpreadsheetRead.main(SpreadsheetRead.java:25)
1

1 个答案:

答案 0 :(得分:0)

控制台输出略微纠结;记录到System.err的数据与记录到System.out的数据重叠:

java.lang.ArrayIndexOutOfBoundsException: 2
4
2
Name
Value
A
    at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
    at SpreadsheetRead.main(SpreadsheetRead.java:25)
1

让我们解开这些问题,并将它们与相应的日志记录调用相匹配:

错误日志:

java.lang.ArrayIndexOutOfBoundsException: 2
    at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
    at SpreadsheetRead.main(SpreadsheetRead.java:25)
  

第25行是我尝试访问Cell(2,0)

的地方

这表明Cell(2,0)不在表中。

让我们看一下System.out日志。

  

他们也失败了,因为我在执行此代码时对这些行进行了评论。

我认为您的意思是访问第1列和相应的日志记录调用。

Cell name = sheet.getCell(0, 0);
Cell name1 = sheet.getCell(1, 0);
Cell value = sheet.getCell(0, 1);
Cell value1 = sheet.getCell(1, 1);

System.out.println(sheet.getRows());      //4
System.out.println(sheet.getColumns());   //2
System.out.println(name.getContents());   //Name
System.out.println(name1.getContents());  //Value
System.out.println(value.getContents());  //A
System.out.println(value1.getContents()); //1

将此与表格进行比较:

Name Value
A    1
B    2
C    3

表示第一个参数是列索引,而不是您期望的行索引。

the Sheet#getCell documentation证实了这一点:

public Cell getCell(int column,
                    int row)