Excelファイルの暗号化

Microsoft Excel の使用

Microsoft Excel(ここではMicrosoft Excel 2003)でファイルの暗号化設定を行うには:

  1. ツールメニューからオプションを選択します。ダイアログが表示されます。
  2. セキュリティタブを選択します。
  3. パスワードを入力し、詳細をクリックします。
  4. 暗号化方式を選択し、パスワードを確認します。

Aspose.Cells を使用した暗号化

次の例は、Aspose.Cells APIを使用してExcelファイルを暗号化およびパスワード保護する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object.
// Open an excel file.
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Specify XOR encryption type.
workbook.SetEncryptionOptions(EncryptionType.XOR, 40);
// Specify Strong Encryption type (RC4,Microsoft Strong Cryptographic Provider).
workbook.SetEncryptionOptions(EncryptionType.StrongCryptographicProvider, 128);
// Password protect the file.
workbook.Settings.Password = "1234";
// Save the excel file.
workbook.Save(dataDir + "encryptedBook1.out.xls");

修正パスワードを指定するオプション

次の例は、Aspose.Cells APIを使用して既存のファイルの修正パスワードMicrosoft Excelオプションを設定する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object.
// Open an excel file.
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Set the password for modification.
workbook.Settings.WriteProtection.Password = "1234";
// Save the excel file.
workbook.Save(dataDir + "SpecifyPasswordToModifyOption.out.xls");

暗号化されたファイルのパスワードを確認します

Aspose.Cells for .NETは、暗号化されたファイルのパスワードを検証するためのVerifyPasswordメソッドを提供します。これらのメソッドは、ファイルストリームと検証する必要があるパスワードの2つのパラメータを受け入れます。 以下のコードスニペットは、提供されたパスワードが有効かどうかを確認するVerifyPasswordメソッドの使用を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create a Stream object
FileStream fstream = new FileStream(dataDir + "EncryptedBook1.xlsx", FileMode.Open);
bool isPasswordValid = FileFormatUtil.VerifyPassword(fstream, "1234");
Console.WriteLine("Password is Valid: " + isPasswordValid);

Aspose.Cells を使用して ODS ファイルの暗号化/復号化

Aspose.Cellsを使用すると、ODSファイルを暗号化および復号化することができます。復号されたODSファイルは、ExcelとOpenOfficeの両方で開くことができますが、暗号化されたODSファイルはOpenOfficeでのみパスワードを提供した後に開くことができます。Excelは暗号化されたODSファイルを開くことができず、警告メッセージが表示される場合があります。暗号化オプションは他のファイルタイプとは異なり、ODSファイルには適用されません。ODSファイルを暗号化するには、ファイルを読み込んでから保存する前に、実際のパスワードをWorkbookSettings.Passwordの値に設定します。出力された暗号化されたODSファイルはOpenOfficeのみで開くことができます。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Open an ODS file
Workbook workbook = new Workbook(sourceDir + "sampleODSFile.ods");
// Password protect the file
workbook.Settings.Password = "1234";
// Save the ODS file
workbook.Save(outputDir + "outputEncryptedODSFile.ods");

ODSファイルを復号化するには、LoadOptions.Passwordによるパスワードの提供でファイルを読み込みます。ファイルが読み込まれたら、WorkbookSettings.Password文字列をnullに設定します。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Open an encrypted ODS file
Aspose.Cells.LoadOptions loadOptions = new Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.Ods);
// Set original password
loadOptions.Password = "1234";
// Load the encrypted ODS file with the appropriate load options
Workbook workbook = new Workbook(sourceDir + "sampleEncryptedODSFile.ods", loadOptions);
// Set the password to null
workbook.Settings.Password = null;
// Save the decrypted ODS file
workbook.Save(outputDir + "outputDecryptedODSFile.ods");