简体   繁体   中英

How to use NPOI to read Excel spreadsheet that contains empty cells?

When I read Excel worksheet using NPOI, empty cells are skipped. For example, it the row contains A, B, , C and I read it using

IRow row = sheet.GetRow(rowNb)

then row.Cells[1].ToString() will output B (as expected) but row.Cells[2].ToString() will output C instead of an empty string. Is there a way to keep empty cells? Thanks.

尝试使用MissingCellPolicyGetCell方法:

ICell cell = row.GetCell(2, MissingCellPolicy.RETURN_NULL_AND_BLANK);

In completion to the accepted answer, the policy can be set on the workbook level as well

workbook.MissingCellPolicy = MissingCellPolicy.RETURN_NULL_AND_BLANK;

This way the policy is applied implicitly when you call GetCell , no need to pass it every time as a parameter

ICell cell = row.GetCell(2);

Note that (at least in the version I'm using) if you do row.Cells[index] , it ignores the policy so it only works if you call row.GetCell(index)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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