2012-09-29 93 views
0

當我試圖打開加密文檔我有這樣的例外如何使用EPPlus檢查是否有密碼保護的excel文件?

System.Exception : Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password 

我寫這篇硬方法測試

public bool HasPassword() 
     { 
      try 
      { 
       if(File.Exists(FileName)) 
       { 
        var fileStream = File.Open(FileName, FileMode.Append); 
        var package = new ExcelPackage(); 
        package.Load(fileStream); 
       } 
      } 
      catch(Exception) 
      { 
       return true; 
      } 
      return false; 
     } 

,但我認爲這是錯誤的做法。

如何檢查是否有密碼保護的Excel文件?

回答

1

如果EEPlus是不是強制性的,你可以簡單地使用Workbook.HasPassword屬性。

Workbook book = ****xyz****; 
if (book.HasPassword) 
{ 
    book.Password = Properties.Settings.Default.ExcelFilePW; 
    MessageBox.Show("Excel file is encrpyted"); 
} 

因此,您需要創建一個可以在.NET/COM(有時)中找到的Office Interop Excel組件的引用。 然後用using指令將它嵌入到你的項目中。

相關問題