Back to Devexpress

SpreadsheetDecryptionException Class

officefileapi-devexpress-dot-xtraspreadsheet-0d1e3abc.md

latest9.3 KB
Original Source

SpreadsheetDecryptionException Class

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

Namespace : DevExpress.XtraSpreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
public class SpreadsheetDecryptionException :
    Exception
vb
Public Class SpreadsheetDecryptionException
    Inherits Exception

Remarks

Note

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.

Example

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.

Note

You can use this technique for password-protected .xls documents created with Microsoft Excel versions up to 2010.

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

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

        public Form1() {
            InitializeComponent();
            spreadsheetControl1.BeforeImport+=spreadsheetControl1_BeforeImport;
            spreadsheetControl1.InvalidFormatException+=spreadsheetControl1_InvalidFormatException;
        }

        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.LoadDocument(sourceUri);
                            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
    }
}
vb
Imports DevExpress.Spreadsheet
Imports DevExpress.XtraSpreadsheet
Imports System.Windows.Forms

Namespace PasswordToOpenSample
    Partial Public Class Form1
        Inherits Form

        Private sourceUri As String
        Private sourceFormat As DocumentFormat

        Public Sub New()
            InitializeComponent()
            AddHandler spreadsheetControl1.BeforeImport, AddressOf spreadsheetControl1_BeforeImport
            AddHandler spreadsheetControl1.InvalidFormatException, AddressOf spreadsheetControl1_InvalidFormatException
        End Sub

        Private Sub spreadsheetControl1_BeforeImport(ByVal sender As Object, ByVal e As DevExpress.Spreadsheet.SpreadsheetBeforeImportEventArgs)
            sourceUri = e.Options.SourceUri
            sourceFormat = e.DocumentFormat
        End Sub
#Region "#decryptionexception"
        Private Sub spreadsheetControl1_InvalidFormatException(ByVal sender As Object, ByVal e As SpreadsheetInvalidFormatExceptionEventArgs)
            Dim decryptionException As SpreadsheetDecryptionException = TryCast(e.Exception, SpreadsheetDecryptionException)
            If decryptionException IsNot Nothing AndAlso sourceFormat = DocumentFormat.Xls Then
                If decryptionException.Error = SpreadsheetDecryptionError.PasswordRequired Then
                    Using form As New PasswordForm()
                        If form.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                            spreadsheetControl1.Options.Import.Xls.Password = form.Password
                            spreadsheetControl1.LoadDocument(sourceUri)
                            spreadsheetControl1.Options.Import.Xls.Password = String.Empty
                        End If
                    End Using
                ElseIf decryptionException.Error = SpreadsheetDecryptionError.WrongPassword Then
                    MessageBox.Show("Incorrect password", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)
                ElseIf decryptionException.Error = SpreadsheetDecryptionError.EncryptionTypeNotSupported Then
                    MessageBox.Show("Unsupported encryption type.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)
                End If
            Else
                MessageBox.Show(String.Format("Cannot open the file {0} because file format is not valid.", sourceUri), Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)
            End If
        End Sub
#End Region ' #decryptionexception
    End Class
End Namespace
csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PasswordToOpenSample {
    public partial class PasswordForm : Form {
        public PasswordForm() {
            InitializeComponent();
        }

        public string Password {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }
    }
}
vb
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Namespace PasswordToOpenSample
    Partial Public Class PasswordForm
        Inherits Form

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Property Password() As String
            Get
                Return textBox1.Text
            End Get
            Set(ByVal value As String)
                textBox1.Text = value
            End Set
        End Property
    End Class
End Namespace

Implements

ISerializable

_Exception

Inheritance

Object Exception SpreadsheetDecryptionException

See Also

SpreadsheetDecryptionException Members

How to: Open and Save a Password Encrypted File

DevExpress.XtraSpreadsheet Namespace