[英]Reach While variable from outside while loop
我必須在while循環內到達變量。 我知道如果我在循環外聲明變量,我可以到達它。 但是在這種情況下,變量必須在循環內。 讓我舉例說明;
while (rs.next()) {
HSSFRow row2 = sheet.createRow((short) index);
Cell cell5 = row2.createCell((short) 0);
cell5.setCellValue(rs.getString(1));
cell5.setCellStyle(stylezones);
Cell cell6 = row2.createCell((short) 1);
cell6.setCellValue(rs.getInt(2));
cell6.setCellStyle(styledatathousand);
Cell cell7 = row2.createCell((short) 2);
cell7.setCellValue(rs.getInt(3));
cell7.setCellStyle(styledatathousand);
Cell cell8 = row2.createCell((short) 3);
cell8.setCellValue(rs.getDouble(4));
cell8.setCellStyle(styledatadouble);
index++;
rscount++;
}
如您所見,此方案為每個結果集行創建excel行。 每次循環增加行索引,並為新的結果集行創建新行。 只有3列使用,但list長於垂直。 如果我想將數據添加到同一行的第4個單元格,我不能。 因為行已經由循環創建。 如果我可以從外部到達“ row2”變量,則可以像這樣將單元格添加到第4列;
Cell cell9 = row2.createCell((short) 4);
cell9.setCellValue("example string");
cell9.setCellStyle(styledatadouble);
從poi的單元格索引4開始創建行的任何方式嗎? 還是無論如何從外面到達while循環變量?
在while循環之后,可以使用sheet.getRow(2)
再次檢索行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.