.NET Excel Examples
Protect a Workbook with a Password Using the Syncfusion .NET Excel Library
Overview
The Syncfusion® .NET Excel Library (XlsIO) enables you to create, read, and edit Excel documents programmatically without Microsoft Excel or interop dependencies. Using this library, you can protect a workbook with a password using C#. After adding workbook protection, structural changes to the workbook are disabled.
Watch this video to see how to protect an Excel workbook with a password using the Syncfusion .NET Excel Library:
Protect a workbook with a password
Follow these steps to protect an Excel workbook with a password using the Syncfusion> .NET Excel Library.
Step 1: Create a new project
Start by creating a new C# Console Application project.
Step 2: Install the NuGet package
Add the Syncfusion.XlsIO.Net.Core package to your project from NuGet.org.
Step 3: Include namespaces
Add the following namespaces to your Program.cs file:
using System;
using System.IO;
using Syncfusion.XlsIO;Step 4: Add code to protect a workbook with a password
Use the following code in Program.cs to protect an Excel workbook with a password.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputWorkbook.xlsx"), FileMode.Open, FileAccess.ReadWrite);
//Open Excel file.
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];
//Protect workbook with password.
workbook.Protect(true, true, "syncfusion");
#region Save
//Saving the workbook.
FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/ProtectedWorkbook.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
//Dispose streams.
outputStream.Dispose();
inputStream.Dispose();
}NuGet installation
Get started quickly by downloading the installer and checking license information from the Downloads page.
Table of contents
Explore these resources for comprehensive guides, knowledge base articles, insightful blogs, and ebooks.
Learning
Technical support
