java - 如何使用 poi 读取 excel 文件中的空单元格以及如何将此空单元格添加到数组列表?

标签 java apache-poi

    public void readExcel(String fileName)
    try 
    {
          FileInputStream myInput = new FileInputStream(fileName);
          POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
          HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
          HSSFSheet mySheet = myWorkBook.getSheetAt(0);
          Iterator rowIter = mySheet.rowIterator();
          while(rowIter.hasNext())
          {
             HSSFRow myRow = (HSSFRow) rowIter.next();
             Iterator cellIter = myRow.cellIterator();
             ArrayList cellStoreVector=new ArrayList();
             String header_name = null;
             while(cellIter.hasNext())
             {
                 HSSFCell myCell = (HSSFCell) cellIter.next();
                 // if it is empty cell in  my excel file its not added to
                 // array List                            
                cellStoreVector.add(myCell); 
             }
             cellVectorHolder.add(cellStoreVector);

          } 
        }catch (Exception e)
         {
           e.printStackTrace(); 
         }
         saveToDatabase(cellVectorHolder);
        }

      public void saveToDatabase(ArrayList array)
      {
        for (int i=0;i<array.size(); i++)
        {
             ArrayList cellStoreVector=(ArrayList)array.get(i);
             for (int j=0; j < cellStoreVector.size();j++) 
             {  
                HSSFCell myCell = (HSSFCell)cellStoreVector.get(j);
                String st = myCell.toString();
                System.out.println("values "+st);
             }
     }
 }

上面的代码是我读取 excel 文件并将值打印到控制台的示例代码。在这里,我在读取空白单元格时遇到问题,并且此空白单元格未显示在控制台中。所以请给我读取空白单元格的解决方案并在控制台中打印这个空白。

最佳答案

来自 HSSFRow#cellIterator JavaDoc:

Note that the 4th element might well not be cell 4, as the iterator will not return un-defined (null) cells. Call getCellNum() on the returned cells to know which cell they are.

这意味着您必须存储当前和最后一个单元格编号,如果您得到的差异大于 1,则您之间有空白/未定义的单元格,即类似于 int numBlankCells = (curCellNum - lastCellNum) - 1;

另一种方法可能是:

short minColIndex = row.getFirstCellNum();
short maxColIndex = row.getLastCellNum();
for(short colIndex = minColIndex; colIndex < maxColIndex; colIndexx++) {
  HSSFCell cell = row.getCell(colIndex);
  if(cell == null) {
    //blank/undefined cell
  }
  else {
    //defined cell which still could be of type HSSFCell.CELL_TYPE_BLANK or contain an empty string
  }
}

关于java - 如何使用 poi 读取 excel 文件中的空单元格以及如何将此空单元格添加到数组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5699936/

相关文章:

java - Gradle包括JavaFX for Heroku

java - 无法让 JUnit 使用我的 @Autowired 存储库和 MongoTemplate

java - 除了将 Apache POI Java 用于 Microsoft Office 之外,还有其他选择吗?

java - 无法使用 Apache POI 读取 excel 抛出 NoClassDefFoundError

java - 不使用 jdbc url 中的主机名和端口

java - 新手关于maven的问题

java - 对由整数对组成的列表进行排序

java - 通过 Apache POI 将自定义(扩展)属性添加到 docx 和段落

java - 如何在不向前移动迭代器的情况下获取单元格值 - Excel、Java POI

java - 将我的 double 单元格变成 double 数组