【问题标题】:Strange behaviour by Apache poi while reading excel fileApache poi在读取excel文件时的奇怪行为
【发布时间】:2015-06-25 05:22:16
【问题描述】:

我已经使用 Apache POI 库成功读取了 excel 文件。但是,我收到了一个奇怪的行为,我不确定它为什么会发生。

如果我创建一个新的excel文件并调整所需数据,就像这样:

在电子邮件列的第一个设置的空单元格根本不被读取(忽略)。

但如果我修改文件并更改同一文件的字体或字体大小,Apache POI 会成功读取空电子邮件单元格。

默认字体设置(未读取空单元格):

我从方法中收到的数组:

[Hari Krishna, 445444, 986544544]

更改字体大小(空单元格读取成功):

我从方法中收到的数组:

[Hari Krishna, 445444, 986544544, ]

这是我用来读取 excel 文件的完整代码:

 public static List importExcelFile(String filePath, String fileName) {
    DataFormatter formatter = new DataFormatter(Locale.UK);
    // stores data from excel file
    List excelDataList = new ArrayList();
    try {
      // Import file from source destination
      FileInputStream file = new FileInputStream(new File(filePath.concat(File.separator.concat(fileName))));

      // Get the workbook instance for XLS file
      XSSFWorkbook workbook = new XSSFWorkbook(file);
      // workbook.setMissingCellPolicy(Row.RETURN_BLANK_AS_NULL);
      // Get first sheet from the workbook
      XSSFSheet sheet = workbook.getSheetAt(0);
      // Iterate through each rows from first sheet
      Iterator<Row> rowIterator = sheet.iterator();
      // Skip first row, since it is header row
      rowIterator.next();
      while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        int nextCell = 1;
        int currentCell = 0;
        // add data of each row
        ArrayList rowList = new ArrayList();
        // For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext()) {
          Cell cell = cellIterator.next();
          currentCell = cell.getColumnIndex();
          if (currentCell >= nextCell) {
            int diffInCellCount = currentCell - nextCell;
            for (int nullLoop = 0; nullLoop <= diffInCellCount; nullLoop++) {
              rowList.add(" ");
              nextCell++;
            }
          }
          switch (cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
              rowList.add(cell.getBooleanCellValue());
              break;
            case Cell.CELL_TYPE_NUMERIC:
              if (DateUtil.isCellDateFormatted(cell)) {
                String date = formatter.formatCellValue(cell);
                rowList.add(date);
              } else {
                rowList.add(cell.getNumericCellValue());
              }
              break;
            case Cell.CELL_TYPE_STRING:
              rowList.add(cell.getStringCellValue());
              break;
            case Cell.CELL_TYPE_BLANK:
              rowList.add(" ");
              break;
            case Cell.CELL_TYPE_ERROR:
              rowList.add(" ");
              break;
            default:
              break;
          }
          nextCell++;
        }
        excelDataList.add(rowList);
      }
      file.close();
    } catch (FileNotFoundException e) {
      System.out.println(e.toString());
      return null;
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
    return excelDataList;

  }

【问题讨论】:

标签: java excel apache apache-poi


【解决方案1】:

原因是当您设置单元格的字体大小时,Excel 需要一种方法来知道该单元格具有不同的字体(通常是CellStyle)。当您更改默认字体大小时,Excel 会创建一个空白单元格并为其指定单元格样式 - 字体大小为 10。因为 CellStyleCell 的属性,Excel 需要 Cell 所以它可以为它存储CellStyle

当您读取带有Iterator&lt;Cell&gt;Cells 时,它只会返回那些存在的Cells。在您更改字体大小之前,“Hari Krishna”的“电子邮件”单元格不存在。更改字体大小后,现在“Hari Krishna”的“电子邮件”单元格存在,即使它是空白的。

如果你想要空白值,即使没有字体大小变化,也不能使用Iterator,因为它不会返回Cell——它不存在。您可以在Row 对象上使用标准的for 循环,使用MissingCellPolicy of CREATE_NULL_AS_BLANK

如果您想跳过空白值,无论字体大小是否发生变化,您都应该跳过类型为CELL_TYPE_BLANK 的单元格。从您的 switch 声明中删除该案例。

【讨论】:

    猜你喜欢
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多