你可以使用EPPlus,用它它的relativly容易,唯一的問題是要知道的是異常升高,因爲保護或別的東西,這裏是代碼:
public bool IsXlsxPasswordProtected(string fileName)
{
bool encrypted = false;
FileStream fs = new FileStream(fileName, FileMode.Open);
ExcelPackage pack = new ExcelPackage();
try
{
pack.Load(fs);
}
catch (Exception ex)
{
/// maybe there is better way to know if exception is because file is protected
/// with password, idealy EPP should raise dedicated exception type
/// e.g. PasswordProtectedException
if (ex.InnerException != null && ex.InnerException is FileFormatException)
encrypted = true;
else
throw;
}
return encrypted;
}