Detecting hidden cells in excel using apache poi

给你一囗甜甜゛ 提交于 2019-12-07 16:47:38

问题


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.

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:

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):

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)


回答1:


getRowStyle should normally work as you supposed.

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().




回答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.



来源:https://stackoverflow.com/questions/10529909/detecting-hidden-cells-in-excel-using-apache-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!