【问题标题】:I am facing problem at cell iterator and blank cell type in excel sheet. how to handle the blank cells and blank columns adjustment dynamically我在 Excel 表中的单元格迭代器和空白单元格类型方面面临问题。如何动态处理空白单元格和空白列调整
【发布时间】:2021-07-02 19:55:31
【问题描述】:

Java,如何动态处理excel表格中的空白单元格和空白列调整。

我在单元格迭代器和空白单元格类型方面遇到问题。

当我为 postgresql db 创建动态查询时,在插入查询时,由于 Excel 中的空白大小写,我遇到了异常。我正在尝试使用 poi 在空白情况下遇到异常。

Excel image

try {
  DataFormatter dataFormatter = new DataFormatter();
  Workbook workbook = WorkbookFactory.create(excelFile);
  Iterator<Sheet> sheetIterator = workbook.sheetIterator();
  List<List<List<Object>>> sheets = new ArrayList<>();

  while (sheetIterator.hasNext()) {
    List<List<Object>> sheetList = new ArrayList<>();
    Sheet sheet = sheetIterator.next();
    logger.info(" ---sheet name ---" + sheet.getSheetName());
    if (sheetNames != null) {
      for (String sheetName : sheetNames) {
        if (sheet.getSheetName().equals(sheetName)) {
          Iterator<Row> rowIterator = sheet.rowIterator();
            while (rowIterator.hasNext()) {
              Row row = rowIterator.next();
              List<Object> rows = new ArrayList<>();

              // Now let's iterate over the columns of the current row
              Iterator<Cell> cellIterator = row.cellIterator();
              while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                CellType type = cell.getCellType();
                if (type == CellType.BLANK) {
                  Integer intsample = 0;
                  cell.setCellValue(intsample);

                  // rows.add(dataFormatter.formatCellValue(cell));
                  rows.add(cell);   
                }
                else if (type == CellType.STRING) {
                  rows.add(cell.getRichStringCellValue().toString());
                } else if (type == CellType.NUMERIC) {
                  if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    rows.add(cell.getDateCellValue());
                  } else if (dataFormatter.formatCellValue(cell).contains(".")) {
                    try {
                      rows.add(Double.parseDouble(dataFormatter.formatCellValue(cell)));
                    } catch (Exception e) {
                        rows.add(cell.getRichStringCellValue().toString());
                    }
                  } else {
                    try {
                      rows.add(Long.parseLong(dataFormatter.formatCellValue(cell)));
                    } catch (Exception e) {
                      rows.add(dataFormatter.formatCellValue(cell));
                    }
                  }
                } else if (type == CellType.BOOLEAN) {
                  rows.add(cell.getBooleanCellValue());
                } else {
                  rows.add(dataFormatter.formatCellValue(cell));
                }
              }
              sheetList.add(rows);
            }
            sheets.add(sheetList);
          }
        }
      }
    }
    workbook.close();

    return sheets;
  } catch (Exception e) {
    e.printStackTrace();
}

【问题讨论】:

    标签: java excel


    【解决方案1】:

    CellIterator 在连续迭代单元格时会跳过空单元格。您可以通过使用 for 循环而不是迭代器来解决此问题:

    Row row = rowIterator.next();
    ...
    int lastCellIndex = 8; // (9-1 cells)
    for (int i=0; i<=lastCellIndex; i++) {
        Cell cell = row.getCell(cn, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
        ...
    }
    
    

    在您的示例中,单元格数为 9,因此 lastCellIndex 为 8。如果一行中的单元格数会有所不同,请使用顶部标题行上的 row.getLastCellNum(); 来接收长度。

    有关getCell() 的更多信息,请访问documentation

    【讨论】:

      【解决方案2】:
          I am trying like with out using celliterator,rowiterator,sheetiterator.but again skipping cell is happen ...
          here is my code 
        try {
                  DataFormatter dataFormatter = new DataFormatter();
                  Workbook workbook = WorkbookFactory.create(file);
                  List<List<List<Object>>> sheets = new ArrayList<List<List<Object>>>();
      
                  for (int sh = 0; sh < workbook.getNumberOfSheets(); sh++) {
      
                      Sheet sheet = workbook.getSheetAt(sh);
                      List<List<Object>> sheetList = new ArrayList<List<Object>>();
      
                      for (int i = 0; i < sheet.getLastRowNum(); i++) {
                          Row row = sheet.getRow(i);
      
                          List<Object> rows = new ArrayList<Object>();
      
                          for (int j = 0; j < row.getLastCellNum(); j++) {
      
                              Cell cell = row.getCell(j);
      
                              CellType type = cell.getCellType();
                              // System.out.println(type+ " -----type -------");
      
                              if (type == CellType.STRING) {
                                  rows.add(cell.getRichStringCellValue().toString());
      
                              } else if (type == CellType.NUMERIC) {
      
                                  if (HSSFDateUtil.isCellDateFormatted(cell)) {
                                      rows.add(cell.getDateCellValue());
                                  } else if (dataFormatter.formatCellValue(cell).contains(".")) {
                                      try {
                                          rows.add(Double.parseDouble(dataFormatter.formatCellValue(cell)));
                                      } catch (Exception e) {
                                          rows.add(cell.getRichStringCellValue().toString());
                                      }
                                  } else {
                                      try {
                                          rows.add(Long.parseLong(dataFormatter.formatCellValue(cell)));
                                      } catch (Exception e) {
                                          rows.add(dataFormatter.formatCellValue(cell));
                                      }
                                  }
      
                              } else if (type == CellType.BOOLEAN) {
                                  rows.add(cell.getBooleanCellValue());
                              } else if (type == CellType.BLANK) {
      
                                   cell.setCellValue("NA");
                                  //System.out.println(cell);
                                      //Cell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                                  Integer intsample = 0;
                                  //cell.setCellValue(intsample);
                                  // rows.add(dataFormatter.formatCellValue(cell));
                                  rows.add(cell);
      
                              } else {
                                  rows.add(dataFormatter.formatCellValue(cell));
      
                              }
      
                          }
      
                          sheetList.add(rows);
                          System.out.println(rows);
                      }
      
                      sheets.add(sheetList);
                      // System.out.println(sheets+"\n");
                  }
      
                  workbook.close();
      
                  return sheets;
              } catch (Exception e) {
                  e.printStackTrace();
              }
      

      【讨论】:

        猜你喜欢
        • 2017-10-01
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多