Skip to main content
A newer version of this page is available. .
.NET Framework 4.5.2+

SpreadsheetDecryptionException Class

Fires under certain conditions when an attempt to load a password-protected .xls file fails.

Namespace: DevExpress.XtraSpreadsheet

Assembly: DevExpress.Spreadsheet.v19.1.Core.dll


public class SpreadsheetDecryptionException :



The sample code which uses the SpreadsheetDecryptionException to load a password-protected file is obsolete. To load a password encrypted file, use the built-in dialog which prompts an end-user for a password, the WorkbookImportOptions.Password property and the ISpreadsheetComponent.EncryptedFilePasswordRequest event.


This example demonstrates the technique required to to open a password protected .xls file prompting a user for a correct password.

When the Spreadsheet control attempts to open a password protected .xls file and the XlsDocumentImporterOptions.Password property is not set, the control raises the SpreadsheetControl.InvalidFormatException event with the SpreadsheetDecryptionException exception in the event arguments. If this event is not handled, the control cancels loading without any alert.

The method is to subscribe to the InvalidFormatException event, prompt the user for a password, specify the password and call the SpreadsheetControl.LoadDocument.


This technique can be used for password-protected .xls documents created with Microsoft Excel versions up to 2010.

using DevExpress.Spreadsheet;
using DevExpress.XtraSpreadsheet;
using System.Windows.Forms;

namespace PasswordToOpenSample {
    public partial class Form1 : Form {
        string sourceUri;
        DocumentFormat sourceFormat;

        public Form1() {

        private void spreadsheetControl1_BeforeImport(object sender, DevExpress.Spreadsheet.SpreadsheetBeforeImportEventArgs e) {
            sourceUri = e.Options.SourceUri;
            sourceFormat = e.DocumentFormat;
        #region #decryptionexception
        private void spreadsheetControl1_InvalidFormatException(object sender, SpreadsheetInvalidFormatExceptionEventArgs e) {
            SpreadsheetDecryptionException decryptionException = e.Exception as SpreadsheetDecryptionException;
            if (decryptionException != null && sourceFormat == DocumentFormat.Xls) {
                if (decryptionException.Error == SpreadsheetDecryptionError.PasswordRequired) {
                    using (PasswordForm form = new PasswordForm()) {
                        if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                            spreadsheetControl1.Options.Import.Xls.Password = form.Password;
                            spreadsheetControl1.Options.Import.Xls.Password = string.Empty;
                else if (decryptionException.Error == SpreadsheetDecryptionError.WrongPassword) {
                    MessageBox.Show("Incorrect password", 
                        this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                else if (decryptionException.Error == SpreadsheetDecryptionError.EncryptionTypeNotSupported) {
                    MessageBox.Show("Unsupported encryption type.", 
                        this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
            else {
                MessageBox.Show(string.Format("Cannot open the file {0} because file format is not valid.", sourceUri),
                    this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
        #endregion #decryptionexception

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the SpreadsheetDecryptionException class.


The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.


See Also