簡體   English   中英

如何檢查 Excel 工作簿或工作表是否受密碼保護?

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

使用 vsto,C# fx 3.5,如何檢查 Excel 工作簿或工作表是否受密碼保護?

您可以通過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”參數。

檢查HasPassword屬性。

如果您想檢查 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM