Free Support Forum - aspose.com

Check that sheet is empty

Hello!
How can we check with api aspose.cell that sheet is empty ?
Thanks.

@Tesla19,
You may try the following sample code which checks each worksheet in a workbook for any data, shape or empty cell initialization.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.

    // 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");
        }
    }
}