C# POI检查单元是否为空?

C# POI检查单元是否为空?,c#,excel,apache-poi,npoi,C#,Excel,Apache Poi,Npoi,嗯,我正在试着从excel表格中读取单元格。如果单元格没有值或为空,则返回false。我尝试了“null”(sheet.getrow(a).getcell(b)==null和sheet.getrow(a).getcell(b).celltype==celltype.Blank)但当单元格有空格或填充颜色时,它返回false 谢谢,这件事我已经坚持了好几天了。(如果您需要代码,我可以编辑它)。单元格是否为“空”部分取决于单元格是否实际存在(即不为空)、单元格的类型(字符串/数字/空白等)以及单元格

嗯,我正在试着从excel表格中读取单元格。如果单元格没有值或为空,则返回false。我尝试了“
null
”(
sheet.getrow(a).getcell(b)==null
sheet.getrow(a).getcell(b).celltype==celltype.Blank
)但当单元格有空格或填充颜色时,它返回
false

谢谢,这件事我已经坚持了好几天了。(如果您需要代码,我可以编辑它)。

单元格是否为“空”部分取决于单元格是否实际存在(即不为空)、单元格的类型(字符串/数字/空白等)以及单元格中的值,具体取决于其类型。我会做一些扩展方法,使这个决定更容易。您可以调整它们,使其按照您的意愿工作。例如,如果您认为一个没有值但填充了颜色的单元格是非空的,则可以在<代码> IsNullOrEmpty < /COD>方法中添加一个检查。< /P>
public static class NpoiExtensions
{
    public static bool IsCellNullOrEmpty(this ISheet sheet, int rowIndex, int cellIndex)
    {
        if (sheet != null)
        {
            IRow row = sheet.GetRow(rowIndex);
            if (row != null)
            {
                ICell cell = row.GetCell(cellIndex);
                return cell.IsNullOrEmpty();
            }
        }
        return true;
    }

    public static bool IsNullOrEmpty(this ICell cell)
    {
        if (cell != null)
        {
            // Uncomment the following lines if you consider a cell 
            // with no value but filled with color to be non-empty.
            //if (cell.CellStyle != null && cell.CellStyle.FillBackgroundColorColor != null)
            //    return false;

            switch (cell.CellType)
            {
                case CellType.String:
                    return string.IsNullOrWhiteSpace(cell.StringCellValue);
                case CellType.Boolean:
                case CellType.Numeric:
                case CellType.Formula:
                case CellType.Error:
                    return false;
            }
        }
        // null, blank or unknown
        return true;
    }
}
有了这些方法,您的代码变得更简单:

if (sheet.IsCellNullOrEmpty(a, b))
{
    Console.WriteLine("Cell at row " + a + " column " + b + " is empty.");
}
单元格是否为“空”部分取决于单元格是否实际存在(即不为空)、单元格的类型(字符串/数字/空白等)以及单元格中的值,具体取决于单元格的类型。我会做一些扩展方法,使这个决定更容易。您可以调整它们,使其按照您的意愿工作。例如,如果您认为一个没有值但填充了颜色的单元格是非空的,则可以在<代码> IsNullOrEmpty < /COD>方法中添加一个检查。< /P>
public static class NpoiExtensions
{
    public static bool IsCellNullOrEmpty(this ISheet sheet, int rowIndex, int cellIndex)
    {
        if (sheet != null)
        {
            IRow row = sheet.GetRow(rowIndex);
            if (row != null)
            {
                ICell cell = row.GetCell(cellIndex);
                return cell.IsNullOrEmpty();
            }
        }
        return true;
    }

    public static bool IsNullOrEmpty(this ICell cell)
    {
        if (cell != null)
        {
            // Uncomment the following lines if you consider a cell 
            // with no value but filled with color to be non-empty.
            //if (cell.CellStyle != null && cell.CellStyle.FillBackgroundColorColor != null)
            //    return false;

            switch (cell.CellType)
            {
                case CellType.String:
                    return string.IsNullOrWhiteSpace(cell.StringCellValue);
                case CellType.Boolean:
                case CellType.Numeric:
                case CellType.Formula:
                case CellType.Error:
                    return false;
            }
        }
        // null, blank or unknown
        return true;
    }
}
有了这些方法,您的代码变得更简单:

if (sheet.IsCellNullOrEmpty(a, b))
{
    Console.WriteLine("Cell at row " + a + " column " + b + " is empty.");
}