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 !!
Row r = sheet.getRow(indexRow);
if ( r!=null ) {
r.setZeroHeight(true);
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With