Protected Sheet Without Password

We use below method to load the document into Aspose by trying with different LoadFormat

Workbook LoadExcelDocument(FileStream stream)
{
    Aspose.Cells.Workbook workbook;
    bool isExcelDocumentLoaded = false;

    var nativeExcelDocument = stream;

    try
    {
        int counter = 0;
        var loadFormats = Enum.GetValues(typeof(LoadFormat)).Cast<LoadFormat>().ToArray();

        while (counter < loadFormats.Length && !isExcelDocumentLoaded)
        {
            try
            {
                var loadOptions = new Aspose.Cells.LoadOptions(loadFormats[counter++]);
                //Load stream from file store.

                workbook = new Aspose.Cells.Workbook(nativeExcelDocument, loadOptions);
                isExcelDocumentLoaded = true;

                return workbook;
            }
            catch
            {
                Console.WriteLine($"Error {counter}");
                if (counter == loadFormats.Length) 
                    throw;
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Loading Failed");
        Console.WriteLine(ex.Message);
    }

    return null;
}

When we are dealing with Protected sheet, it shouldn’t be loaded with any LoadFormat we use, but we’re able to load the document with LoadFormat.Csv .
But the catch here is the protected document and loaded document is not same and loaded document has only one sheet(As it’s loaded with Csv format) and all the data is encrypted. Can you check this?

@shivanandchikkalli

Summary
A workbook that is protected with a password is encrypted. Aspose.Cells can open such a file only when the correct password is supplied via LoadOptions.Password. Loading the same file with LoadFormat.Csv forces the library to treat the binary Excel file as a plain‑text CSV file, which results in a single sheet containing unreadable data. This is not a supported way to read a protected workbook.

Why it happens

  • File‑level password protection – The password encrypts the whole package, so the file must be decrypted before any Excel‑specific parsing can occur.
  • CSV loader – When LoadFormat.Csv is used, the loader does not attempt decryption; it simply reads the raw bytes as if they were CSV text. Because the source is an encrypted Excel file, the output is garbled and only one sheet is created.

What to do next

  1. Provide the password
using Aspose.Cells;
using System.IO;

Workbook LoadWorkbook(FileStream stream, string password)
{
var loadOptions = new LoadOptions
{
Password = password // required for encrypted files
};
return new Workbook(stream, loadOptions);
}

If the password is unknown, the workbook cannot be opened.

  1. Handle the missing‑or‑incorrect‑password case
try
{
var wb = LoadWorkbook(stream, password);
}
catch (IncorrectPasswordException)
{
Console.WriteLine("The workbook is password‑protected. Supply a valid password.");
}
catch (Exception ex)
{
Console.WriteLine($"Loading failed: {ex.Message}");
}

The IncorrectPasswordException is thrown when the supplied password does not match.

  1. Avoid using plain‑text formats (CSV, TXT) as fall‑backs for Excel files
    Since those loaders do not understand Excel encryption, they will produce unusable output. Limit your fallback list to actual Excel formats such as LoadFormat.Xlsx, LoadFormat.Xlsb, LoadFormat.Xls.

  2. Optional: Detect password protection
    Attempt to load the workbook without a password; if an IncorrectPasswordException is thrown, the file is password‑protected.

Next steps for you

  • Verify whether the file uses a workbook‑level password.
  • Obtain the correct password from the file owner.
  • Load the workbook using LoadOptions.Password as shown above.
  • Remove LoadFormat.Csv (and other plain‑text formats) from any fallback logic when dealing with Excel files.

Note: The explanation is based on Aspose.Cells documentation regarding password‑protected files and the LoadOptions class. The behavior of the CSV loader described here follows the general principle that it does not perform decryption.

:warning: I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.

catching IncorrectPasswordException should work for us to ignore the files which are protected but Aspose.Cells is not throwing this exception, is there any way other than checking the message ? Can this Exception included into Aspose.Cells too ?

@shivanandchikkalli
By examining your sample code, we found that you are traversing all loading formats to load the sample file. You can use FileFormatUtil.DetectFileFormat methnod to detect file formats. Please refer to the following example code.

FileFormatInfo fileFormatInfo = FileFormatUtil.DetectFileFormat("your sample file");
Console.WriteLine("IsEncrypted: " + fileFormatInfo.IsEncrypted);
Console.WriteLine("Can the file be loaded? " + (fileFormatInfo.LoadFormat != LoadFormat.Unknown));

static Workbook LoadExcelDocument(FileStream stream, FileFormatInfo fileFormatInfo)
{
    Aspose.Cells.Workbook workbook;

    try
    {
        var loadOptions = new Aspose.Cells.LoadOptions(fileFormatInfo.LoadFormat);
        //Load stream from file store.
        workbook = new Aspose.Cells.Workbook(stream, loadOptions);

        return workbook;
    }
    catch (Exception ex)
    {
        Console.WriteLine("Loading Failed");
        Console.WriteLine(ex.Message);
    }

    return null;
}

Would you like to describe your needs in detail? If you can provide sample files, it will be very helpful for us to locate the problem. We will check it soon.

When we tried LoadFormat.Auto for CSV files, it failed hence we tried to load with all formats. But the given solution should work for us.

Thanks @John.He

@shivanandchikkalli
Thank you for your feedback. You are welcome. I’m glad you solved the issue with the suggested code. If you have any questions, please feel free to contact us at any time.

@shivanandchikkalli
We only can detect a few CSV file because it’s hard to find which character is delimiter for a Txt file with codes though we can simply find delimiter with eyes.

Please use FileFormatInfo fileFormatInfo = FileFormatUtil.DetectFileFormat(“your sample file”); to check the file format.
And if fileFormatInfo.LoadFormat is LoadFormat.Unknown, you also try to load it with LoadFormat.CSV