【问题标题】:index out of bounds exception, apache poi sheets, java program索引越界异常,apache poi 表,java 程序
【发布时间】:2014-04-04 03:21:27
【问题描述】:

所以我将 2 个数组列表引用到一个函数,以便可以将元素写入 excel 工作表:

sheetCounter 比列表索引大 1

public static void writeToSheet(List<String> name, List<Double> salary, int sheetCounter){

        Workbook wb=new XSSFWorkbook();
        List<Sheet> outputSheets=new ArrayList<>(10);
        outputSheets.add(wb.createSheet("sheet"+sheetCounter));
        for(int i=0;i<salary.size();i++){
            **Row row=outputSheets.get(sheetCounter-1).createRow(i);**

            Cell nameCell=row.createCell(0);
            nameCell.setCellValue(name.get(i));

            Cell salaryCell=row.createCell(1);
            salaryCell.setCellValue(salary.get(i));
        }

        try{ 
            FileOutputStream out=new FileOutputStream("newStackOverflow.xlsx");
            wb.write(out);
            out.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

但是,我在星号线上遇到了错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at additives.StackOverflow.writeToSheet(StackOverflow.java:31)
    at additives.StackOverflow.main(StackOverflow.java:91)

我该如何解决这个问题?

这里是main(...)和readWorkbook(),一切主要发生在嵌套的for循环中:

public static Workbook readWorkbook(){

        Workbook wb=null;
        try {
            wb = WorkbookFactory.create(new File("stackOverflow.xlsx"));
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }


public static void main(String[] args){

        Workbook inputWb=readWorkbook();
        Sheet inputWs=inputWb.getSheet("sheet1");

        List<String> name=new ArrayList<>();
        List<Double> salary=new ArrayList<>();

        int rowIndex=inputWs.getLastRowNum()+1;

        int sheetCounter=0;

        for(int i=1; i<rowIndex-1; i++){
            Row outerRow=inputWs.getRow(i);
            Row innerRow=null; 
            Cell outerCell=outerRow.getCell(0);
            Cell innerCell=null;


            int j=0;
            for(j=i+1;j<rowIndex;j++){

                innerRow=inputWs.getRow(j);
                innerCell=innerRow.getCell(0);

                if(outerCell.getStringCellValue().equals(innerCell.getStringCellValue())){
                    name.add(innerRow.getCell(0).getStringCellValue());
                    salary.add(innerRow.getCell(1).getNumericCellValue());
                }


                if(!outerCell.getStringCellValue().equals(innerCell.getStringCellValue())){
                    sheetCounter++;
                    break;
                }


            }
            System.out.println("the sheet no is="+sheetCounter);
            name.add(outerRow.getCell(0).getStringCellValue());
            salary.add(outerRow.getCell(1).getNumericCellValue());
            System.out.println(name+" "+salary);
            writeToSheet(name,salary,sheetCounter);

            i=j;
            name.add(outerRow.getCell(0).getStringCellValue());
            salary.add(outerRow.getCell(1).getNumericCellValue());
            System.out.println(i);
            name.clear();
            salary.clear();
            System.out.println(name);

        }
    }
}

【问题讨论】:

  • ArrayList 中只有一个元素时,不要请求第二个元素。
  • 但我要求第一个元素,因此 sheetCounter-1
  • 这似乎给了你1的值。数组(和ArrayList's)是基于 0 的。
  • 在for里面我什至做了listCounter=sheetCounter--;行 row=outputSheets.get(listCounter).createRow(i);当我输入函数时 sheetCounter 的值为 1,为什么会发生这种情况?
  • 你为变量 sheetCounter 传递了什么值??

标签: java apache-poi indexoutofboundsexception


【解决方案1】:

我遇到了与此异常类似的问题,解决的是我的 excel 文件的格式。我定义了“表”并为其命名。

【讨论】:

    猜你喜欢
    • 2018-11-19
    • 2013-03-30
    • 2013-03-05
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多