保护 Excel 文件 IronXL 的Encrypt方法使开发人员能够对整个 Excel 工作簿和单个工作表应用密码保护。 此功能对于保护敏感数据至关重要,并确保只有授权用户才能访问或修改 Excel 文件的内容。 IronXL 还允许进行工作表级别的保护,从而防止用户编辑工作簿的某些部分或特定工作表。 这在协作环境中尤其有用,因为你可能希望限制编辑权限,但仍然允许用户查看数据或与数据进行交互。 此外,无论是否使用密码,都可以选择移除保护,这提供了灵活性,使其既适用于安全环境,也适用于需要临时恢复访问权限的情况。 总而言之,此功能是确保自动化数据处理工作流程中 Excel 文件安全的重要工具。 使用密码加密工作簿的 5 个步骤 var workBook = WorkBook.Load("sample.xlsx");代码 <代码>workBook.Encrypt("myP@ssw0rd");代码 <代码>var workSheet = workBook.DefaultWorkSheet; 代码 <代码>workSheet.ProtectSheet("myP@ssw0rd_sheet");代码 <代码>workBook.Save();代码 在这段代码中,首先使用 IronXL 的WorkBook.Load()方法从文件sample.xlsx加载工作簿。 工作簿加载后,使用Encrypt()方法对整个工作簿应用密码保护,密码设置为myP@ssw0rd 。 这样可以确保没有正确的密码就无法打开或修改工作簿。 Next, the default worksheet of the workbook is accessed using the DefaultWorkSheet property. The ProtectSheet() method is then called on this worksheet, applying a password code>("myP@ssw0rd_sheet") that restricts editing on the sheet level. 这样可以防止用户在未提供正确密码的情况下对受保护的工作表进行更改。 Finally, the Save() method is invoked to save the changes made to both the workbook and the worksheet, ensuring that the protection settings are preserved in the saved file. This ensures that the workbook and its worksheet are both securely protected and can only be accessed or modified with the respective passwords. 学习如何在 C# 中为 Excel 工作簿设置密码 相关文档链接 在Github上查看 相关教程 相关操作指南 类文档 下载IronXL DLL 报告此页面的问题 准备开始了吗? Nuget 下载 1,802,965 | 版本: 2025.12 刚刚发布 免费 NuGet 下载 总下载量:1,802,965 查看许可证