Excel Encryption for XLSX in C# and VB.NET

XLSX encryption enables you to protect the content of your Excel workbook from unwanted viewers by encrypting it with a password. The passwords are case-sensitive and can't be empty.

You can use the XlsxLoadOptions.IsEncrypted static methods to check if an Excel workbook is encrypted.

Location of encryption option in Microsoft Excel application

To read an encrypted Excel workbook, you'll need to provide a valid password using the XlsxLoadOptions.Password property. In case of an invalid password, GemBox.Spreadsheet won't be able to decrypt the file, and will throw a SecurityException when trying to load it.

To write an encrypted Excel workbook, all you need to do is provide a password using the XlsxSaveOptions.Password property.

The following example shows how to load an encrypted XLSX file and save it as an encrypted XLSX file with a different password in C# and VB.NET.

Encrypting and decrypting Excel file of XLSX format in C# and VB.NET
Screenshot of Excel workbook's encryption
Upload your file (Drag file here)
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var inputPassword = "%InputPassword%";
        var outputPassword = "%OutputPassword%";

        var workbook = ExcelFile.Load("%InputFileName%",
            new XlsxLoadOptions() { Password = inputPassword });

        workbook.Save("XLSX Encryption.xlsx",
            new XlsxSaveOptions() { Password = outputPassword });
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim inputPassword = "%InputPassword%"
        Dim outputPassword = "%OutputPassword%"

        Dim workbook = ExcelFile.Load("%InputFileName%",
            New XlsxLoadOptions With {.Password = inputPassword})

        workbook.Save("XLSX Encryption.xlsx",
            New XlsxSaveOptions With {.Password = outputPassword})

    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.