Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Open and Save a Password Encrypted File

  • 2 minutes to read
In This Article

#Open a File

When SpreadsheetControl loads a password encrypted file and the WorkbookImportOptions.Password property is not set or contains an incorrect password, the end-user is prompted for a password with the following dialog:

EncryptedFilePasswordRequestForm

You can specify a password in code and not show the dialog, as illustrated in the code below.

The code snippet handles the SpreadsheetControl.EncryptedFilePasswordRequest event to specify a password for loading a password encrypted file.

View Example

private void SpreadsheetControl1_EncryptedFilePasswordRequest(object sender, EncryptedFilePasswordRequestEventArgs e) {
    if (e.DocumentName == "encrypted_test.xlsx") e.Password = "test";
    if (e.DocumentName == "corrupted.xlsx") e.Password = "000";
    e.Handled = true;
}

SpreadsheetControl can handle a situation when the encrypted file is corrupted.

This code snippet handles the SpreadsheetControl.EncryptedFileIntegrityCheckFailed event to perform certain actions when loading a corrupted password encrypted file.

View Example

private void SpreadsheetControl1_EncryptedFileIntegrityCheckFailed(object sender, EncryptedFileIntegrityCheckFailedEventArgs e) {
    e.Cancel = true;
    e.Handled = true;
    MessageBox.Show("File is corrupted. Cannot load.","Warning");
}

#Save a File

To save a password encrypted file, use the Encrypt Document dialog or specify the EncryptionOptions settings in code.

This code snippet obtains the DocumentSettings.Encryption settings selected in the editors and prompts for saving a password encrypted file.

View Example

IWorkbook workbook = spreadsheetControl1.Document;
workbook.DocumentSettings.Encryption.Type = (EncryptionType)Enum.Parse(typeof(EncryptionType), barEncryptionTypeComboBox.EditValue.ToString());
workbook.DocumentSettings.Encryption.Password = barPasswordEdit.EditValue.ToString();
spreadsheetControl1.SaveDocumentAs();