如何在C#中设置Excel工作表密码

How to Set Password to Worksheet

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

Restricting a worksheet with a Read-Only authentication is a very common requirement for data files. IronXL makes it easy to apply Read-Only protection to worksheets in .NET.

Quickstart: Protect a Worksheet with One Line of Code

Using IronXL, you can make any worksheet read-only by calling the ProtectSheet method — just one line added is all it takes to instantly secure a sheet. Perfect for developers who want effortless protection in C#.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    new IronXL.WorkBook("data.xlsx").DefaultWorkSheet.ProtectSheet("MyPass123");
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer


Get started with IronXL

今天在您的项目中使用 IronXL,免费试用。

第一步:
green arrow pointer


Access a Password Protected Worksheet

IronXL allows you to access and modify any protected worksheet without requiring the password. Once the spreadsheet is opened with IronXL, you can modify any cell in any worksheet.

Apply Password to Worksheet

To restrict modifications to a worksheet while allowing users to view its content in Excel, use the ProtectSheet method with a password as a parameter. For example, workSheet.ProtectSheet("IronXL"). This sets a password-based ReadOnly authentication for the selected worksheet.

:path=/static-assets/excel/content-code-examples/how-to/set-password-worksheet-protect.cs
using IronXL;

WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set protection for selected worksheet
workSheet.ProtectSheet("IronXL");

workBook.Save();
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Set protection for selected worksheet
workSheet.ProtectSheet("IronXL")

workBook.Save()
$vbLabelText   $csharpLabel

Open a Password-Protected Worksheet

Access Protected Worksheet

Remove Password from Worksheet

To remove a password from a specific worksheet, you can use the UnprotectSheet method. Simply call workSheet.UnprotectSheet() to remove any password associated with the worksheet.

:path=/static-assets/excel/content-code-examples/how-to/set-password-worksheet-unprotect.cs
// Remove protection for selected worksheet. It works without password!
workSheet.UnprotectSheet();
' Remove protection for selected worksheet. It works without password!
workSheet.UnprotectSheet()
$vbLabelText   $csharpLabel

IronXL allows you to protect and unprotect any Excel workbook and worksheet with a single line of C# code.

常见问题解答

如何在C#中为Excel工作表设置密码?

您可以使用IronXL的ProtectSheet方法在C#中为Excel工作表设置密码。只需将密码作为参数传递给方法,例如,workSheet.ProtectSheet("IronXL")

是否可以在没有密码的情况下在C#中访问和修改密码保护的工作表?

是的,使用IronXL,一旦打开电子表格,您就可以在没有密码的情况下访问和修改密码保护的工作表。

如何使用C#从Excel工作表中删除密码?

要从Excel工作表中删除密码,请使用IronXL的UnprotectSheet方法。调用workSheet.UnprotectSheet()来清除与工作表关联的任何密码。

在C#中将密码保护应用到工作表所涉及的步骤有哪些?

首先,从NuGet下载IronXL C#库。然后,使用ProtectSheet方法在已打开的工作簿中将密码保护应用于选定的工作表。

如何在C#中将密码保护的工作表导出为不同格式?

IronXL允许您将密码保护的工作表导出为各种格式。在应用保护后,您可以使用SaveAs方法将电子表格导出为不同格式。

在C#中使用只读认证的优点是什么?

使用IronXL的只读认证可以限制修改,同时允许用户查看工作表内容。这是通过使用ProtectSheet方法与密码实现的。

如何开始在C#中使用IronXL进行工作表保护?

要开始使用IronXL进行工作表保护,您需要从NuGet下载并引用IronXL库到您的C#项目中。

我可以使用一行C#代码保护和取消保护Excel工作簿吗?

是的,IronXL允许您使用一行C#代码保护和取消保护任何Excel工作簿或工作表,使其成为管理工作表安全性的一种高效工具。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布