Cassie
Cassie

Reputation: 39

How to remove iterator loop and allow cellDateType to work in xlsx java?

Hi to all experts out there, Currently I have some problem regarding the iterator loop. I need to remove it in order for my data to appear on my xlsx excel sheet but I am not sure how do I go about removing it such that my codes are error free. And I suspect that the error may be on the iterator loop. For now, this is my codes and an image link of how it looks like now.

How it looks now

The data in the excel sheet are not suppose to have a space in between but apparently, there is. There isn't any data in the first column because I didn't have any data being keyed in into the website. So it's okay for the first column to be blank.

      int r = 3; 
      for (Iterator iter = Cells.iterator();iter.hasNext();) {
           Object[]  _o = (Object[]) iter.next();
           currentRow = s.createRow(r);
      for(int colNum = 0; colNum < _col_cnt; colNum++){
         XSSFCell currentCell =currentRow.createCell(colNum);       

                if (CellDataType[c].equals("STRING")
                        || CellDataType[c].equals("VARCHAR")) {
                    String _l = (String) _o[colNum];
                    if (_l != null) {
                        currentCell.setCellValue(_l);
                        System.out.println("Data: " + _l);
                    }       
                }

hardcode (Testing):

 int r = 3; 
            for (Iterator iter = Cells.iterator();iter.hasNext();) {
                Object[]  _o = (Object[]) iter.next();
                currentRow = s.createRow(r);
            for(int colNum = 0; colNum < _col_cnt; colNum++){
                XSSFCell currentCell =currentRow.createCell(colNum);       


                currentCell.setCellValue("Hello"); 

hardcode

Upvotes: 1

Views: 1090

Answers (1)

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79877

Your problem is not in the code you've shown here, but it's in the code that you showed in your earlier question. Two words of advice.

  • If you used indentation correctly, and lined up the curly braces where they're supposed to be, you would see the error almost immediately.
  • If you stepped through your code with the debugger, and looked at the value of r, you would have found the problem immediately.

Your line r++; is inside the inner loop. This means it gets incremented 3 times for each iteration of the outer loop. You need to move r++; down one line, so that it's outside the inner loop, but inside the outer loop. That way, it will get incremented just once per row, which is what you need.

Upvotes: 1

Related Questions