检测空工作表

检查填充 Cells

工作表可以有一个或多个单元格填充值,其中值可以是简单的(文本、数字、日期/时间)或公式或基于公式的值。在这种情况下,很容易检测到给定工作表是否为空。我们只需要检查Cells.MaxDataRow要么Cells.MaxDataColumn特性。如果上述属性返回零值或正值,这意味着一个或多个单元格已被填充,但是,如果这些属性中的任何一个返回 -1,则表示给定工作表中没有任何单元格被填充。

检查空初始化 Cells

所有具有值的单元格都会自动初始化,但是,工作表中的单元格可能只应用了格式。在这种情况下,Cells.MaxDataRow要么Cells.MaxDataColumn properties 将返回 -1,表示没有任何填充值,但使用此方法无法检测到由于单元格格式而初始化的单元格。为了检查工作表是否有空的初始化单元格,建议对从中获取的枚举器使用 IEnumerator.MoveNext 方法Cells收藏。如果 IEnumerator.MoveNext 方法返回真的这意味着给定工作表中有一个或多个已初始化的单元格。

检查形状

给定的工作表可能没有任何填充的单元格,但是,它可能包含形状和对象,例如控件、图表、图像等。如果我们需要检查工作表是否包含任何形状,我们可以通过检查ShapeCollection.Count财产。任何正值表示工作表中存在形状。

编程范例

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create an instance of Workbook and load an existing spreadsheet
var book = new Workbook(dataDir + "sample.xlsx");
// Loop over all worksheets in the workbook
for (int i = 0; i < book.Worksheets.Count; i++)
{
Worksheet sheet = book.Worksheets[i];
// Check if worksheet has populated cells
if (sheet.Cells.MaxDataRow != -1)
{
Console.WriteLine(sheet.Name + " is not empty because one or more cells are populated");
}
// Check if worksheet has shapes
else if (sheet.Shapes.Count > 0)
{
Console.WriteLine(sheet.Name + " is not empty because there are one or more shapes");
}
// Check if worksheet has empty initialized cells
else
{
Aspose.Cells.Range range = sheet.Cells.MaxDisplayRange;
var rangeIterator = range.GetEnumerator();
if (rangeIterator.MoveNext())
{
Console.WriteLine(sheet.Name + " is not empty because one or more cells are initialized");
}
}
}