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

时间:2011-04-18 07:46:29

标签: 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文件并将值打印到控制台中。这里我在读取空白单元格时遇到问题,并且这些空白单元格未显示在控制台中。所以请给我解读空白单元格的解决方案并在控制台中打印这个空白。

3 个答案:

答案 0 :(得分:6)

来自HSSFRow#cellIterator JavaDoc:

  

请注意,第4个元素可能不是单元格4,因为迭代器不会返回未定义的(null)单元格。在返回的单元格上调用getCellNum()以了解它们是哪个单元格。

这意味着您必须存储当前和最后一个单元格编号,如果您得到的差异大于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
  }
}

答案 1 :(得分:1)

List cellDataList = new ArrayList();
int lineNumber = 0;   

while (rowIterator.hasNext())
{
    HSSFRow hssfRow = (HSSFRow) rowIterator.next();
    //System.out.println("Befor If");
    lineNumber++;
    if(lineNumber==1){continue;}
    //System.out.println("Out side if ");

    Iterator iterator = hssfRow.cellIterator();
    List cellTempList = new ArrayList();
    int current = 0, next = 1;

    while (iterator.hasNext())
    {
        HSSFCell hssfCell = (HSSFCell) iterator.next();
        current = hssfCell.getColumnIndex();

        if(current<next) 
        {
            System.out.println("Condition Satisfied");
        }
        else 
        {
            int loop = current-next;
            System.out.println("inside else Loop value : "+(loop));

            for(int k=0;k<loop+1;k++)
            {
            System.out.println("Adding nulls");
                cellTempList.add(null);
                next = next + 1;
            }
        }

        cellTempList.add(hssfCell);
        next = next + 1;
        System.out.println("At End  next value is : "+next);
    }

    cellDataList.add(cellTempList);
}

尝试使用此代码即可。

答案 2 :(得分:0)

if(myCell.cellType == NPOI.SS.UserModel.CellType.BLANK)

您是否修改或覆盖了HSSFCell?如果没有,这应该可以正常工作。