如何在 C# 中为工作簿设置密码

How to Set a Password to Workbook

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

Ensuring that information or data is shared with the correct individual is crucial for maintaining proper authority. With IronXL, you can create password-protected spreadsheets and secure each individual worksheet.

Quickstart: Encrypt a Workbook Password with IronXL

In just one simple step, IronXL lets developers encrypt an Excel workbook—no Interop, no fuss. Use the Encrypt method with your password and save the file to immediately protect your workbook.

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.

    var wb = WorkBook.Load("input.xlsx"); wb.Encrypt("MyStrongPass"); wb.SaveAs("input.xlsx");
  3. Deploy to test on your live environment

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


Access a Password-Protected Workbook

A protected spreadsheet can be opened by providing the password as the second parameter to the Load method. For example: WorkBook.Load("sample.xlsx", "IronSoftware").

请注意It is not possible to open a protected spreadsheet without the correct password

Apply a Password to Workbook

To password-protect a spreadsheet, use the Encrypt method as shown in the code below:

:path=/static-assets/excel/content-code-examples/how-to/set-password-workbook-protect.cs
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Open protected spreadsheet file
WorkBook protectedWorkBook = WorkBook.Load("sample.xlsx", "IronSoftware");

// Set protection for spreadsheet file
workBook.Encrypt("IronSoftware");

workBook.Save();
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Open protected spreadsheet file
Dim protectedWorkBook As WorkBook = WorkBook.Load("sample.xlsx", "IronSoftware")

' Set protection for spreadsheet file
workBook.Encrypt("IronSoftware")

workBook.Save()
$vbLabelText   $csharpLabel

Open a Password-Protected Workbook

Open Protected Spreadsheet

Remove Password from Workbook

To remove the password from a spreadsheet, simply set the Password field to null, as demonstrated in the code below:

请注意This action can only be performed after accessing the workbook. Therefore, it is necessary to know the original password.

:path=/static-assets/excel/content-code-examples/how-to/set-password-workbook-unprotect.cs
// Remove protection for opened workbook. Original password is required.
workBook.Password = null;
' Remove protection for opened workbook. Original password is required.
workBook.Password = Nothing
$vbLabelText   $csharpLabel

IronXL provides the ability to protect and unprotect Excel workBooks and workSheets with a single line of C# code.

常见问题解答

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

要在 C# 中为 Excel 工作簿设置密码,可以使用 IronXL 的 Encrypt 方法。首先,下载 IronXL 库,加载您的工作簿,使用您想要的密码应用 Encrypt 方法,然后保存工作簿。

使用 IronXL 打开密码保护的工作簿的过程是什么?

要使用 IronXL 打开密码保护的工作簿,使用 WorkBook.Load 方法,将文件路径和密码作为参数。这可以让您安全地访问工作簿。

如何从 Excel 工作簿中移除密码?

要使用 IronXL 从 Excel 工作簿中移除密码,首先使用正确的密码加载工作簿。然后,使用 Decrypt 方法将 Password 字段设置为 null,并保存工作簿。

我能在不知道密码的情况下打开密码保护的 Excel 文件吗?

不,在使用 IronXL 时不能在不知道密码的情况下打开密码保护的 Excel 文件。必须提供正确的密码才能访问内容。

在 C# 中哪里可以找到用于密码保护 Excel 工作簿的库?

您可以从 NuGet 网站 https://nuget.org/packages/IronXL.Excel/ 下载 IronXL 用于密码保护 Excel 工作簿的库。

是否可以保护工作簿中的个别工作表?

是的,IronXL 允许您使用 C# 代码保护和解除保护 Excel 工作簿及个别工作表,确保两个层次的安全性。

如何确保 Excel 工作簿中的数据安全性?

为了确保 Excel 工作簿中的数据安全性,您可以利用 IronXL 的密码保护功能。通过使用 Encrypt 方法应用密码,您可以确保只有授权用户可以访问数据。

应用密码保护到 Excel 工作簿的步骤是什么?

应用密码保护到 Excel 工作簿的步骤是:下载 IronXL 库,加载工作簿,使用 Encrypt 方法设置密码,最后保存工作簿以确保其安全性。

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