使用apache poi检测excel中的隐藏单元格

时间:2021-10-30 20:23:24

We are using apache poi 3.8 to parse excels. We need to be able to detect (and skip) hidden rows as they tend to contain junk data in our usecases.

我们使用apache poi 3.8来解析excels。我们需要能够检测(并跳过)隐藏的行,因为它们往往在我们的用例中包含垃圾数据。

It would seem this should work:

这似乎应该工作:

row.isFormatted() && row.getRowStyle().getHidden()

But there never appears to be any row-level formatting (getRowStyle() always returns null). As a last resort we thought checking cell styles might work:

但似乎从来没有任何行级格式(getRowStyle()总是返回null)。作为最后的手段,我们认为检查单元格样式可能有效:

for (int i = 0; i < row.getLastCellNum(); i++) {
    Cell cell = row.getCell(i);
    if (cell != null && cell.getCellStyle() != null && cell.getCellStyle().getHidden())
        ...

But for every row we get (custom output in the above for loop):

但是对于我们获得的每一行(上面的for循环中的自定义输出):

Cell 0 is not hidden org.apache.poi.hssf.usermodel.HSSFCellStyle@1b9142d0 / false

Does the "getHidden()" not work or does it not work as I think it does? Is there another way to detect hidden rows? (hidden columns would also be a nice bonus but slightly less relevant atm)

“getHidden()”不起作用还是不起作用?还有另一种检测隐藏行的方法吗? (隐藏的列也是一个很好的奖励,但相关性稍差的atm)

2 个解决方案

#1


5  

getRowStyle should normally work as you supposed.

getRowStyle应该正常工作。

Otherwise, you can check the height of the row, as hidden rows tends to have a height set to 0. Using row.getHeight() or row.getZeroHeight().

否则,您可以检查行的高度,因为隐藏行的高度往往设置为0.使用row.getHeight()或row.getZeroHeight()。

#2


2  

After trying a few approaches, row.getZeroHeight() worked correctly for identifying hidden row. Also for those who are stuck with Apache POI <= 3.7 version this may be the only solution.

尝试几种方法后,row.getZeroHeight()正常工作以识别隐藏的行。对于那些坚持使用Apache POI <= 3.7版本的人来说,这可能是唯一的解决方案。

#1


5  

getRowStyle should normally work as you supposed.

getRowStyle应该正常工作。

Otherwise, you can check the height of the row, as hidden rows tends to have a height set to 0. Using row.getHeight() or row.getZeroHeight().

否则,您可以检查行的高度,因为隐藏行的高度往往设置为0.使用row.getHeight()或row.getZeroHeight()。

#2


2  

After trying a few approaches, row.getZeroHeight() worked correctly for identifying hidden row. Also for those who are stuck with Apache POI <= 3.7 version this may be the only solution.

尝试几种方法后,row.getZeroHeight()正常工作以识别隐藏的行。对于那些坚持使用Apache POI <= 3.7版本的人来说,这可能是唯一的解决方案。