using vsto, C# fx 3.5, how to check Excel Workbook or sheet is password protected or not?
使用vsto,C#fx 3.5,如何检查Excel工作簿或工作表是否受密码保护?
2 个解决方案
#2
11
You can check if a workbook is password protected via the Workbook.HasPassword property. You can set the workbook password via the Workbook.SaveAs method:
您可以通过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);
}
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. 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.ProtectContents,Worksheet.ProtectDrawingObjects和Worsksheet.ProtectScenarios属性进行检查。我不知道测试工作表是否受密码保护的任何方法,除了尝试调用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. 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. Setting 'UserInterfaceOnly' to 'false' would lock out all changes, whether made by the user or via code.
您可以通过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);
Note that the 'UserInterfaceOnly' does NOT persist when the workbook is saved and reverts to 'false' automatically when the workbook is closed. 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. This can be be done by subscribing to the Workbook.Open event.
请注意,“UserInterfaceOnly”在保存工作簿时不会保留,并在工作簿关闭时自动恢复为“false”。如果您希望在将来的所有会话中都为“true”,则每次打开工作簿时都必须通过调用“Worksheet.Protect”方法重新应用“UserInterfaceOnly”设置。这可以通过订阅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'参数。
#1
#2
11
You can check if a workbook is password protected via the Workbook.HasPassword property. You can set the workbook password via the Workbook.SaveAs method:
您可以通过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);
}
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. 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.ProtectContents,Worksheet.ProtectDrawingObjects和Worsksheet.ProtectScenarios属性进行检查。我不知道测试工作表是否受密码保护的任何方法,除了尝试调用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. 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. Setting 'UserInterfaceOnly' to 'false' would lock out all changes, whether made by the user or via code.
您可以通过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);
Note that the 'UserInterfaceOnly' does NOT persist when the workbook is saved and reverts to 'false' automatically when the workbook is closed. 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. This can be be done by subscribing to the Workbook.Open event.
请注意,“UserInterfaceOnly”在保存工作簿时不会保留,并在工作簿关闭时自动恢复为“false”。如果您希望在将来的所有会话中都为“true”,则每次打开工作簿时都必须通过调用“Worksheet.Protect”方法重新应用“UserInterfaceOnly”设置。这可以通过订阅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'参数。