| Quick Start Guide | |
|---|---|
| Tutorial Concept |
Learn five ways to protect and secure Excel XLSX data in .NET WinForms applications using Spread.NET, a .NET spreadsheet. This tutorial covers workbook and worksheet protection, locked cells and ranges, runtime protection options, and password-protected Excel files using C# and VB.NET. |
| What You Will Need |
|
| Controls Referenced | |
Data security is a growing priority for .NET developers working with sensitive spreadsheet data, whether that data is being viewed and edited inside an application or exported to an Excel XLSX file. Once a workbook contains information such as budgets, employee records, financial data, or sales figures, developers may need to control who can open it, what users can modify, and which cells, worksheets, or workbook features remain protected. That can include locking specific cells and ranges, restricting worksheet or workbook changes, and requiring passwords before protected content can be edited or accessed.
Utilizing a .NET spreadsheet component, like Spread.NET, allows developers to easily protect Excel data both inside their applications and in exported workbooks. In this tutorial, we’ll cover five ways to protect the viewing and editing of Excel XLSX data in a WinForms .NET applications using C# and VB.NET, including workbook and worksheet protection, cell locking, passwords, and Excel-like protection dialogs.
5 Ways to Protect Spreadsheet Data in .NET Applications
- Protect and Unprotect an Excel Workbook
- Protect and Unprotect a Specific Excel Worksheet
- Add an Excel-Like Protect Sheet Dialog at Runtime
- Protecting Specific Cells, Ranges, and Worksheet Objects
- Password Protecting Excel Workbooks and Worksheets
Download the Latest Release of Spread.NET
1. Protect and Unprotect an Excel Workbook
To restrict your users from editing across an entire Excel workbook, use the Spread.NET Workbook Protect method.
The method accepts the WorkbookLocks enumeration, which determines which workbook elements are protected. Setting it to All protects the workbook structure and windows.
C#:
FpSpread fpSpread1 = new FpSpread(); // Initialize Spread.NET
fpSpread1.AsWorkbook().Protect(WorkbookLocks.All); // Protect the Excel Workbook
VB:
FpSpread fpSpread1 = new FpSpread() 'Initialize Spread.NET
fpSpread1.AsWorkbook().Protect(WorkbookLocks.All) 'Protect the Excel Workbook
To remove workbook protection, call the Unprotect method.
C#:
FpSpread fpSpread1 = new FpSpread(); // Initialize Spread.NET
fpSpread1.AsWorkbook().Unprotect(); // Unprotect the Excel Workbook
VB:
FpSpread fpSpread1 = new FpSpread() 'Initialize Spread.NET
fpSpread1.AsWorkbook().Unprotect() 'Unprotect the Excel Workbook
2. Protect and Unprotect a Specific Excel Worksheet
If users only need restricted access to certain sheets, you can protect individual worksheets instead of the entire workbook.
The Spread.NET Worksheet’s Protect method accepts the WorksheetLocks enumeration, which specifies the worksheet data and behaviors that should be protected.
C#:
fpSpread1.LegacyBehaviors = LegacyBehaviors.None;
fpSpread1.AsWorkbook().ActiveSheet.Protect(WorksheetLocks.All);
VB:
fpSpread1.LegacyBehaviors = LegacyBehaviors.None
fpSpread1.AsWorkbook().ActiveSheet.Protect(WorksheetLocks.All)
To remove protection from the active worksheet, invoke the sheet’s Unprotect method.
C#:
fpSpread1.LegacyBehaviors = LegacyBehaviors.None;
fpSpread1.AsWorkbook().ActiveSheet.Unprotect();
VB:
fpSpread1.LegacyBehaviors = LegacyBehaviors.None
fpSpread1.AsWorkbook().ActiveSheet.Unprotect()
3. Add an Excel-Like Protect Sheet Dialog at Runtime
Spread.NET also lets end users configure worksheet protection through an Excel-like Protect Sheet dialog during runtime. Users can select which worksheet behaviors to allow and optionally apply a password, giving them a familiar Excel-style experience directly inside the WinForms application.

