---
title: "Protect an Excel worksheet with a password in C#"
url: https://recursiveai.org/onedocument/cookbook/protect-excel-worksheet/
description: "Restrict editing on an Excel worksheet while leaving its contents readable."
version: "1.0.0"
---

Restrict editing on an Excel worksheet while leaving its contents readable.

```shell
dotnet add package \
  RecursiveAI.OneDocument.Spreadsheets
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneDocument.Spreadsheets;

using var book = Workbook.Open("input.xlsx");
book.Sheets["Data"].Protection.Protect(
    password: "sample");
book.SaveAs("protected.xlsx");
```

Writes `protected.xlsx`; the snippet prints nothing.

Unzip [the example](https://recursiveai.org/onedocument/cookbook/protect-excel-worksheet/protect-excel-worksheet.zip), then `dotnet run`.

## Full program

`input.xlsx` has a `Data` sheet with these cells:

```text
A1: Customer   B1: Total
A2: Ada        B2: 42
A3: Lin        B3: 18
```

```csharp
using RecursiveAI.OneDocument.Spreadsheets;

using var book = Workbook.Open("input.xlsx");
book.Sheets["Data"].Protection.Protect(
    password: "sample");
book.SaveAs("protected.xlsx");
Console.WriteLine(book.Sheets["Data"].Protection.Verify("sample"));
```

`dotnet run` prints:

```text
Match
```

Protection is not encryption: the content stays readable and restrictions bind only applications that honor them. Chart and macro sheets throw `NotSupportedException` on `Protect`.

## Variations

### Allow sorting and selection of unlocked cells

```csharp
using RecursiveAI.OneDocument.Spreadsheets;

using var book = Workbook.Open("input.xlsx");
book.Sheets["Data"].Protection.Protect(
    SheetActions.Sort |
    SheetActions.SelectUnlockedCells, "sample");
book.SaveAs("protected.xlsx");
```

Writes `protected.xlsx` with sorting and unlocked-cell selection allowed. It prints nothing.

### Remove protection with the password

After running the full program:

```csharp
using RecursiveAI.OneDocument.Spreadsheets;

using var book = Workbook.Open("protected.xlsx");
Console.WriteLine(
    book.Sheets["Data"].Protection.TryRemove("sample"));
book.SaveAs("unprotected.xlsx");
```

```text
Match
```

Writes `unprotected.xlsx` after the password matches.

## Related recipes

- [Create an Excel file](https://recursiveai.org/onedocument/cookbook/create-excel-file/)
- [Read an Excel file](https://recursiveai.org/onedocument/cookbook/read-excel-file/)
- [Fill a Word template](https://recursiveai.org/onedocument/cookbook/fill-word-template/)

## The short answer

Open the workbook, call `Worksheet.Protection.Protect` with a password, and save it as XLSX. The password controls lifting an editing restriction, not opening the file. This distinction matters more than the choice of password: sheet protection is not a way to hide salaries, customer data or other confidential information.

The example uses the default allowed actions, selecting locked and unlocked cells. The argument describes what a user **may** do, not what you want to prohibit. The [protection documentation](https://recursiveai.org/onedocument/docs/protection/) explains how those flags map to the stored XML.

## How do I make my Excel sheet password protected?

`book.Sheets["Data"]` selects the worksheet named Data, not every worksheet in the workbook. Repeat the operation for other worksheets if each needs its own restrictions. The sample contains a `Data` worksheet with two customer totals; its contents remain readable after protection.

`Protect` replaces the sheet’s existing protection settings. Decide which actions should remain available before overwriting settings in a supplied workbook. `SheetActions` includes actions such as sorting and formatting cells. The variation sets the entire allowed-action list; it does not add two flags to the previous list.

Saving persists the metadata to `protected.xlsx`; setting protection without saving only changes the open model. The full program prints `Match` because `Verify` compares the supplied password with the saved restriction’s verifier in the model. It does not launch Excel or demonstrate an interactive password prompt. Not yet tested in desktop Excel.

## What’s the difference between protect sheet and protect workbook?

Worksheet protection controls actions on a worksheet. Workbook protection controls structure and windows through `Workbook.Protection.Protect(lockStructure, lockWindows, password)`. Neither encrypts the file’s contents. A workbook-level restriction is therefore not a stronger version of worksheet confidentiality; it is a restriction on a different set of operations.

A write-reservation password is another separate setting, exposed by `Workbook.FileSharing`. If the requirement is that an unauthorized reader cannot inspect values at all, this recipe does not meet it. Use file encryption rather than editing restrictions.

## How do I remove password protection from an Excel file?

For these worksheet editing restrictions, `TryRemove(password)` removes protection only when verification matches. `Remove()` lifts the restriction without checking a password, leaving authorization to your application. Do not expose that operation to users merely because it is available in the library.

`Verify` returns `Match`, `Mismatch` or `UnsupportedAlgorithm`. An unsupported verifier is not a wrong password; it means the library cannot compute that stored algorithm. A restriction with no stored password matches any input. Reading the workbook never requires the sheet-protection password because the values are not encrypted.

Keep XLSX output when you need the modern protection metadata described here. The documented XLS conversion loses the modern hash and allowed-action list, and the binary writers do not carry every workbook-level protection setting.
