Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java apache poi indexoutofbound异常_Java_Apache Poi - Fatal编程技术网

Java apache poi indexoutofbound异常

Java apache poi indexoutofbound异常,java,apache-poi,Java,Apache Poi,Excel文件(test1.xlsx) NameGenderageSign日期 AliM20 AbuM25 SitiF30 代码 public class ReadExcel { public static ArrayList<String> record; public static void main(String[] args) throws FileNotFoundException, IOException { //---Read file--- FileInput

Excel文件(test1.xlsx)
NameGenderageSign日期
AliM20
AbuM25
SitiF30

  • 代码

    public class ReadExcel {
    
    public static ArrayList<String> record;
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
    
     //---Read file---
    FileInputStream in = new FileInputStream("test1.xlsx");
    XSSFWorkbook workbook = new XSSFWorkbook(in);
    XSSFSheet spreadsheet = workbook.getSheetAt(0);
    XSSFRow row;
    Cell cell;
    
    Iterator<Row> rowIterator = spreadsheet.iterator();
    
    while(rowIterator.hasNext()){
        record = new ArrayList<String>();
        row = (XSSFRow)rowIterator.next();
    
        if(row.getRowNum()==0) {
            continue;
        }
    
         for(int k = 0; k < row.getLastCellNum();k++){
            cell = row.getCell(k, Row.CREATE_NULL_AS_BLANK);
        }
    
        Iterator<Cell> cellIterator = row.cellIterator();
    
        while(cellIterator.hasNext()){
                cell = cellIterator.next();
                cell.setCellType(Cell.CELL_TYPE_STRING); 
    
                switch(cell.getCellType()){
                    case Cell.CELL_TYPE_STRING:
                        record.add(cell.getStringCellValue());
                        System.out.print(cell.getStringCellValue());
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        Double value = cell.getNumericCellValue();
                        Long longValue = value.longValue();
    
                        record.add(Double.toString(cell.getNumericCellValue()));
                        System.out.print(cell.getNumericCellValue());
                        break;
                }
            }   
            System.out.println();
    
            String name = record.get(0);
            String gender = record.get(1);
            String age = record.get(2);
            String dateLeave = record.get(3);  //[ERROR]
    
            System.out.println(name + gender + age + dateLeave);
            }
        }
    }
    

    我犯了什么错误?

    您的代码试图引用集合中只有三个元素的第四个元素:

    record.get(3)
    
    for(int k = 0; k < 4; k++){
        cell = row.getCell(k, Row.CREATE_NULL_AS_BLANK);
    }
    
    因为只有三个元素,所以尝试引用第四个元素会产生错误

    为什么只有三个元素?

    好吧,看看数据:

    Ali     M     20
    Abu     M     25
    Siti    F     30
    
    每行三个元素

    看起来代码正在动态检查最后一个“元素”:


    你调试过这个吗?
    for(int k = 0; k < row.getLastCellNum(); k++){
        cell = row.getCell(k, Row.CREATE_NULL_AS_BLANK);
    }
    
    for(int k = 0; k < 4; k++){
        cell = row.getCell(k, Row.CREATE_NULL_AS_BLANK);
    }