5 Easy Ways to Protect Excel Data in C#
Tags: csharp, dotnet, excel
Published: 2021-12-23 11:52:16

---
title: 5 Easy Ways to Protect Excel Data in C#
published: true
date: 2021-12-23 11:00:45 UTC
tags: csharp, dotnet, excel
canonical_url: https://www.syncfusion.com/blogs/post/5-easy-ways-to-protect-excel-data-in-c.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99k3dkpp4xcnd9fdl728.png
---

The Syncfusion [.NET Excel Library](https://www.syncfusion.com/excel-framework/net "Link to .NET Excel Framework") (XlsIO) allows users to easily protect and unprotect from Excel files in C# and VB.NET with a variety of customization options. As a result, you can prevent unnecessary data loss or modifications to your Excel worksheets while also making them available to others with limited access. You can also use these features to protect a range of data that needs to be secured in a large Excel file.

In this blog post, we will see how to protect and unprotect Excel files using the various protection options available in our Excel Library with code examples.

**Note:** If you are new to our .NET Excel Library, it is highly recommended that you follow our [Getting Started guide](https://help.syncfusion.com/file-formats/xlsio/getting-started "Documentation link to Getting Started - Create Excel File in C# and VB.NET").

The .NET Excel Library provides the following protection options:

- [Open as read-only](#open-as-read-only).
- [Protect specific sheets](#protect-specific-sheets).
- [Protect workbook structure](#protect-workbook-structure).
- [Encryption](#encryption).
- [Mark as final](#mark-as-final).

Let’s explore them!

## Open as read-only

The open in [read-only](https://help.syncfusion.com/file-formats/xlsio/security#protect-workbook "Documentation link to protect workbook") mode in XlsIO provides only read access for the data in the Excel file. With this feature, we can restrict other users from editing the data. When you open a read-only Excel document, a message will appear to indicate this mode.

Refer to the following image.

![Excel Document in Read Only Mode](https://www.syncfusion.com/blogs/wp-content/uploads/2021/12/Read-Only-Mode.png)

Refer to the following code to make an Excel document read-only.

```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;

    FileStream input = new FileStream("../../../Data/InputData.xlsx", FileMode.Open, FileAccess.ReadWrite);

    //Open the Excel document.
    IWorkbook workbook = application.Workbooks.Open(input);

    //Set Read only.
    workbook.ReadOnlyRecommended = true;

    FileStream output = new FileStream("../../../Output/ReadOnlyOutput.xlsx", FileMode.Create, FileAccess.ReadWrite);

    //Save the document.
    workbook.SaveAs(output);
}
```

## Protect specific sheets

You may come across situations where a specific worksheet needs to be protected in an Excel document. In such cases, you can [protect one or more worksheets](https://help.syncfusion.com/file-formats/xlsio/security#protect-worksheet "Documentation link to Protect Worksheet") with a password along with a variety of customizations using our XlsIO library. Before getting into the customization options, we need to learn about locked cells in a worksheet.

### Locked cells

By default, all cells in a protected worksheet will be locked. [Locked cells](https://help.syncfusion.com/file-formats/xlsio/security#protect-cell "Documentation link to Protect Cell") in a protected sheet cannot be edited. To edit the locked cells in a protected worksheet, we need unlock specific cells.

Refer to the following code to unlock a specific cell in a protected worksheet.

```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;

    FileStream input = new FileStream("../../../Data/InputData.xlsx", FileMode.Open, FileAccess.ReadWrite);
 
    //Open the Excel document.
    IWorkbook workbook = application.Workbooks.Open(input);

    IWorksheet worksheet = workbook.Worksheets[0];

    //Unlock cell.
    worksheet["A1"].CellStyle.Locked = false;

    FileStream output = new FileStream("../../../Output/LockedCells.xlsx", FileMode.Create, FileAccess.ReadWrite);

    //Save the modified document.
    workbook.SaveAs(output);
}
```

The output of the above code will look like the following image.

![Locked and Hidden Cells Options](https://www.syncfusion.com/blogs/wp-content/uploads/2021/12/Format-Cells-Dialog.png)
<figcaption>Locked and Hidden Cells Options</figcaption>

### Custom sheet protection options

XlsIO offers the following customization options to allow users certain functionalities while protecting a worksheet:

- Select locked cells.
- Select unlocked cells.
- Format cells.
- Format columns.
- Format rows.
- Insert columns.
- Insert rows.
- Insert hyperlinks.
- Delete columns.
- Delete rows.
- Sort.
- Use Autofilter.
- Use pivot table and pivot chart.
- Edit objects.
- Edit scenarios.

![Custom Protection Options](https://www.syncfusion.com/blogs/wp-content/uploads/2021/12/Custom-Worksheet-Protection-Options.png)
<figcaption>Custom Protection Options</figcaption>

#### Select locked cells and Select unlocked cells

By default, both select locked cells and select unlocked cells options will be enabled while protecting a worksheet.

The following code example illustrates this.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect");
```

To enable the select locked cells option alone, you can use the following code.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.LockedCells);
```

To enable the select unlocked cells option alone, you can use the following code.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.UnLockedCells);
```

#### Format cells

This option allows you to modify the cell formatting when the worksheet is protected. You can modify the font name, font size, font color, cell background, and so on.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.FormattingCells);
```

#### Format columns

This option allows you to adjust the width of the columns in a protected worksheet.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.FormattingColumns);
```

#### Format rows

This option enables you to adjust the height of the rows in the protected worksheet.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.FormattingRows);
```

#### Insert columns

This option allows you to insert columns into the protected worksheet.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.InsertingColumns);
```

#### Insert rows

This option enables you to insert rows into the protected worksheet.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.InsertingRows);
```

#### Insert hyperlinks

This option allows you to add hyperlinks in the protected worksheet.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.InsertingHyperlinks);
```

#### Delete columns

The delete columns option allows you to delete columns in the worksheet.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.DeletingColumns);
```

#### Delete rows

The delete rows option allows you to delete rows in the worksheet.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.DeletingRows);
```

#### Sort

This option allows the end users to sort a specific range of cells in the worksheet, but the sorting range should not contain any locked cells. So, we should set the locked property value in these cells to false to enable sorting.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.Sorting);
```

#### Use Autofilter

Select the Autofilter option to use the auto filters that are already available in the worksheet. Remember, we cannot create new filters or remove the existing auto filters in the protected worksheet.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.Filtering);
```

#### Use pivot table and pivot chart

This option allows you to format, change the layout, refresh, or modify pivot table and pivot chart reports, or create new reports in the locked worksheet.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.UsingPivotTables);
```

#### Edit objects

This option allows you to format or edit shapes, text boxes, pictures, form controls, and charts. However, we can’t delete these objects when the worksheet is protected.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.Objects);
```

#### Edit scenarios

This option allows you to edit the scenarios that you have already protected and hidden. If the cells are not protected, then you can change the values in the cells and add new scenarios.

```csharp
IWorksheet worksheet = workbook.Worksheets[0];

worksheet.Protect("Protect", ExcelSheetProtection.Scenarios);
```

Refer to the following code example to protect an Excel worksheet with multiple options using C# in XlsIO.

```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;

    FileStream input = new FileStream("../../../Data/InputData.xlsx", FileMode.Open, FileAccess.ReadWrite);

    //Open the Excel document.
    IWorkbook workbook = application.Workbooks.Open(input);

    IWorksheet worksheet = workbook.Worksheets[0];

    //Protect the worksheet with multiple options.
    worksheet.Protect("Protect", ExcelSheetProtection.FormattingCells | ExcelSheetProtection.LockedCells |
        ExcelSheetProtection.UnLockedCells);
                
    FileStream output = new FileStream("../../../Output/ProtectedSheet.xlsx", FileMode.Create, FileAccess.ReadWrite);

    //Save the modified document.
    workbook.SaveAs(output);
}
```

After executing the above code, the Excel document will display a notification when you try to modify any of the cells in it. Refer to the following image.

![Protected Sheet Notification](https://www.syncfusion.com/blogs/wp-content/uploads/2021/12/Protect-Current-Sheet.png)
<figcaption>Protected Sheet Notification</figcaption>

### Unprotect sheet

We can easily [unprotect](https://help.syncfusion.com/file-formats/xlsio/security#protect-worksheet "Documentation link to unprotect worksheet") a protected Excel worksheet using the password that was used to protect it.

Refer to the following code.

```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;

    FileStream input = new FileStream("../../../Data/ProtectedWorksheet.xlsx", FileMode.Open, FileAccess.ReadWrite);

    //Open the Excel document.
    IWorkbook workbook = application.Workbooks.Open(input);

    IWorksheet worksheet = workbook.Worksheets[0];

    //UnProtect the worksheet with password.
    worksheet.Unprotect("syncfusion");

    FileStream output = new FileStream("../../../Output/UnProtectedSheet.xlsx", FileMode.Create, FileAccess.ReadWrite);

    //Save the modified document.
    workbook.SaveAs(output);
}
```

After executing the above code, all the features in the Excel worksheet will be enabled in the output document.

![Unprotecting an Excel Worksheet](https://www.syncfusion.com/blogs/wp-content/uploads/2021/12/Unprotect-Excel-Worksheet.png)
<figcaption>Unprotecting an Excel Worksheet</figcaption>

## Protect workbook structure

The data in an Excel worksheet is secure when that specific worksheet is protected. The [protect workbook](https://help.syncfusion.com/file-formats/xlsio/security#protect-workbook "Documentation link to Protect Workbook") option is used to protect an entire Excel workbook’s structure, such as sheet names, sheet order, and the ability access to insert or remove worksheets. This ensures that the structure of the workbook remains unmodified even though we can edit the data in the unprotected worksheets. XlsIO supports protecting a workbook structure with a password in C#.

Refer to the following code example.

```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;

    FileStream input = new FileStream("../../../Data/InputWorkbook.xlsx", FileMode.Open, FileAccess.ReadWrite);

    //Open the Excel document.
    IWorkbook workbook = application.Workbooks.Open(input);

    IWorksheet worksheet = workbook.Worksheets[0];

    //Protect the workbook structure and windows with a password. Here, we have provided true for protecting structure and windows.
    workbook.Protect(true, true, "syncfusion");

    FileStream output = new FileStream("../../../Output/ProtectedWorkbook.xlsx", FileMode.Create, FileAccess.ReadWrite);

    //Save the modified document.
    workbook.SaveAs(output);
}
```

After executing the above code, the worksheet insert and delete options will be disabled in the output document. Refer to the following image.

![Restricting Insert and Delete Options in a Protected Workbook](https://www.syncfusion.com/blogs/wp-content/uploads/2021/12/Protect-Workbook-Structure.png)
<figcaption>Restricting Insert and Delete Options in a Protected Workbook</figcaption>

### Unprotect workbook

Also, we can easily [unprotect a workbook](https://help.syncfusion.com/file-formats/xlsio/security#unprotect-workbook-elements "Documentation link to Unprotect Workbook elements") to edit its structure using our XlsIO library.

Refer to the following code to unprotect a workbook in C#.

```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;

    FileStream input = new FileStream("../../../Data/InputWorkbook.xlsx", FileMode.Open, FileAccess.ReadWrite);

    //Open the Excel document.
    IWorkbook workbook = application.Workbooks.Open(input);

    IWorksheet worksheet = workbook.Worksheets[0];

    //UnProtect the workbook with the password.
    workbook.Unprotect("syncfusion");

    FileStream output = new FileStream("../../../Output/UnProtectedWorkbook.xlsx", FileMode.Create, FileAccess.ReadWrite);

    //Save the modified document.
    workbook.SaveAs(output);
}
```

After executing the above code, the output document will look like the following image.

![Unprotected Excel Workbook](https://www.syncfusion.com/blogs/wp-content/uploads/2021/12/Unprotect-Excel-Workbook.png)
<figcaption>Unprotected Excel Workbook</figcaption>

## Encryption

Unlike worksheet and workbook protection, the [encryption](https://help.syncfusion.com/file-formats/xlsio/security#protect-workbook "Documentation link to Security in Excel (XlsIO) Library") option completely hides the worksheet data and workbook structure. To open an encrypted Excel document, we have to use the password that was used to encrypt it. This feature ensures data security when sharing the document with other users.

The data in an encrypted Excel document can be viewed or modified by authorized individuals alone. Our Syncfusion Excel Library supports encryption, so you can encrypt/decrypt Excel documents with a valid password.

### Encrypt workbook

When we encrypt an Excel document, the Excel data will be transformed into unreadable data with an encryption algorithm. The encrypting password is needed to decrypt the Excel document.

Refer to the following code to encrypt an Excel document with a password in C#.

```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;

    FileStream input = new FileStream("../../../Data/InputExcel.xlsx", FileMode.Open, FileAccess.ReadWrite);

    //Open the Excel document.
    IWorkbook workbook = application.Workbooks.Open(input);

    IWorksheet worksheet = workbook.Worksheets[0];

    //Encrypt the workbook with password.
    workbook.PasswordToOpen = "syncfusion";

    FileStream output = new FileStream("../../../Output/EncryptedWorkbook.xlsx", FileMode.Create, FileAccess.ReadWrite);

    //Save the modified document.
    workbook.SaveAs(output);
}
```

After executing the above code, you will be prompted for a password when trying to open the encrypted Excel document.

![Enter the Valid Password](https://www.syncfusion.com/blogs/wp-content/uploads/2021/12/Enter-the-Valid-Password.png)

After we enter the password, the output Excel document will look like the following image.

![Opened Encrypted Workbook](https://www.syncfusion.com/blogs/wp-content/uploads/2021/12/Encrypting-Excel-Workbook.png)
<figcaption>Opened Encrypted Workbook</figcaption>

### Decrypt workbook

Similarly, we can easily [decrypt a password-protected Excel](https://help.syncfusion.com/file-formats/xlsio/security#protect-workbook "Documentation link to Security in Excel (XlsIO) Library") document using XlsIO, but we need the password to first decrypt the document and then remove the encryption algorithm by making the password empty.

Refer to the following code to decrypt an Excel document with a password in C#.

```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;

    application.DefaultVersion = ExcelVersion.Xlsx;

    FileStream input = new FileStream("../../../Data/EncryptedWorkbook.xlsx", FileMode.Open, FileAccess.ReadWrite);

    //Open the encrypted Excel document with a password.
    IWorkbook workbook = application.Workbooks.Open(input, ExcelParseOptions.Default, false, "syncfusion");

    IWorksheet worksheet = workbook.Worksheets[0];

    //Decrypt the workbook.
    workbook.PasswordToOpen = string.Empty;

    FileStream output = new FileStream("../../../Output/DecryptedWorkbook.xlsx", FileMode.Create, FileAccess.ReadWrite);

    //Save the modified document.
    workbook.SaveAs(output);
}
```

## Mark as Final

The **Mark as Final** option helps us avoid further editing or reviewing in an Excel document. When we apply Mark as Final to an Excel document, we can’t edit, type, or mark within the Excel document anymore.

Refer to the following code example.

```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
FileStream input = new FileStream("../../../Data/InputData.xlsx", FileMode.Open, FileAccess.ReadWrite);

