Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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 Can';t使用for循环(Apache_poi)在Excel工作表中写入数据_Java_Excel_Swing_Apache_Apache Poi - Fatal编程技术网

Java Can';t使用for循环(Apache_poi)在Excel工作表中写入数据

Java Can';t使用for循环(Apache_poi)在Excel工作表中写入数据,java,excel,swing,apache,apache-poi,Java,Excel,Swing,Apache,Apache Poi,我正在尝试使用for循环将数据写入Excel工作表。但它只在第一个单元格上写入,不进一步迭代。我试了很多,但都没能解决这个问题。我的鳕鱼怎么了。请帮忙。 以下是我迄今为止所做的工作 HSSFRow row1 = sheet.createRow((short) 1); String[] cellname = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I"}; String[] fie

我正在尝试使用for循环将数据写入Excel工作表。但它只在第一个单元格上写入,不进一步迭代。我试了很多,但都没能解决这个问题。我的鳕鱼怎么了。请帮忙。 以下是我迄今为止所做的工作

 HSSFRow row1 = sheet.createRow((short) 1);
            String[] cellname = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I"};
            String[] fields = new String[]{"Student's Name", "Father's Name", "Mother's Name", "Address", "Phone No", "Date Of Birth", "Roll NO", "Class", "subjectMajor"};
            for (int i = 0; i <= 9; i++) {
                String Cellname = cellname[i] + 2;
                System.out.print(Cellname);
                HSSFCell CellName = row1.createCell((short) i);
                CellName.setCellValue(fields[i]);
                wb.write(output);
                output.close();
            }
HSSFRow行1=sheet.createRow((短)1);
字符串[]cellname=新字符串[]{“A”、“B”、“C”、“D”、“E”、“F”、“G”、“H”、“I”};
字符串[]字段=新字符串[]{“学生姓名”、“父亲姓名”、“母亲姓名”、“地址”、“电话号码”、“出生日期”、“卷号”、“班级”、“主修科目”};
对于(int i=0;i
  • 实际上,您甚至不需要将单元格坐标指定为“A2”、“B2”等。在为
    createRow()
    createCell()方法提供参数时,您已经定义了要填充的单元格。例如:

    // creating an HSSFCell at "D9" and setting its value
    // "D" column has index 3; row number 9 has index 8 (starting index is 0)
    HSSFRow row = sheet.createRow( 8 );
    HSSFCell cell = row.createCell( 3 );
    cell.setCellValue( 1341.873 );
    

  • 另外,为了避免出现
    ArrayIndexOutOfBoundsException
    ,请使用
    尝试将close调用仅放在for的外部loop@I-LOVE-2-Resove我照你说的做了。现在它完全迭代,但没有为数组字段的第一个值写任何东西扫描你显示更多的代码。输出包含什么?@i-LOVE-2-Resove循环的输出是这样的吗excel文件的输出是这样的:您已经声明了3个不同的单元名-您可能需要进行一些重构,以使代码更易于理解。
    
    String[] fields = new String[] { "Student's Name", "Father's Name", "Mother's Name", "Address", "Phone No", "Date Of Birth", "Roll NO", "Class", "subjectMajor" };
    
    // Row with index 1 is the second row in Excel sheet
    HSSFRow row1 = sheet1.createRow( 1 );
    
    // Filling the row with the given data, one element per cell, 
    // starting from the "A" column (cellIndex = 0).
    for ( int cellIndex = 0; cellIndex < fields.length; cellIndex++ ) {
        HSSFCell cell = row1.createCell( cellIndex );
        cell.setCellValue( fields[cellIndex] );
    }
    
    // Writing a workbook to the disk
    try {
        FileOutputStream fileOut = new FileOutputStream( "students.xls" );
        wb.write( fileOut );
        fileOut.close();
    } catch ( IOException e ) {
        System.out.println( "IOException:" );
        System.out.println( e.getMessage() );
    }