无法使用for循环在Excel Sheet中写入数据(Apache_poi)

时间:2022-01-31 20:25:45

I am trying to write data into Excel sheet using a for loop. But it only writes on first cell and don't iterate further. I tried a lot but couldn't fix this bug. What is wrong with my cod e. Please help. Here is what i have done so far

我正在尝试使用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();
            }

1 个解决方案

#1


2  

  • Actually, you don't even need to specify cell coordinates as "A2", "B2", etc. You're already defining which cell to fill when providing arguments to the createRow() and createCell() methods. For example:

    实际上,您甚至不需要将单元格坐标指定为“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 );
    
  • Also, to avoid ArrayIndexOutOfBoundsException, use < instead of <= in your loop. Otherwise, fields[9] will try to access the 10th element, while your array has only 9. Don't forget, arrays are also "0 based" (the first element has index 0, not 1). And, you'll better use fields.length() instead of specifying 9 directly. Doing this, you won't have to change your loop's limits when some day you will add/remove some column names in fields[] array.

    另外,要避免ArrayIndexOutOfBoundsException,请在循环中使用 <而不是<=。否则,fields [9]将尝试访问第10个元素,而您的数组只有9个。不要忘记,数组也是“基于0”(第一个元素的索引为0,而不是1)。并且,您最好使用fields.length()而不是直接指定9。这样做,当有一天你将在fields []数组中添加 删除一些列名时,你不必改变你的循环限制。< p>

  • As said in comments, wb.write(output) and output.close() are to be put after the loop, normally at the end of program.

    如注释中所述,wb.write(output)和output.close()将在循环之后放置,通常在程序结束时。

So here is the complete code which will perform your task. Hope it helps!

所以这里是执行任务的完整代码。希望能帮助到你!

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() );
}

#1


2  

  • Actually, you don't even need to specify cell coordinates as "A2", "B2", etc. You're already defining which cell to fill when providing arguments to the createRow() and createCell() methods. For example:

    实际上,您甚至不需要将单元格坐标指定为“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 );
    
  • Also, to avoid ArrayIndexOutOfBoundsException, use < instead of <= in your loop. Otherwise, fields[9] will try to access the 10th element, while your array has only 9. Don't forget, arrays are also "0 based" (the first element has index 0, not 1). And, you'll better use fields.length() instead of specifying 9 directly. Doing this, you won't have to change your loop's limits when some day you will add/remove some column names in fields[] array.

    另外,要避免ArrayIndexOutOfBoundsException,请在循环中使用 <而不是<=。否则,fields [9]将尝试访问第10个元素,而您的数组只有9个。不要忘记,数组也是“基于0”(第一个元素的索引为0,而不是1)。并且,您最好使用fields.length()而不是直接指定9。这样做,当有一天你将在fields []数组中添加 删除一些列名时,你不必改变你的循环限制。< p>

  • As said in comments, wb.write(output) and output.close() are to be put after the loop, normally at the end of program.

    如注释中所述,wb.write(output)和output.close()将在循环之后放置,通常在程序结束时。

So here is the complete code which will perform your task. Hope it helps!

所以这里是执行任务的完整代码。希望能帮助到你!

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() );
}