简体   繁体   English

从while循环外部到达While变量

[英]Reach While variable from outside while loop

I must reach variable inside the while loop. 我必须在while循环内到达变量。 I know if i declare variable outside of loop i can reach it. 我知道如果我在循环外声明变量,我可以到达它。 But variable must inside the loop in this scenario. 但是在这种情况下,变量必须在循环内。 Let me explain with an example; 让我举例说明;

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++;
 }

This scenario create excel row for every resultset row as you can see. 如您所见,此方案为每个结果集行创建excel行。 Loop increase row index every time and create new row for new resultset row. 每次循环增加行索引,并为新的结果集行创建新行。 Only 3 columns using but list is long as vertical. 只有3列使用,但list长于垂直。 If i want add data to 4th cell of same row i cant. 如果我想将数据添加到同一行的第4个单元格,我不能。 Because rows already created by loop. 因为行已经由循环创建。 If i can reach "row2" variable from outside i can add cell to 4th column just like this; 如果我可以从外部到达“ row2”变量,则可以像这样将单元格添加到第4列;

Cell cell9 = row2.createCell((short) 4);
cell9.setCellValue("example string");
cell9.setCellStyle(styledatadouble);

Any way create row start from cell index 4 in poi? 从poi的单元格索引4开始创建行的任何方式吗? Or anyway reach while loop variable from outside? 还是无论如何从外面到达while循环变量?

在while循环之后,可以使用sheet.getRow(2)再次检索行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM