Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide the following Un-used rows in Excel sheet using Java Apache POI?

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 !!

like image 462
Yugal Jindle Avatar asked Dec 16 '22 11:12

Yugal Jindle


2 Answers

Row r = sheet.getRow(indexRow);
if ( r!=null ) {
    r.setZeroHeight(true);
}
like image 182
jezz Avatar answered Dec 28 '22 08:12

jezz


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


            }
like image 32
JoseK Avatar answered Dec 28 '22 09:12

JoseK