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

我使用数据库中的数据填充模板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 ? 

目标达成:

  • 我希望填充的行之后的未使用的行应该隐藏?
  • 所有填充的行必须可见。
  • 例如:如果第一个100个数据行被填充,那么101和之后的行应该被隐藏。

请帮忙 !!

POI可以识别您的工作表中创build逻辑行的次数。 所以当你用100行填充它时,它将创build100条logging。 剩下的部分将在Excel中使用默认布局打开XLS时显示 – 但这些不是POIlogging。

您必须在最后一个数据logging之后创build一些虚拟行

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

创build一个单元格样式,并将其设置为隐藏

 CellStyle hiddenstyle = workBook.createCellStyle(); hiddenstyle.setHidden(true); 

从最后一行到表尾的所有行设置此样式

 while (rows.hasNext()){ Row row1 = rows.next (); row1.setRowStyle(hiddenstyle); } 

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

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