简体   繁体   中英

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

I am teaching myself Apache POI on the fly this weekend. 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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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