【问题标题】:How to check whether it is password-protected excel file using EPPlus?如何使用 EPPlus 检查它是否是受密码保护的 excel 文件?
【发布时间】:2012-09-29 22:17:08
【问题描述】:

当我尝试打开安全文档时出现此异常

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文件是否有密码保护?

【问题讨论】:

    标签: c# excel epplus


    【解决方案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 指令将其嵌入到您的项目中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 2013-02-05
      • 1970-01-01
      • 2014-12-30
      相关资源
      最近更新 更多