如何在 C# 中在 Excel 工作表上設置密碼

如何在 C# 工作表上設定密碼;。

This article was translated from English: Does it need improvement?
Translated
View the article in English

要在 C# 中使用密碼保護工作表,請使用 IronXL 的 ProtectSheet 方法,並加入密碼參數,例如 workSheet.ProtectSheet("MyPass123") 。 這會對任何 Excel 工作表套用唯讀保護,防止未經授權的修改,同時允許使用者檢視內容。

快速入門:用一行程式碼保護工作表

使用 IronXL,您可以透過呼叫 ProtectSheet 方法來使任何工作表成為唯讀 - 只要一行程式碼就能立即保護工作表。 非常適合希望在 C# 中輕鬆實現安全防護的開發人員。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronXL

    PM > Install-Package IronXL.Excel

  2. 複製並運行這段程式碼。

    new IronXL.WorkBook("data.xlsx").DefaultWorkSheet.ProtectSheet("MyPass123");
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronXL,免費試用!
    arrow pointer


開始使用 IronXL

!{--01001100010010010100001001010010010000010101001001011001010111110101001101010100010001010101010 10100010111110101010001010010010010010100000101001100010111110100001001001100010011111010000100100110001001111010101


如何存取受密碼保護的工作表? IronXL 可讓您存取和修改任何受保護的工作表,而無需密碼。 使用 IronXL 開啟電子表格後,您可以修改任何工作表中的任何儲存格。 This capability is particularly useful when you need to load existing spreadsheets that may have protection applied by other users or systems. 在使用受保護的工作表時,IronXL 會無縫處理底層安全性。 You can open Excel worksheets that are password-protected and perform operations like reading data, updating cells, or applying formulas without needing to know the original password. 這使得 IronXL.Excel 成為需要處理多個受保護檔案的自動化資料處理情境的絕佳選擇。

如何在工作表中應用密碼保護? 若要限制工作表的修改,同時允許使用者在 Excel 中查看其內容,請使用`ProtectSheet`方法,並將密碼作為參數。 例如, `workSheet.ProtectSheet("IronXL")` 。 這將為選定的工作表設定基於密碼的**唯讀身份**驗證。 ```csharp :path=/static-assets/excel/content-code-examples/how-to/set-password-worksheet-protect.cs ``` ### 保護多個工作表 在處理包含多個工作表的複雜工作簿時,您可能需要運用不同的保護策略。 以下是使用唯一密碼保護多個工作表的方法: ```csharp using IronXL; // Load the workbook WorkBook workBook = WorkBook.Load("financial-report.xlsx"); // Protect each worksheet with a different password workBook.GetWorkSheet("Summary").ProtectSheet("SummaryPass123"); workBook.GetWorkSheet("Details").ProtectSheet("DetailsSecure456"); workBook.GetWorkSheet("Charts").ProtectSheet("ChartsProtect789"); // Save the workbook with all protections applied workBook.SaveAs("protected-financial-report.xlsx"); ``` This approach is particularly useful when managing worksheets that contain different levels of sensitive information. You can also combine worksheet protection with workbook-level password protection for enhanced security.

當使用者嘗試開啟受保護的工作表時會發生什麼? Code editor showing Excel worksheet protection implementation with IronXL library and file explorer displaying .xlsx files 當使用者嘗試修改 Excel 中受保護的工作表時,系統會提示他們輸入密碼。 如果沒有正確的密碼,他們只能檢視內容,但無法進行任何變更。 此保護在不同版本的 Excel 及其他支援 Excel 格式的試算表應用程式中仍然有效。 ### 在不同的情況下使用受保護的工作表。 IronXL.Excel 的工作表保護功能可與其他 Excel 作業無縫整合。 You can still perform read operations, extract data, and even convert the file to different formats while maintaining the protection status. ```csharp using IronXL; // Load a workbook and protect specific worksheets based on content WorkBook workBook = WorkBook.Load("employee-data.xlsx"); foreach (WorkSheet sheet in workBook.WorkSheets) { // Check if the sheet name contains sensitive keywords if (sheet.Name.Contains("Salary") || sheet.Name.Contains("Personal")) { // Apply stronger password protection to sensitive sheets sheet.ProtectSheet($"Secure_{sheet.Name}_2024!"); } else { // Apply standard protection to other sheets sheet.ProtectSheet("StandardProtection"); } } // Save the selectively protected workbook workBook.SaveAs("employee-data-protected.xlsx"); ```

如何從工作表中移除密碼保護? 若要移除特定工作表的密碼,請使用 `UnprotectSheet` 方法。 只需呼叫`workSheet.UnprotectSheet()`即可移除與工作表關聯的任何密碼。 ```csharp :path=/static-assets/excel/content-code-examples/how-to/set-password-worksheet-unprotect.cs ``` ### 批量解除工作表的保護 在處理多個受保護的工作表時,您可能需要一次移除所有工作表的保護。 以下是一種有效的方法: ```csharp using IronXL; using System; // Load the protected workbook WorkBook workBook = WorkBook.Load("multi-protected.xlsx"); // Counter for tracking operations int unprotectedCount = 0; // Iterate through all worksheets and remove protection foreach (WorkSheet sheet in workBook.WorkSheets) { try { sheet.UnprotectSheet(); unprotectedCount++; Console.WriteLine($"Unprotected: {sheet.Name}"); } catch (Exception ex) { Console.WriteLine($"Failed to unprotect {sheet.Name}: {ex.Message}"); } } Console.WriteLine($"Successfully unprotected {unprotectedCount} worksheets"); // Save the unprotected workbook workBook.SaveAs("multi-unprotected.xlsx"); ``` ## 工作表保護的最佳實務 在您的 C# 應用程式中實施工作表保護時,請考慮這些建議: 1.**使用強密碼**:產生結合字母、數字和特殊字符的複雜密碼。 考慮使用密碼管理器或安全的儲存空間來管理多個工作表密碼。 2.**文件保護狀態**:維護哪些工作表受到保護及其原因的記錄。 這有助於維護和故障排除。 3. **Combine with License Management**: When distributing protected Excel files, ensure you have properly configured your IronXL license for deployment scenarios. 4.**測試保護情境**:在部署受保護的工作表之前,請使用各種 Excel 版本進行測試,以確保相容性。 5.**考慮效能**:雖然保護不會對效能造成重大影響,但在大型工作簿中處理許多受保護的工作表可能需要優化策略。 ## 進階保護情境 IronXL 的工作表保護可以整合到更複雜的工作流程中。 For instance, you can create new spreadsheets with pre-configured protection settings: ```csharp using IronXL; using System; // Create a new workbook with protected templates WorkBook workBook = WorkBook.Create(); // Add and configure protected worksheets WorkSheet budgetSheet = workBook.CreateWorkSheet("Budget2024"); budgetSheet["A1"].Value = "Annual Budget"; budgetSheet["A2"].Value = "Department"; budgetSheet["B2"].Value = "Allocated Amount"; // Add more data... budgetSheet.ProtectSheet("BudgetProtect2024"); WorkSheet forecastSheet = workBook.CreateWorkSheet("Forecast"); forecastSheet["A1"].Value = "Revenue Forecast"; // Add forecast data... forecastSheet.ProtectSheet("ForecastSecure123"); // Save the protected workbook workBook.SaveAs("protected-templates.xlsx"); ``` For comprehensive Excel file manipulation capabilities, explore the complete IronXL documentation or check out tutorials on reading Excel files to expand your Excel automation toolkit. IronXL allows you to protect and unprotect any Excel workbook and **worksheet** with a single line of C# code.

常見問題解答

如何在 C# 中為 Excel 工作表提供密碼保護?

您可以使用 IronXL.Excel 的 ProtectSheet 方法在 C# 中對 Excel 工作表進行密碼保護。只需在任何工作表物件上呼叫 workSheet.ProtectSheet("YourPassword")。這會套用唯讀保護,防止未經授權的修改,同時允許使用者檢視內容。

我可以在不知道密碼的情況下存取和修改受密碼保護的工作表嗎?

是的,IronXL 允許您存取和修改任何受保護的工作表,而不需要原始密碼。一旦使用 IronXL 開啟試算表,您就可以修改任何工作表中的任何儲存格,因此非常適合需要處理多個受保護檔案的自動化資料處理情境。

ProtectSheet 方法適用於哪種類型的保護?

IronXL 中的 ProtectSheet 方法會對選取的工作表套用唯讀驗證。這表示使用者可以檢視內容,但在 Excel 中開啟檔案時,如果沒有輸入正確的密碼,則無法進行修改。

我可以使用不同的密碼保護多個工作表嗎?

是的,IronXL 允許您使用唯一密碼保護多個工作表。您可以遍歷工作簿中的工作表,並使用 ProtectSheet 方法為每個工作表套用不同的密碼,例如 workBook.GetWorkSheet("Summary").ProtectSheet("SummaryPass123")。

用代碼保護 Excel 工作表的最簡單方法是什麼?

最簡單的方法是使用 IronXL 的單行程式碼方法:new IronXL.WorkBook("data.xlsx").DefaultWorkSheet.ProtectSheet("MyPass123")。這可立即以密碼保護預設工作表。

密碼保護會影響將工作表匯出成不同格式的能力嗎?

不,IronXL 中的密碼保護並不阻止您匯出工作表至不同的試算表格式。您仍然可以在套用密碼保護後,將受保護的工作表儲存並匯出為各種 Excel 格式。

Chaknith Bin
軟體工程師
Chaknith 在 IronXL 和 IronBarcode 上工作。他對 C# 和 .NET 擁有深厚的專業知識,幫助改進了軟體並支持客戶。他從用戶互動中得到的見解有助於改善產品、文檔和整體體驗。
準備好開始了嗎?
Nuget 下載 1,765,830 | 版本: 2025.12 剛發表