We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Can Add Sheets to Password Protected Workbook


I am working with various levels of password protection and Aspose.Cells .NET 18.5.1 (via NuGet).

I have an XLSX file that is password protected (PSWD: PEERNET) at the WORKBOOK level (not encrypted): ProtectWBStructurePassword_PEERNET_FilteredAndCommentedCells.zip (9.8 KB)

I protected this Workbook using the Protect Workbook option on the Review tab: ProtectWorkbook.png (10.2 KB)

When I open this file in Excel, and cannot add/delete/move any sheets in the protected workbook until I remove the protection. See image of worksheet tab context menu: SheetContextMenu.png (6.6 KB)

Using Aspose, I can detect that the workbook is protected but I can still add a new worksheet to the end of my collection. I would have expected that action to fail or throw an exception.

Here a code snippet showing what I am doing:

wBook = new Aspose.Cells.Workbook(FILE_PATH, lo);

// Try to unprotect
if (wBook.Settings.IsProtected && 
    (wBook.Settings.ProtectionType == Aspose.Cells.ProtectionType.All ||
     wBook.Settings.ProtectionType == Aspose.Cells.ProtectionType.Structure ||
     wBook.Settings.ProtectionType == Aspose.Cells.ProtectionType.Windows))
        TestContext.WriteLine(" ====>>>>   Workbook - called Unprotect with password {0} - current IsProtected: {1}",
                               (UnprotectPassword == null ? ("null") : String.IsNullOrEmpty(UnprotectPassword) ? "empty" : UnprotectPassword),
                                wBook.Settings.IsProtected );
    catch (Exception ex)
         TestContext.WriteLine(" ====>>>>   Workbook - Unprotect failed with password {0}",
                               (UnprotectPassword == null ? ("null") : String.IsNullOrEmpty(UnprotectPassword) ? "empty" : UnprotectPassword) );
         Assert.That(((Aspose.Cells.CellsException)ex).Code, Is.EqualTo(Aspose.Cells.ExceptionType.IncorrectPassword));

// This succeeds on a protected Workbook
Aspose.Cells.Worksheet addedSheet = wBook.Worksheets.Add("NUnut Added Sheet");
addedSheet.Cells["A1"].Value = "Nunit Added Sheet";

The above code of adding the sheet works on the protected XLSX file; my output PDF had the extra page at the end.

It successfully adds the new worksheet both when I don’t call Unprotect and when I do call Unprotect with an invalid password. When calling with an invalid password, I do get an InvalidPassword exception as expected but if I handle it and move on, it seems like other actions/methods on the workbook are not listening to the protection mode.




Thanks for the template file and sample code.

After an initial test,I am able to observe the issue as you mentioned by using your sample code with your template file. I found that Aspose.Cells still allows to add worksheets to a password protected workbook which it should not do. Even it throws exception when we use a wrong password for unprotecting workbook but it will also allow to add worksheets to the workbook. When we save to Excel file or PDF file format, the added worksheets are visible in the output file.

I have logged a ticket with an id “CELLSNET-46143” for your issue. We will look into it soon.

Once we have an update on it, we will let you know here.


Thanks for using Aspose APIs.

Aspose.Cells does not check whether the workbook is protected when adding worksheets because performance will be slower if we support these checks (such as: workbook’s protection, worksheet’s protection, cells’ validation etc.)

Please add such checks by yourself.