4

I am populating a template Excel sheet, with the data from the database :

 for (Map<String, Object> resultRow : dbResults) {
        if ((currentRow = sheet.getRow(currentDataRow)) == null) {
            currentRow = sheet.createRow(currentDataRow);   // Creates a new row.
        }
        //currentRow.getRowStyle().setHidden(false);

        for (int i = 0; i < resultColumns; i++) {
            currentCell = currentRow.getCell(i, Row.CREATE_NULL_AS_BLANK);
            setCellValue(currentCell, resultRow.get(dbcolumnNames[i]));
        }
        currentDataRow += 1;
    }

// How to hide all empty/Un-used rows following currentDataRow ?

Aim to Achieve :

  • I want that the Un-Used rows following the populated rows should be hidden ?
  • All Populated rows must be visible.
  • Eg: If 1st 100 data rows are filled, then rows following 101 and onward should be hidden.

Please, help !!

Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197

3 Answers3

6
Row r = sheet.getRow(indexRow);
if ( r!=null ) {
    r.setZeroHeight(true);
}
jezz
  • 61
  • 1
  • 1
3

POI recognizes the number of logical rows in your sheet when you create it. So as you populate it with 100 rows, it will create 100 records. The rest will appear when you open the XLS in Excel with the default layout - but these are not POI records.

You'll have to create some dummy rows after your last data record like this

for (int i=currentDataRow ;i<65000;i++)
                sheet.createRow(i);

Create a cell style, and set it to hidden

CellStyle hiddenstyle = workBook.createCellStyle();
hiddenstyle.setHidden(true);

Set this style for all rows from your last row to end of sheet

while (rows.hasNext()){
                Row row1 = rows.next ();
                row1.setRowStyle(hiddenstyle);


            }
JoseK
  • 31,141
  • 14
  • 104
  • 131
  • I will try your answer and then later will let you know about the results. – Yugal Jindle Aug 04 '11 at 09:52
  • I don't know pal whats happening.. `row.setRowStye() method not found`. Similary, row.getRowStyle() method not found.. Actually, I already had a solution but its not working. I don't know why - When its given in the API docs, but not working when using in my program. God Knows ! – Yugal Jindle Aug 07 '11 at 04:07
  • Yap.. I am using the latest 3.7.. Its weird ! – Yugal Jindle Aug 07 '11 at 05:19
3

row.setRowStye() and row.getRowStye() are present in poi-3.8-beta4.

Draken
  • 3,134
  • 13
  • 34
  • 54
mahmudaq
  • 56
  • 2