如何检查Excel工作簿或工作表是否受到密码保护?

3

使用VSTO和C# fx 3.5,如何检查Excel工作簿或工作表是否已加密?

3个回答

12
你可以通过Workbook.HasPassword属性检查工作簿是否受密码保护。你可以通过Workbook.SaveAs方法设置工作簿密码:
Excel.Workbook myWorkbook = ...;

if (!myWorkbook.HasPassword)
{
   excelWorkbook.Application.DisplayAlerts = false;

   excelWorkbook.SaveAs(
        excelWorkbook.Name,
        Type.Missing,
        "My Password",
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing,
        Type.Missing);
}

一个工作表可以保护其单元格内容、图形对象和/或方案。这些可以通过Worksheet.ProtectContentsWorksheet.ProtectDrawingObjectsWorsksheet.ProtectScenarios属性进行检查。我不知道有什么方法可以测试工作表是否受密码保护,除非尝试调用Worksheet.Unprotect,将空字符串作为密码传递,并查看工作表是否成功取消保护:

bool wasPasswordProtected;

try
{
    myWorksheet.Unprotect(string.Empty);

    // Unprotect suceeded:
    wasPasswordProtected = false;  
}
catch
{
    // Unprotect failed:
    wasPasswordProtected = true;
}

您可以通过Worksheet.Protect方法设置工作表的保护设置。以下示例将在任何三个保护元素未设置的情况下保护工作表。它还设置了密码,并将“UserInterfaceOnly”参数传递为“true”,这意味着仅阻止用户编辑工作表,而VBA、VB.NET或C#等代码不会被防止操纵工作表。将“UserInterfaceOnly”设置为“false”将锁定所有更改,无论是由用户还是通过代码进行的。

if(!myWorksheet.ProtectContents ||
   !myWorksheet.ProtectDrawinngObjects ||
   !myWorsksheet.ProtectScenarios)
{
    string myPassword = "...";

    bool protectContents = true;
    bool protectDrawingObjects = true;
    bool protectScenarios = true;

    bool userInterfaceOnly = true;

    myWorksheet.Protect(
        myPassword,
        protectDrawingObjects,
        protectContents, 
        protectScenarios,
        userInterfaceOnly,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing,    
        Type.Missing);

请注意,当工作簿保存并关闭时,“UserInterfaceOnly”不会持久化,并自动恢复为“false”。如果您希望在所有未来的会话中都保持为“true”,则必须每次打开工作簿时通过调用“Worksheet.Protect”方法重新应用“UserInterfaceOnly”设置。这可以通过订阅Workbook.Open事件来完成。
您还可能希望阅读有关Worksheet.Protect方法的帮助文件,以便了解可选参数,特别是“UserInterfaceOnly”参数。

Mike,对于处理使用空密码保护的工作表有什么想法吗? - Jpad Solutions

3

2
我看到你现在已经将这个标记为答案,shahjapan,但请注意,这只检查工作簿保护,而不是工作表保护。请参阅下面的我的答案以获取更多详细信息。 - Mike Rosenblum
2
你能发一个代码示例吗?要读取HasPassword属性,我需要先打开wb。但是,为了正确地打开它,我需要知道它是否有密码。 - Laser42
你如何在没有密码的情况下打开文档以检查它是否有密码?我正在尝试编程忽略受保护的文档,因为我无法打开它们进行处理,但是没有办法在不先打开它的情况下查看它是否是受保护的文档。这似乎是一个毫无用处的属性...如果我必须使用密码才能进入,那么我已经知道该文档有密码了。 - DiggyJohn

2
如果您想检查Excel工作簿是否受密码保护,检查工作簿对象上的HasPassword属性将不起作用,因为您必须打开工作簿才能检查属性,如果您没有密码,则无法打开受密码保护的工作簿。看到问题了吗?
这是我发现的解决方案,可知道工作簿是否有密码:
using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlsApp = new Excel.Application();
xlsApp.DisplayAlerts = false;

Excel.Workbooks wkbs = xlsApp.Workbooks;
Excel.Workbook wkb;


try
{
    wkb = wkbs.Open(path, ReadOnly: true, Password: "");
    //If you don't send a string for the password, it will popup a window
    //asking for the password and halt your program. If the workbook has no
    //password, it will open just fine.

}
catch (Exception ex)
{
    //If the file is password protected or otherwise unreadable, it will throw an exception.
}


wkb.Close(false);
xlsApp.Quit();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接