Open the Protect Sheet Dialog from the Context Menu
To make the option available from the Tab Strip context menu, set the Tab Strip to editable.
C#:
fpSpread1.TabStrip.Editable = true;
VB:
fpSpread1.TabStrip.Editable = True
Open the Protect Sheet Dialog Programmatically
Developers can also invoke the dialog through the ProtectSheetDialog class and ShowDialog method.
C#:
private void OnProtectSheetMenuItemClick(object sender, System.EventArgs e)
{
if (_fpSpread.ActiveSheet == null)
return;
if (!_isEmptyPasswordProtected)
{
ProtectSheetDialog protectSheetDialog = new ProtectSheetDialog(_fpSpread);
protectSheetDialog.ShowDialog(this);
}
else
{
_fpSpread.AsWorkbook().ActiveSheet.Unprotect(string.Empty);
}
}
VB:
Private Sub OnProtectSheetMenuItemClick(ByVal sender As Object, ByVal e As System.EventArgs)
If _fpSpread.ActiveSheet Is Nothing Then Return
If Not _isEmptyPasswordProtected Then
Dim protectSheetDialog As ProtectSheetDialog = New ProtectSheetDialog(_fpSpread)
protectSheetDialog.ShowDialog(Me)
Else
_fpSpread.AsWorkbook().ActiveSheet.Unprotect(String.Empty)
End If
End Sub
This is useful when end users need control over worksheet protection without requiring developers to build a custom security interface.
4. Protect Specific Cells, Ranges, and Worksheet Actions
Protecting an entire worksheet is not always necessary. Many applications still need users to edit input cells while keeping formulas, calculated values, or other sensitive areas locked.
Because worksheet cells are locked by default, first unlock the worksheet and then apply the Locked property only to the cells or ranges that should be protected (or not be edited).
The following example unlocks the worksheet cells and then targets the range B2:D5.
C#:
FpSpread fpSpread1 = new FpSpread(); // Initialize Spread.NET
fpSpread1.LegacyBehaviors = LegacyBehaviors.None;
fpSpread1.TabStrip.Editable = true;
// Set style for locked cells
fpSpread1.ActiveSheet.LockBackColor = System.Drawing.Color.LightCyan;
fpSpread1.ActiveSheet.LockForeColor = System.Drawing.Color.Green;
// Using the Column Object unlock all the cells
FarPoint.Win.Spread.Column columnobj; // Column Object
int count = fpSpread1.ActiveSheet.Columns.Count - 1; // Get the column count
columnobj = fpSpread1.ActiveSheet.Columns[0, count]; // Get all of the columns in the worksheet
columnobj.Locked = false; // Unlock the cells
// Use the Cells Object to lock a specific cells or cell ranges
FarPoint.Win.Spread.Cell cellobj; // Cell Object
cellobj = fpSpread1.ActiveSheet.Cells["B2:D5"]; // Get a cell range
VB:
Dim fpSpread1 As FpSpread = New FpSpread() 'Initialize Spread.NET
fpSpread1.LegacyBehaviors = LegacyBehaviors.None
fpSpread1.TabStrip.Editable = True
'Set style for locked cells
fpSpread1.ActiveSheet.LockBackColor = System.Drawing.Color.LightCyan
fpSpread1.ActiveSheet.LockForeColor = System.Drawing.Color.Green
'Using the Column Object Unlock all the cells
Dim columnobj As FarPoint.Win.Spread.Column 'Column Object
Dim count As Integer = fpSpread1.ActiveSheet.Columns.Count - 1 'Get the column count
columnobj = fpSpread1.ActiveSheet.Columns(0, count) 'Get all of the columns in the worksheet
columnobj.Locked = False 'Unlock the cells
'Use the Cells Object to lock a specific cells or cell ranges
Dim cellobj As FarPoint.Win.Spread.Cell 'Cell Object
cellobj = fpSpread1.ActiveSheet.Cells("B2:D5") 'Get a cell range
cellobj.Value = "Locked Cells" 'Lock the cell range

Set Custom Worksheet Protection Options
You can also control which actions users are allowed to perform on a protected worksheet through WorksheetLocks. For example, the following setting prevents users from selecting locked cells.
C#:
fpSpread1.LegacyBehaviors = LegacyBehaviors.None;
fpSpread1.AsWorkbook().ActiveSheet.Protect(WorksheetLocks.SelectLockedCells);
VB:
fpSpread1.LegacyBehaviors = LegacyBehaviors.None
fpSpread1.AsWorkbook().ActiveSheet.Protect(WorksheetLocks.SelectLockedCells)
These protection options can also be configured by end users through the built-in Protect Sheet dialog.

5. Password Protect Excel Workbooks and Worksheets
For additional protection, Spread.NET supports passwords at both the workbook and worksheet levels.
Restrict Opening an Excel Workbook
A password can be applied when exporting an XLSX file so that users must provide it before opening the workbook.
Spread.NET supports opening and saving password-protected Excel files through the OpenExcel and SaveExcel methods.
C#:
fpSpread1.OpenExcel("D:\\tmp\\SalesInfo.xlsx", "password"); // Open password protected Excel file
fpSpread1.SaveExcel("D:\\tmp\\SalesInfo.xlsx", ExcelSaveFlags.UseOOXMLFormat, "password"); // Programmatically
VB:
fpSpread1.OpenExcel("D:\tmp\SalesInfo.xlsx", "password") 'Open password protected Excel file
fpSpread1.SaveExcel("D:\tmp\SalesInfo.xlsx", ExcelSaveFlags.UseOOXMLFormat, "password") 'Programmatically
When the file is opened in Excel, the user must enter the correct password before accessing its contents.

Restrict Workbook Modification
You can also password protect the workbook structure to prevent changes such as adding, removing, hiding, or unhiding worksheets.
C#:
fpSpread1.AsWorkbook().Protect(WorkbookLocks.All, "password");
VB:
fpSpread1.AsWorkbook().Protect(WorksheetLocks.All, "password")
When opening the modification-password-protected workbook, the user will be informed that the workbook is protected and cannot be changed.

Password Protect an Individual Worksheet
If only a specific worksheet needs protection, pass a password to its Protect method.
C#:
fpSpread1.LegacyBehaviors = LegacyBehaviors.None;
fpSpread1.AsWorkbook().ActiveSheet.Protect(WorksheetLocks.All, "password");
VB:
fpSpread1.LegacyBehaviors = LegacyBehaviors.None
fpSpread1.AsWorkbook().ActiveSheet.Protect(WorksheetLocks.All, "password")
With the password applied to a worksheet, users can view the data but not edit the content. If the workbook is opened using Excel and an edit is attempted, the user is notified that the sheet is protected and to enter the password to make changes:

Protect Excel Data in .NET Applications with Spread.NET
Spread.NET gives .NET developers several ways to protect Excel data using this .NET spreadsheet, from securing entire workbooks and worksheets to locking specific cells, applying passwords, and providing Excel-like protection options at runtime in their .NET applications.
Explore the Spread.NET documentation and download the latest Demo Explorer to learn more about its .NET spreadsheet protection features and other capabilities for building Excel-compatible experiences into WinForms applications.