Java Apache poi在读取excel文件时的奇怪行为

Java Apache poi在读取excel文件时的奇怪行为,java,excel,apache,apache-poi,Java,Excel,Apache,Apache Poi,我已经使用ApachePOI库成功地读取了excel文件。但是,我从它那里收到了一种奇怪的行为,我不知道为什么会发生这种情况 如果我创建了一个新的excel文件,并对所需数据进行了如下调整: 在电子邮件列的第一个位置设置的空单元格根本不会被读取(忽略) 但是,如果我修改文件并更改同一文件的字体或字号,ApachePOI将成功读取空的电子邮件单元格 默认字体设置(未读取空单元格): 我从方法接收的数组: [Hari Krishna, 445444, 986544544] [Hari Kris

我已经使用ApachePOI库成功地读取了excel文件。但是,我从它那里收到了一种奇怪的行为,我不知道为什么会发生这种情况

如果我创建了一个新的excel文件,并对所需数据进行了如下调整:

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

但是,如果我修改文件并更改同一文件的字体或字号,ApachePOI将成功读取空的电子邮件单元格

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

我从方法接收的数组:

[Hari Krishna, 445444, 986544544]
[Hari Krishna, 445444, 986544544, ]
更改字体大小(空单元格读取成功):

我从方法接收的数组:

[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;

  }
公共静态列表导入文件(字符串文件路径,字符串文件名){
DataFormatter formatter=新的DataFormatter(Locale.UK);
//存储excel文件中的数据
List excelDataList=新建ArrayList();
试一试{
//从源目标导入文件
FileInputStream file=newfileinputstream(新文件(filePath.concat(file.separator.concat(fileName)));
//获取XLS文件的工作簿实例
XSSF工作簿=新XSSF工作簿(文件);
//workbook.setMissingCellPolicy(行。返回为空);
//从工作簿中获取第一张工作表
XSSFSheet sheet=workbook.getSheetAt(0);
//从第一张图纸开始遍历每行
迭代器rowIterator=sheet.Iterator();
//跳过第一行,因为它是标题行
roweiterator.next();
while(roweiterator.hasNext()){
行=行迭代器。下一步();
int-nextCell=1;
int currentCell=0;
//添加每行的数据
ArrayList行列表=新建ArrayList();
//对于每一行,遍历每一列
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
currentCell=cell.getColumnIndex();
如果(currentCell>=nextCell){
int diffInCellCount=currentCell-nextCell;

对于(int nullLoop=0;nullLoop,原因是当您设置单元格的字体大小时,Excel需要一种方法来知道单元格具有不同的字体(通常是
CellStyle
)。当您更改默认字体大小时,Excel创建了一个空白单元格,并给它一个单元格样式-字体大小为10。由于
单元格样式
单元格
的一个属性,Excel需要一个
单元格
,以便为其存储
单元格样式

当您使用
迭代器阅读
单元格
时,它将只返回那些存在的
单元格
。在更改字体大小之前,“哈里克里希纳”的“电子邮件”单元格不存在。更改字体大小后,现在“哈里克里希纳”的“电子邮件”单元格存在,即使它是空的

如果您想要空白值,即使没有字体大小的改变,也不能使用<代码> Iterator <代码>,因为它不会返回<代码>单元格< /代码>——它不存在。

如果您想跳过空白值,不管字体大小是否有变化,那么您应该跳过具有“代码> > CyryType Type空白/代码>的单元格。从您的<代码>开关语句中删除该情况。