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