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 the 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 the 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

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy