如何检测文件格式并检查文件是否加密
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); |