[英]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.