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

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

Try the GetCell method with the MissingCellPolicy:

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

 

https://github.com/tonyqus/npoi/blob/master/main/HSSF/UserModel/HSSFRow.cs#L444

复制代码
 /// <summary>
        /// Get the hssfcell representing a given column (logical cell)
        /// 0-based.  If you ask for a cell that is not defined then
        /// you get a null, unless you have set a different
        /// MissingCellPolicy on the base workbook.
        /// </summary>
        /// <param name="cellnum">0 based column number</param>
        /// <returns>Cell representing that column or null if undefined.</returns>
        public ICell GetCell(int cellnum)
        {
            return GetCell(cellnum, book.MissingCellPolicy);
        }
复制代码

 

https://github.com/tonyqus/npoi/blob/master/main/HSSF/UserModel/HSSFWorkbook.cs#L429

复制代码
 [NonSerialized]
        private NPOI.SS.UserModel.MissingCellPolicy missingCellPolicy = NPOI.SS.UserModel.MissingCellPolicy.RETURN_NULL_AND_BLANK;
        /// <summary>
        /// Retrieves the current policy on what to do when
        /// getting missing or blank cells from a row.
        /// The default is to return blank and null cells.
        /// </summary>
        /// <value>The missing cell policy.</value>
        public NPOI.SS.UserModel.MissingCellPolicy MissingCellPolicy
        {
            get { return missingCellPolicy; }
            set { this.missingCellPolicy = value; }
        }
复制代码

 

复制代码
/// <summary>Missing cells are returned as null, Blank cells are returned as normal</summary>
        // Token: 0x04001517 RID: 5399
        public static readonly MissingCellPolicy RETURN_NULL_AND_BLANK = new MissingCellPolicy();

        /// <summary>Missing cells are returned as null, as are blank cells</summary>
        // Token: 0x04001518 RID: 5400
        public static readonly MissingCellPolicy RETURN_BLANK_AS_NULL = new MissingCellPolicy();

        /// <summary>A new, blank cell is Created for missing cells. Blank cells are returned as normal</summary>
        // Token: 0x04001519 RID: 5401
        public static readonly MissingCellPolicy CREATE_NULL_AS_BLANK = new MissingCellPolicy();
复制代码

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(490)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-09-29 posh-git
2018-09-29 leelazero and google colab
2018-09-29 download file by python in google colab
2018-09-29 physical processor, core, logical processor
2016-09-29 Entity Framework 不支持DefaultValue
2015-09-29 给枚举加上Description,必要时,可以直接获取枚举类型代表的中文
2015-09-29 word使用技巧
点击右上角即可分享
微信分享提示