开发者

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


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


            }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