为什么这个 for 循环不使用 Apache POI 遍历行? Java
Why is this for loop not iterating thru rows using Apache POI? Java
我有一个 for 循环,里面有另一个循环,第一个循环设置行,第二个循环读取 4 个单元格,循环第一次运行时运行良好,但在那之后我猜它不会改变行,因为它只是重复第一个循环的结果但不显示单元格值,这里是代码:
for(int k=1; k<=2; k++){
XSSFRow row1 = worksheet.getRow(e);
System.out.println("Row: "+e);
e++;
for(int i=0;i<Oleo18_1.size();i++){
XSSFCell c = row1.getCell((short) d);
if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
Oleo18_1.set(i, false);
}
else {
Oleo18_1.set(i, true);
c.setCellType(Cell.CELL_TYPE_STRING);
}
if(Oleo18_1.get(i) == true){
values_18_1.set(i, 9.09090909);
}
String a1Val = c.getStringCellValue();
System.out.println("valor ponderacion"+(d+1)+": "+values_18_1.get(i));
System.out.println("valor celda "+(d+1)+": "+a1Val);
d++;
}
}
因此,使用 Apache POI 迭代单元格的最简单方法是使用 Busy Developers Guide 中的提示,该提示演示了在 for-each 循环中进行迭代,因为行可以像这样迭代:
Iterate over rows and cells
Sometimes, you'd like to just iterate over all the rows in a sheet, or all the >cells in a row. This is possible with a simple for loop.
Luckily, this is very easy. Row defines a CellIterator inner class to handle iterating over the cells (get one with a call to row.cellIterator()), and Sheet provides a rowIterator() method to give an iterator over all the rows. These implement the java.lang.Iterable interface to allow foreach loops.
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
// Do something here
}
}
其中最重要的部分实际上是 "grabbing" 正确的 sheet 正如您在代码片段的第一行中看到的那样并遍历所需的部分而不是指定行你想开始。相反,您想有选择地操作您的数据,如下所示:
private void startParsing() {
for (int i = 0; i < worksheet.getPhysicalNumberOfRows(); i++) {
if (i == ROW_E || i == ROW_F) {
mutateCell(worksheet.getRow(i));
}
}
}
private void mutateCell(final Row curRow) {
for(int i=0;i<Oleo18_1.size();i++){
XSSFCell c = curRow.getCell((short) d);
if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
Oleo18_1.set(i, false);
}
else {
Oleo18_1.set(i, true);
c.setCellType(Cell.CELL_TYPE_STRING);
}
if(Oleo18_1.get(i) == true){
values_18_1.set(i, 9.09090909);
}
String a1Val = c.getStringCellValue();
System.out.println("valor ponderacion"+(d+1)+": "+values_18_1.get(i));
System.out.println("valor celda "+(d+1)+": "+a1Val);
d++;
}
}
我有一个 for 循环,里面有另一个循环,第一个循环设置行,第二个循环读取 4 个单元格,循环第一次运行时运行良好,但在那之后我猜它不会改变行,因为它只是重复第一个循环的结果但不显示单元格值,这里是代码:
for(int k=1; k<=2; k++){
XSSFRow row1 = worksheet.getRow(e);
System.out.println("Row: "+e);
e++;
for(int i=0;i<Oleo18_1.size();i++){
XSSFCell c = row1.getCell((short) d);
if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
Oleo18_1.set(i, false);
}
else {
Oleo18_1.set(i, true);
c.setCellType(Cell.CELL_TYPE_STRING);
}
if(Oleo18_1.get(i) == true){
values_18_1.set(i, 9.09090909);
}
String a1Val = c.getStringCellValue();
System.out.println("valor ponderacion"+(d+1)+": "+values_18_1.get(i));
System.out.println("valor celda "+(d+1)+": "+a1Val);
d++;
}
}
因此,使用 Apache POI 迭代单元格的最简单方法是使用 Busy Developers Guide 中的提示,该提示演示了在 for-each 循环中进行迭代,因为行可以像这样迭代:
Iterate over rows and cells
Sometimes, you'd like to just iterate over all the rows in a sheet, or all the >cells in a row. This is possible with a simple for loop.
Luckily, this is very easy. Row defines a CellIterator inner class to handle iterating over the cells (get one with a call to row.cellIterator()), and Sheet provides a rowIterator() method to give an iterator over all the rows. These implement the java.lang.Iterable interface to allow foreach loops.
Sheet sheet = wb.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { // Do something here } }
其中最重要的部分实际上是 "grabbing" 正确的 sheet 正如您在代码片段的第一行中看到的那样并遍历所需的部分而不是指定行你想开始。相反,您想有选择地操作您的数据,如下所示:
private void startParsing() {
for (int i = 0; i < worksheet.getPhysicalNumberOfRows(); i++) {
if (i == ROW_E || i == ROW_F) {
mutateCell(worksheet.getRow(i));
}
}
}
private void mutateCell(final Row curRow) {
for(int i=0;i<Oleo18_1.size();i++){
XSSFCell c = curRow.getCell((short) d);
if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
Oleo18_1.set(i, false);
}
else {
Oleo18_1.set(i, true);
c.setCellType(Cell.CELL_TYPE_STRING);
}
if(Oleo18_1.get(i) == true){
values_18_1.set(i, 9.09090909);
}
String a1Val = c.getStringCellValue();
System.out.println("valor ponderacion"+(d+1)+": "+values_18_1.get(i));
System.out.println("valor celda "+(d+1)+": "+a1Val);
d++;
}
}