如何使用Java Apache POI在Excel工作表中隐藏以下未使用的行?

时间:2021-05-18 20:24:04

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

我正在使用数据库中的数据填充模板Excel工作表:

 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.
  • 例如:如果填充了第1 100个数据行,则应隐藏101之后的行。

Please, help !!

请帮忙 !!

3 个解决方案

#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.

POI会在您创建工作表时识别工作表中的逻辑行数。因此,当您使用100行填充它时,它将创建100条记录。当您使用默认布局在Excel中打开XLS时,其余部分将显示 - 但这些不是POI记录。

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


            }

#2


5  

Row r = sheet.getRow(indexRow);
if ( r!=null ) {
    r.setZeroHeight(true);
}

#3


3  

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

poi-3.8-beta4中存在row.setRowStye()和row.getRowStye()。

#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.

POI会在您创建工作表时识别工作表中的逻辑行数。因此,当您使用100行填充它时,它将创建100条记录。当您使用默认布局在Excel中打开XLS时,其余部分将显示 - 但这些不是POI记录。

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


            }

#2


5  

Row r = sheet.getRow(indexRow);
if ( r!=null ) {
    r.setZeroHeight(true);
}

#3


3  

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

poi-3.8-beta4中存在row.setRowStye()和row.getRowStye()。