Excel Encryption for XLSX

Below you can find an example that demonstrates how to load an encrypted XLSX file and then save it as an encrypted XLSX file using a different password in both C# and VB.NET, using the GemBox.Spreadsheet library.

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
Encrypting and decrypting Excel file of XLSX format in C# and VB.NET
Screenshot of Excel workbook's encryption

To access 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 CryptographicException 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.

Note that 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.

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