尝试读取单元格时奇怪的超出范围异常

时间:2013-02-22 11:45:54

标签: java jxl

我一直在尝试阅读excel表并将数据从单元格插入到程序中。我很成功,但是当我尝试从位置4及以后的单元格中获取数据时,我得到一个超出范围的异常。不知道为什么会这样,因为我可以把位置0,1,2和3称为好。这是我的代码。

public void readExcel(File file) {

try {

//Workbook newBook = new Workbook();
Workbook workbook = Workbook.getWorkbook(file);
WritableWorkbook workbookOutput = Workbook.createWorkbook(new File("output.xls"));
// Workbook workbook = Workbook.getWorkbook(new File(“Res.xls”));
WritableSheet sheetOutput = workbookOutput.createSheet("First Sheet", 0);


sheet = workbook.getSheet(0);
boolean isnotnull = true;
int i = 0;
while(isnotnull){

    //Cell a1 = sheet.getCell(0, 2);
try{
    Cell a2 = sheet.getCell(0, i);
    Cell a3 = sheet.getCell(1, i);
    Cell a4 = sheet.getCell(2, i);
    stringa1 = a2.getContents();
    stringa2 = a3.getContents();
    stringa3 = a4.getContents();

    char c = stringa2.charAt(0);
    char d = stringa2.charAt(1);

    if(Character.isDigit(c) || Character.isDigit(d)){
        System.out.println("found one");
        stringa1 = stringa1 + "" + stringa2;
        stringa2 = stringa3;
        Cell a5 = sheet.getCell(3, i);
        try{
            Cell a10 = sheet.getCell(4, i);
           // System.out.print("Cell" + a6.getContents());
        }
        catch(Exception e){
            System.out.println(e);
        }

        stringa4 = a5.getContents();
        stringa3 = stringa4;
      //  checkCells(1, i);

    }
    Label label = new Label(0, i, stringa1);
    Label label2 = new Label(1, i, stringa2);
    Label label3 = new Label(2, i, stringa3);
    sheetOutput.addCell(label);
    sheetOutput.addCell(label2);
    sheetOutput.addCell(label3);
    i++;
    System.out.println(stringa1 + " |||||||||||| " + stringa2 + " |||||||| " + stringa3 + " " + i);
}catch(Exception e){
    System.out.println(e);
    isnotnull = false;
}

}

workbookOutput.write(); 
try{
workbookOutput.close();
}catch(Exception r){
    System.out.println(r);
}
System.out.println(stringa1);

workbook.close();

} catch (BiffException e) {

e.printStackTrace();

} catch (IOException e) {

   e.printStackTrace();

}

}`enter code here`

1 个答案:

答案 0 :(得分:0)

此单元格在电子表格中根本不存在。似乎createSheet方法只创建了几个第一个单元格。