简体   繁体   English

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

[英]how to check Excel Workbook or sheet is password protected or not?

使用 vsto,C# fx 3.5,如何检查 Excel 工作簿或工作表是否受密码保护?

You can check if a workbook is password protected via the Workbook.HasPassword property.您可以通过Workbook.HasPassword属性检查工作簿是否受密码保护。 You can set the workbook password via the Workbook.SaveAs method:您可以通过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);
}

A worksheet can have it's cell contents protected, the drawing objects protected, and/or the scenarios protected.工作表可以保护其单元格内容、保护绘图对象和/或保护场景。 These can be checked via the Worksheet.ProtectContents , Worksheet.ProtectDrawingObjects , and Worsksheet.ProtectScenarios properties, respectively.这些可以分别通过Worksheet.ProtectContentsWorksheet.ProtectDrawingObjectsWorsksheet.ProtectScenarios属性进行检查。 I do not know of any way of testing if the worksheet is protected with a password or not, other than trying to call Worksheet.Unprotect , passing in an empty string for the password, and then seeing if the worksheet was successfully unprotected:我不知道有什么方法可以测试工作表是否受密码保护,除了尝试调用Worksheet.Unprotect ,传入一个空字符串作为密码,然后查看工作表是否已成功取消保护:

bool wasPasswordProtected;

try
{
    myWorksheet.Unprotect(string.Empty);

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

You can set the protection setting for the worksheet via the Worksheet.Protect method.您可以通过Worksheet.Protect方法设置工作表的保护设置。 The following example will protect the worksheet if any of the three protection elements are not set.如果三个保护元素中的任何一个未设置,以下示例将保护工作表。 It also sets a password and passes in the 'UserInterfaceOnly' argument as 'true', which means that only the user is blocked from editing the worksheet, while code such as VBA, VB.NET, or C# would not be prevented from manipulating the worksheet.它还设置密码并将“UserInterfaceOnly”参数传递为“true”,这意味着仅阻止用户编辑工作表,而不会阻止 VBA、VB.NET 或 C# 等代码操作工作表。 Setting 'UserInterfaceOnly' to 'false' would lock out all changes, whether made by the user or via code.将“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);

Note that the 'UserInterfaceOnly' does NOT persist when the workbook is saved and reverts to 'false' automatically when the workbook is closed.请注意,保存工作簿时“UserInterfaceOnly”不会持续存在,并在工作簿关闭时自动恢复为“false”。 If you wish it to be 'true' in all future sessions as well, the 'UserInterfaceOnly' setting must be re-applied by calling the 'Worksheet.Protect' method each time the workbook is open.如果您也希望它在以后的所有会话中都为“true”,则必须在每次打开工作簿时通过调用“Worksheet.Protect”方法来重新应用“UserInterfaceOnly”设置。 This can be be done by subscribing to the Workbook.Open event.这可以通过订阅Workbook.Open事件来完成。

You also might want to read the help files regarding the Worksheet.Protect method so you can understand the optional parameters, paying particular attention to the 'UserInterfaceOnly' parameter.您可能还想阅读有关Worksheet.Protect方法的帮助文件,以便了解可选参数,尤其要注意“UserInterfaceOnly”参数。

检查HasPassword属性。

If you want to check if an excel workbook is password protected checking the HasPassword property on a workbook object will not work, because you must open a workbook before you can check the properties, and you can't open a workbook that is password protected if you don't have the password.如果您想检查 Excel 工作簿是否受密码保护,检查工作簿对象上的 HasPassword 属性将不起作用,因为您必须先打开工作簿才能检查属性,并且无法打开受密码保护的工作簿,如果你没有密码。 See the problem?看到问题了吗?

This is the solution I found to know if the workbook has a password or not:这是我发现知道工作簿是否有密码的解决方案:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM