We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback


Trusted by the world’s leading companies

Syncfusion Trusted Companies

Overview

The Syncfusion .NET Excel Library (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, it can prevent unnecessary data loss or modifications to Excel worksheets while also making them available to others with limited access.

.NET Security


How to encrypt an Excel workbook with a password using C#

The encryption option completely hides the worksheet data and workbook structure. To open an encrypted Excel document, a password that was used to encrypt it is needed. This feature ensures data security when sharing the document with other users.

Here is an example of how to protect an Excel workbook with a password using C#.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    IWorkbook workbook = application.Workbooks.Create(1);

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

    // Saving the workbook as a stream
    using (FileStream stream = new FileStream("Encrypt.xlsx", FileMode.Create, FileAccess.ReadWrite))
    {
        workbook.SaveAs(stream);
    }
}

How to decrypt an Excel workbook with a password using C#

The encryption can be removed by making PasswordToOpen of IWorkbook as empty.

Here is an example of how to decrypt an Excel workbook with a password using C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelParseOptions.Default, false, "password" );

    // Remove the encryption.
    workbook.PasswordToOpen = "";

    // Saving the workbook as a stream
    using (FileStream stream = new FileStream("Decrypt.xlsx", FileMode.Create, FileAccess.ReadWrite))
    {
        workbook.SaveAs(stream);
    }
}

How to protect a worksheet in Excel with a password using C#

Specific worksheets in the Excel document can be protected with a password by using the Protect method of IWorksheet.

Here is an example of how to protect the worksheet in Excel using C#.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(inputStream);
    IWorksheet sheet = workbook.Worksheets[0];
 
    // Protecting the Worksheet by using a Password
    sheet.Protect("password", ExcelSheetProtection.All);
 
    // Saving the workbook as a stream
    using (FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite))
    {
        workbook.SaveAs(stream);
    }
}

How to unprotect a worksheet in Excel with a password using C#

Worksheets can be unprotected with passwords by using the Unprotect method of IWorksheet.

Here is an example of how to unprotect the worksheet in Excel with a password using C#.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(inputStream);
    IWorksheet sheet = workbook.Worksheets[0];
 
    //Unprotect the workbook with a password.
    sheet.Unprotect("password");
 
    // Saving the workbook as a stream
    using (FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite))
    {
        workbook.SaveAs(stream);
    }
}

How to protect a workbook structure in Excel with a password using C#

The data in an Excel worksheet is secure when that specific worksheet is protected. The protect workbook option is used to protect an entire Excel workbook’s structure, such as sheet names, sheet orders, 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. Specific worksheets in the Excel document can be protected with passwords by using the Protect method of IWorkbook.

Here is an example of how to protect the worksheet in Excel using C#.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(inputStream);
    IWorksheet sheet = workbook.Worksheets[0];
 
    // Protecting the Worksheet by using a Password
    sheet.Protect("password", ExcelSheetProtection.All);
 
    // Saving the workbook as a stream
    using (FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite))
    {
        workbook.SaveAs(stream);
    }
}

How to unprotect a workbook structure in Excel with a password using C#

Workbook structure protection in Excel documents can be removed with passwords by using the Unprotect method of IWorkbook.

Here is an example of how to unprotect the workbook structure in Excel using C#.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(inputStream);
   
    //Unprotect the workbook with the password.
    workbook.Unprotect("password");
 
    // Saving the workbook as a stream
    using (FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite))
    {
        workbook.SaveAs(stream);
    }
}

How to unlock a specific Excel cell using C#

By default, all cells in a protected worksheet will be locked. Locked cells in a protected sheet cannot be edited. To edit the locked cells in a protected worksheet, you need to unlock specific cells. When the Locked property of CellStyle is set to FALSE, it is unlocked. Using this property, the cells can be locked or unlocked.

Here is an example of how to unlock a specific Excel cell using C#.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    using (FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read))
    {
        IWorkbook workbook = application.Workbooks.Open(inputStream);
        IWorksheet worksheet = workbook.Worksheets[0];
        
        // Unlocking a cell to edit in worksheet protection mode
        worksheet.Range["A1"].CellStyle.Locked = false;
 
        // Saving the workbook as a stream
        using (FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite))
        {
            workbook.SaveAs(stream);
        }
    }
}



Awards

Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion is proud to hold the following industry awards.

Scroll up icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon