2

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

 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();
            }
6
  • 2
    try put the close call only outside the for loop Commented Jul 18, 2014 at 14:04
  • @I-LOVE-2-REVIVE i did what you said .. now it iterates completely but doesn't write anything apart for first value of array fields Commented Jul 18, 2014 at 14:07
  • can you show some more code. What does output contain? Commented Jul 18, 2014 at 14:11
  • @I-LOVE-2-REVIVE output of loop is this postimg.org/image/c1m0yy96x output of excel file is this postimg.org/image/5o2d0v2l3 Commented Jul 18, 2014 at 14:19
  • 2
    You've got 3 different CellNames declared - you might want to do a little refactoring to make the code easier for people to understand. Commented Jul 18, 2014 at 15:30

1 Answer 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:

    // 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.

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

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() );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.