索引越界异常,apache poi表,java程序

时间:2014-03-03 07:05:15

标签: java apache-poi indexoutofboundsexception

所以我将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);

        }
    }
}

0 个答案:

没有答案