读取 excel 文件 2010 apache poi 忽略空单元格 [英] Read excel file 2010 apache poi ignoring empty cells

查看:51
本文介绍了读取 excel 文件 2010 apache poi 忽略空单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,java应用程序在读取Excel文件.xlsx扩展名时,应用程序工作正常,但工作簿中有很多张,1、2张正确读取,但读取3张时,没有读取所有单元格,当我从 Excel 文件打开并检查时,单元格存在空值,但是当 apache poi 读取它时,单元格只是忽略了空单元格.什么是原因?更新

I am having an problem, while java application reading Excel file .xlsx extention, The application is working normally, but in the workbook has many sheets, 1, 2 sheets reading correctly , but when reads 3 sheet, there does not read all the cells and while I opened and checked from Excel file, cell is exists with empty values, but when apache poi reading it the cell is just ignored the empty cells. What is a cause? Update

 FileInputStream file = new FileInputStream(file_location);
  //this.workbook = new HSSFWorkbook(file);
  this.workbook = new XSSFWorkbook(file);
XSSFSheet mySheet = workbook.getSheetAt(index);
       Iterator rowIter = mySheet.rowIterator();
        while(rowIter.hasNext()){
          XSSFRow myRow = (XSSFRow) rowIter.next();
          Iterator cellIter = myRow.cellIterator();
          ArrayList colStoreArray=new ArrayList();



          while(cellIter.hasNext()){
              XSSFCell myCell = (XSSFCell) cellIter.next();
              colStoreArray.add(myCell);
                                   }
          rowArrayHolder.add(colStoreArray);

        } }

在读取 excel 文件上方,虽然我在一行 20 个单元格中检查了 Excel 文件,但在某些行中只读取了 14 行.我正在使用 ArrayList,似乎这里应该是并发访问 ArrayList 的问题.可能是什么原因?

Above the reading excel file, While i checked the Excel file there in one row 20 cells, but while it reads in some rows only 14 rows. I am using ArrayList, seems here should be problem with the concurrent accessing the ArrayList. What can be cause?

推荐答案

迭代器只迭代文件中定义的单元格.如果该单元格从未在 Excel 中使用过,它可能不会出现在文件中(Excel 并不总是一致的...),因此 POI 将看不到它

The iterators only iterate over cells that are defined in the file. If the cell has never been used in Excel, it probably won't appear in the file (Excel isn't always consistent...), so POI won't see it

如果您想确保命中每个单元格,则应改为按索引查找,并检查空单元格(表示该单元格从未存在于文件中),或设置 MissingCellPolicy控制如何处理空单元格和空白单元格

If you want to make sure you hit every cell, you should lookup by index instead, and either check for null cells (indicating the cell has never existed in the file), or set a MissingCellPolicy to control how you want null and blank cells to be treated

这篇关于读取 excel 文件 2010 apache poi 忽略空单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