我正在用来自数据库的数据填充模板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 ?

要达到的目标:


  • 我希望隐藏已填充行之后的未使用行?
  • 所有填充的行必须可见。
  • 例如:如果填充了第1 100个数据行,则应隐藏101及之后的行。


  • 请帮忙!

    最佳答案

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

    您必须在这样的最后一个数据记录之后创建一些虚拟行

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

    创建一个单元格样式,并将其设置为隐藏
    CellStyle hiddenstyle = workBook.createCellStyle();
    hiddenstyle.setHidden(true);
    

    为从最后一行到工作表结尾的所有行设置此样式
    while (rows.hasNext()){
                    Row row1 = rows.next ();
                    row1.setRowStyle(hiddenstyle);
    
    
                }
    

    08-03 12:16
    查看更多