简体   繁体   English

如何使用Apache POI输出到特定的列和工作表?

[英]How Do I Output To A Specific Column and Worksheet Using Apache POI?

I am teaching myself Apache POI on the fly this weekend. 这个周末我要在自学Apache POI。 My latest task is to take a column that has numerical values and also blank cells, and add values to the blank ones. 我的最新任务是获取具有数值和空白单元格的列,并将值添加到空白单元格中。 I think I've almost accomplished that much with a switch statement and setCellValues, but I cannot seem to output the contents back to my workbook on the specific sheet and column. 我想我用switch语句和setCellValues差不多可以做到,但是我似乎无法将内容输出到特定工作表和列上的工作簿中。

Here is what I Have below. 这是我下面的内容。 The data is pulled into the method just fine. 数据被很好地拉入方法中。

Random randomNum = new Random();
      double randomD = randomNum .nextDouble();


      for (Row row : sheet2) {
          for (Cell cell : row) {

              switch (cell.getCellType()) {
                  case Cell.CELL_TYPE_NUMERIC:
                  System.out.println(cell.getNumericCellValue());
                      break;
                  case Cell.CELL_TYPE_BLANK:
                     cell.setCellValue(randomD);
                     FileOutputStream fileOut = new FileOutputStream("C:/Users/testtest/Desktop/Test/ProductTests.xls");
                     wb.write(fileOut);
                     fileOut.close();
                        System.out.println(cell.getNumericCellValue());
                      break;
                  default:
                      System.out.println();
              }
          }
      }

First: Don't try to write the whole file on every cell, only do this at the end when you have done all the changes 第一:不要尝试在每个单元格上写入整个文件,只有在完成所有更改后才这样做

Second: Do no try to write to the same file that you used for loading, POI does not support in-place writing of changes (yet). 第二:不要尝试写入与用于加载的文件相同的文件,POI不支持就地写入更改(但)。

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

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