IWorkbook workbook = application.Workbooks.Open(input);

//Set workbook as final.
workbook.MarkAsFinal();

FileStream output = new FileStream("../../../Output/MarkAsFinalOutput.xlsx", FileMode.Create, FileAccess.ReadWrite);

//Save the modified document.
workbook.SaveAs(output);
}
```

After executing the above code, the output document will show a **marked as a final** message when we open it.

![Excel Workbook Marked as Final](https://www.syncfusion.com/blogs/wp-content/uploads/2021/12/Mark-As-Final-option.png)
<figcaption>Excel Workbook Marked as Final</figcaption>

## Github reference

Also, you can download the examples of [protect and unprotect Excel documents in C#](https://github.com/SyncfusionExamples/XlsIO "Link to protect and unprotect Excel documents in C# demos on GitHub") from GitHub.

## Wrapping up

Thanks for reading! As you can see, the Syncfusion [.NET Excel Library](https://www.syncfusion.com/excel-framework/net "Link to .NET Excel Framework") provides various easy ways to protect and unprotect Excel data in C#. Take a moment to peruse the [documentation](https://help.syncfusion.com/file-formats/xlsio/working-with-data#importing-data-to-worksheets "Documentation link to Importing Data to Worksheets") where you’ll find other options and features, all with accompanying code examples. Using this library, you can also export or write Excel data to [PDF](https://help.syncfusion.com/file-formats/xlsio/excel-to-pdf-conversion "Documentation link to Excel to PDF Conversion"), [image](https://help.syncfusion.com/file-formats/xlsio/worksheet-to-image-conversion "Documentation link to Worksheet to Image conversion in Excel Library"), [data table](https://help.syncfusion.com/file-formats/xlsio/working-with-data#exporting-from-worksheet-to-data-table "Documentation link to Exporting from Worksheet to Data Table"), [CSV](https://www.syncfusion.com/kb/9092/convert-excel-file-to-csv-in-c-vb-net "Link to Convert Excel file to CSV in C#, VB.NET"), [TSV](https://www.syncfusion.com/kb/8556/does-xlsio-support-tsv-files "Documentation link to XlsIO support to TSV files"), HTML, [collections of objects](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-collection-objects "Documentation link to Import Data from Collection Objects"), [ODS](https://help.syncfusion.com/file-formats/xlsio/excel-to-ods-conversion "Documentation link to Excel to ODS Conversion") file formats, and more.

Are you already a Syncfusion user? You can download the product setup [here](https://www.syncfusion.com/account/downloads/studio/ "Link to license and download page of Essential Studio"). If you’re not a Syncfusion user, you can download a free, [30-day trial](https://www.syncfusion.com/downloads/fileformats/confirm "Link to the free evaluation of the Syncfusion Essential Studio").

If you have any questions or require clarifications about these features, please let us know in the comments below. You can also contact us through our [support forum](https://www.syncfusion.com/forums "Link to the Syncfusion support forum"), [support portal,](https://support.syncfusion.com/ "Link to the Syncfusion support portal") or [feedback portal](https://www.syncfusion.com/feedback/ "Link to Syncfusion Feedback Portal"). We are always happy to assist you!

## Related blogs

- [How to Export Data from SQL Server to Excel Table in C#](https://dev.to/syncfusion/how-to-export-data-from-sql-server-to-excel-table-in-c-fo7 "Link to How to Export Data from SQL Server to Excel Table in C# blog")
- [Easy Steps to Export HTML Tables to an Excel Worksheet in C#](https://dev.to/syncfusion/easy-steps-to-export-html-tables-to-an-excel-worksheet-in-c-4je1 "Link to Easy Steps to Export HTML Tables to an Excel Worksheet in C# blog")
- [Export Data from Collection to Excel and Group It in C#](https://dev.to/karthickramasamy08/export-data-from-collection-to-excel-and-group-it-in-c-1j65-temp-slug-6071060 "Link to Export Data from Collection to Excel and Group It in C# blog")
- [Export Data to a Predefined Excel Template in C#](https://dev.to/sureshmohan/export-data-to-a-predefined-excel-template-in-c-dp6 "Link to Export Data to a Predefined Excel Template in C# blog")
- [6 Easy Ways to Export Data to Excel in C#](https://www.syncfusion.com/blogs/post/6-easy-ways-to-export-data-to-excel-in-c-sharp.aspx "Link to 6 Easy Ways to Export Data to Excel in C# blog")