Table of Contents

Protection and encryption

A sheet, a workbook's structure and ranges of a sheet are protected and unprotected as Excel's Review tab does it, with or without a password, and cells locked or their formulas hidden. The DocWright.Spreadsheet.Security package opens a password-encrypted workbook and saves one encrypted, as Excel's Encrypt with Password does. Measured against Excel both ways: what is protected or encrypted here Excel opens, lists and unlocks with the password, and the reverse.

dotnet add package DocWright.Spreadsheet
dotnet add package DocWright.Spreadsheet.Security   # for encryption only
Important

Protection is not security. A protected sheet is a flag for Excel's interface: anyone with the file can remove it, and DocWright edits a protected sheet unless the workbook is opened with XlsxReadSettings.HonorProtection. Only encryption keeps a workbook from being read without its password.

Read a workbook's protection

using DocWright.Spreadsheet;

XlsxDocument workbook = XlsxDocument.Open(File.OpenRead("protected.xlsx"));
foreach (XlsxDocumentSheet sheet in workbook.Sheets)
{
    Console.WriteLine($"{sheet.Name}: {sheet.Protection?.ToString() ?? "not protected"}");
    foreach (XlsxAllowEditRange range in sheet.AllowEditRanges)
    {
        Console.WriteLine($"  edit range {range}");
    }
}

Console.WriteLine($"structure protected: {workbook.IsStructureProtected}");

Protection is null for a sheet that is not protected; otherwise it says what the protected sheet still allows. A range with a password is open only to who knows it.

Unprotect, change and protect again

using DocWright.Spreadsheet;

XlsxDocumentSheet locked = workbook.FindSheet("Locked")!;
locked.Unprotect("sheet");                                            // the password it was protected with
locked.SetValue("A3", XlsxCellValue.Number(42m));
locked.SetStyle("B6", XlsxCellStyle.Default.WithLocked(false));      // stays editable once protected
locked.AllowEditRanges.Add("Notes", "D2:D9");
locked.Protect(new XlsxSheetProtection { AllowFormatCells = true, AllowSort = true }, "new password");
Console.WriteLine($"Locked: {locked.Protection}");

A password is hashed as Excel hashes it (SHA-512, a random salt, 100,000 spins) and never kept. A wrong one is an InvalidPasswordException. ProtectStructure and UnprotectStructure do the same for the workbook's structure: no sheet added, removed, moved, renamed or hidden in Excel.

XlsxSheetProtection Excel's Protect Sheet option
AllowSelectLockedCells, AllowSelectUnlockedCells Select locked / unlocked cells (on by default)
AllowFormatCells, AllowFormatColumns, AllowFormatRows Format cells / columns / rows
AllowInsertColumns, AllowInsertRows, AllowInsertHyperlinks Insert columns / rows / hyperlinks
AllowDeleteColumns, AllowDeleteRows Delete columns / rows
AllowSort, AllowAutoFilter, AllowPivotTables Sort, Use AutoFilter, Use PivotTable & PivotChart
AllowEditObjects, AllowEditScenarios Edit objects / scenarios

Open and save an encrypted workbook

using DocWright.Cryptography;
using DocWright.Spreadsheet;
using DocWright.Spreadsheet.Security;

using (FileStream output = File.Create("protected-encrypted.xlsx"))
{
    XlsxEncryption.Save(workbook, output, new AgileEncryptionSettings("open sesame"));
}

using FileStream input = File.OpenRead("protected-encrypted.xlsx");
XlsxDocument reopened = XlsxEncryption.Open(input, "open sesame");
Console.WriteLine($"opened with the password: A3 = {reopened.FindSheet("Locked")!.GetCell("A3").Value.NumberValue}");
Locked: protected with a password, allowing select locked, select unlocked, format cells, insert rows, sort, filter
  edit range Inputs (B2:C4, with a password)
  edit range Open (E2:E4)
Plain: protected, allowing select locked, select unlocked
Free: not protected
structure protected: True
Locked: protected with a password, allowing select locked, select unlocked, format cells, sort
opened with the password: A3 = 42

Save writes Excel's own encryption (agile: AES-256, SHA-512, 100,000 spins); agile and standard encryption are read. Each save draws a new salt and key. XlsxDocument.Open alone refuses an encrypted workbook with DocumentEncryptedException. Download the encrypted workbook (password open sesame): protected-encrypted.xlsx.