Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Apache POI

Options
  • 06-12-2013 11:39am
    #1
    Registered Users Posts: 3,500 ✭✭✭


    Hi im trying to write data to an excel file. Im having a small bit of trouble with my loop.

    Basically I want to write my first lot of data to the first column row by row, then move onto the next column and write the next bit of data row by row. My problem is that only the last column seems to be written. I think the problem might be this line.
    Row row = sheet.createRow(rowNumber++);
    

    That I am creating a new row and its replacing the row I have written.



    Here is the code anyways. Any help would be great
    //Blank workbook
            XSSFWorkbook workbook = new XSSFWorkbook();
            
          //Create a blank sheet
            XSSFSheet sheet = workbook.createSheet("Base compare");
        
            int rowNumber = 1;
            int colNumber = 0;
            
           for(int k=0; k<2; k++){
            for(int j=0; j<results.size(); j++){
            	Row row = sheet.createRow(rowNumber++);
            	Cell cell = row.createCell(colNumber);
            	if(colNumber == 0){
            	cell.setCellValue(results.get(j).getSuiteName());
            	}else if(colNumber == 1){
            		cell.setCellValue(results.get(j).getTestsDifference());
            	}
            	
            }
            rowNumber = 1;
            colNumber++;
           }
    


Comments

  • Registered Users Posts: 11,264 ✭✭✭✭jester77


    Why are you using 2 loops?

    Wouldn't 1 loop work for you:
        for(int j=0; j < results.size(); j++) {
            Row row = sheet.createRow(++rowNumber);
            Cell cell0 = row.createCell(0);
            Cell cell1 = row.createCell(1);
    
            cell0.setCellValue(results.get(j).getSuiteName());
            cell1.setCellValue(results.get(j).getTestsDifference());
        }        	
    }
    


    Edit:
    And the reason you are only getting 1 row is because you reset rowNumber back to 1, so it will always overwrite the last row at 1.


  • Registered Users Posts: 1,144 ✭✭✭Ballyv24


    I think the issue is because you are creating a new row every time.

    After you have the first column populated (and each row created), in the next loop, all new rows are being created and column 2 is being populated for each row and I'm guessing that this is overwriting the rows with the column 1 data..

    This would explain why you only have the last column of data at the end


  • Registered Users Posts: 3,500 ✭✭✭Drexel


    jester77 wrote: »
    Why are you using 2 loops?

    Wouldn't 1 loop work for you:
        for(int j=0; j < results.size(); j++) {
            Row row = sheet.createRow(++rowNumber);
            Cell cell0 = row.createCell(0);
            Cell cell1 = row.createCell(1);
    
            cell0.setCellValue(results.get(j).getSuiteName());
            cell1.setCellValue(results.get(j).getTestsDifference());
        }        	
    }
    


    Edit:
    And the reason you are only getting 1 row is because you reset rowNumber back to 1, so it will always overwrite the last row at 1.

    Why am I using two loops? I dont know haha. Any example I looked at was using nested loops. I was resetting row number so that when one row was finished, it went back to the top, moved column and started again.

    Your code worked a treat so thanks a bunch for that. Having a brain fart I think :)


  • Registered Users Posts: 3,500 ✭✭✭Drexel


    Ballyv24 wrote: »
    I think the issue is because you are creating a new row every time.

    After you have the first column populated (and each row created), in the next loop, all new rows are being created and column 2 is being populated for each row and I'm guessing that this is overwriting the rows with the column 1 data..

    This would explain why you only have the last column of data at the end

    Ya thats what I was thinking. I got it sorted from the reply above. I think ive been misunderstanding rows and columns.Once a row is created, that row will be there for each new column.


Advertisement