簡體   English   中英

我在 excel 表中的單元格迭代器和空白單元格類型方面遇到問題。 如何動態處理空白單元格和空白列調整

[英]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

Java中,如何動態處理 excel 工作表和空白列調整中的空白單元格。

我在單元格迭代器和空白單元格類型方面面臨問題。

當我為postgresql db創建動態查詢時,在插入查詢時,由於 excel 中的空白大小寫,我遇到了異常。 我正在嘗試使用poi在空白情況下出現異常。

Excel 圖像

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();
}

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(); 在頂部 header 行接收長度。

有關getCell()的更多信息可以在文檔中找到。

    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();
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM