简体   繁体   English

Apache POI 框架何时将 excel 单元格视为空和空?

[英]When does Apache POI framework consider a excel cell as empty and null?

I was working on excel sheet upload with the help of apache POI framework in java.我在java中的apache POI框架的帮助下处理excel表格上传。 I inserted an empty column in between filled columns, in some files cell of that particular column is considered as null and in some file the empty cells in column were considered as empty string?我在填充的列之间插入了一个空列,在某些文件中该特定列的单元格被视为空,而在某些文件中,列中的空单元格被视为空字符串? Why is this happening?为什么会这样? I inserted an empty column in excel using google sheets.我使用谷歌表格在 excel 中插入了一个空列。

Also, to understand the question, consider the use of Row.CREATE_NULL_AS_BLANK missing policy we use in row.getCell() method.此外,要理解这个问题,请考虑使用我们在row.getCell()方法中使用的Row.CREATE_NULL_AS_BLANK缺失策略。 When does excel sheet have null value for a cell where we can use this missing policy? excel 工作表何时为我们可以使用此缺失策略的单元格提供空值?

In a Excel file in storage for a sheet only rows and cells are stored if they have values or settings.在用于存储工作表的Excel文件中,只有具有值或设置的行和单元格才会被存储。 Other rows and cells are not stored.不存储其他行和单元格。 Otherwise each sheet always would must have all possible rows and cells stored even when those rows and cells are not used.否则,即使不使用这些行和单元格,每个工作表也必须始终存储所有可能的行和单元格。 That would be a waste of file memory.那会浪费文件内存。

Rows have values if they contain cells having values or settings.如果行包含具有值或设置的单元格,则行具有值。 Rows have settings if they have a row style or a special row height different from default.如果行具有不同于默认值的行样式或特殊行高,则行具有设置。

Cells have values if they have cell value set.如果单元格设置了单元格值,则单元格具有值。 Cells have settings if they have cell styles.如果单元格具有单元格样式,则单元格具有设置。

It might also be that cells are stored even if they are totally blank and don't have style.也可能是存储单元格,即使它们完全空白并且没有样式。 That depends on the spreadsheet software used.这取决于所使用的电子表格软件。 Mainly this happens when cells were present and then deleted or when cells were copy/pasted.这主要发生在单元格存在然后被删除或单元格被复制/粘贴时。

The Sheet.getRow and Row.getCell methods of apache poi get not stored rows or cells as null . apache poiSheet.getRowRow.getCell方法不会将存储的行或单元格存储为null In other words, only stored rows and cells are got as not null .换句话说,只有存储的行和单元格被设为 not null So you always need check null after Sheet.getRow and Row.getCell :所以你总是需要在Sheet.getRowRow.getCell之后检查null

...
Row row = sheet.getRow(3); if (row == null) row = sheet.createRow(3);
Cell cell = row.getCell(3); if (cell == null) cell = row.createCell(3);
...

After that you can be sure having Row row and Cell cell not null .之后,您可以确定Row rowCell cell不为null

For Cell you also can use Row.getCell(int cellnum, Row.MissingCellPolicy policy) .对于Cell您还可以使用Row.getCell(int cellnum, Row.MissingCellPolicy policy) See ( Row.MissingCellPolicy ).请参阅 ( Row.MissingCellPolicy )。

There also is CellUtil which provides methods CellUtil.getRow(int rowIndex, Sheet sheet) and CellUtil.getCell(Row row, int columnIndex) .还有CellUtil提供方法CellUtil.getRow(int rowIndex, Sheet sheet)CellUtil.getCell(Row row, int columnIndex) Both are creating the row or cell if they cannot be got.如果无法获取,两者都在创建行或单元格。

If you only need traversing filled rows and cells, then you can use the iterators as described in Iterate over rows and cells .如果您只需要遍历填充的行和单元格,那么您可以使用迭代行和单元格中所述的迭代器。 The iterators never have null rows or cells.迭代器永远不会null行或单元格。 But then you possibly miss totally blank rows or skip blank cells in rows.但是,您可能会错过完全空白的行或跳过行中的空白单元格。 To avoid that read "Iterate over cells, with control of missing / blank cells" right below.为避免阅读下面的“迭代单元格,控制丢失/空白单元格”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM