本文介紹了如何使用Java ApachePOI在Excel表格中隱藏以下未使用的行?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在用數(shù)據(jù)庫中的數(shù)據(jù)填充一個模板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個數(shù)據(jù)行,則應(yīng)該隱藏101之后的行。
請幫幫忙!!
推薦答案
當您創(chuàng)建工作表時,它可以識別工作表中的邏輯行數(shù)。
因此,當您用100行填充它時,它將創(chuàng)建100條記錄。其余部分將在您使用默認布局在Excel中打開XLS時顯示,但這些不是POI記錄。
您必須在最后一條數(shù)據(jù)記錄之后創(chuàng)建一些虛擬行,如下所示
for (int i=currentDataRow ;i<65000;i++)
sheet.createRow(i);
創(chuàng)建單元格樣式并將其設(shè)置為隱藏
CellStyle hiddenstyle = workBook.createCellStyle();
hiddenstyle.setHidden(true);
為從最后一行到工作表末尾的所有行設(shè)置此樣式
while (rows.hasNext()){
Row row1 = rows.next ();
row1.setRowStyle(hiddenstyle);
}
這篇關(guān)于如何使用Java ApachePOI在Excel表格中隱藏以下未使用的行?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,