ファイル形式を検出し、ファイルが暗号化されているかどうかを確認する方法
Contents
[
Hide
]
ファイル拡張子はファイルの内容が適切であるとは限らないため、ファイルを開く前にファイルの形式を検出する必要がある場合があります。ファイルは暗号化されている可能性があり (パスワードで保護されたファイル)、直接読み取ることができないか、読み取るべきではありません。 Aspose.Cells はFileFormatUtil.DetectFileFormat()ドキュメントを処理するために使用できる静的メソッドといくつかの関連 API が含まれています。
次のサンプル コードは、(ファイル パスを使用して) ファイル形式を検出し、その拡張子を確認する方法を示しています。ファイルが暗号化されているかどうかを判断することもできます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
//Detect file format | |
FileFormatInfo info = FileFormatUtil.DetectFileFormat(dataDir + "Book1.xlsx"); | |
//Gets the detected load format | |
Console.WriteLine("The spreadsheet format is: " + FileFormatUtil.LoadFormatToExtension(info.LoadFormat)); | |
//Check if the file is encrypted. | |
Console.WriteLine("The file is encrypted: " + info.IsEncrypted); |