使用 IRONXL 如何在 Excel 中锁定单元格:逐步教程 Curtis Chau 已更新:六月 22, 2025 Download IronXL NuGet 下载 DLL 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article Many people find it challenging to understand the procedure for locking cells in Excel. We are here to help. This article will show you how to lock cells in an Excel file easily, one step at a time. You will be able to do it like a pro by the end of this article, so let's get started. IronXL: The C# Excel Library IronXL is a library for C# that lets you work with Excel without installing Excel on your system. This is a significant benefit as you won't need access to administrative rights and won't have to worry about interfering with the user's machine. If there are still any doubts about this library, we recommend reading our datasheet. To create a new Excel file and format the data according to your needs, you can use the IronXL C# library. This is a go-to package for all Excel operations, meaning that it provides complete functionality. If you're building any software and need to edit or create an Excel file, you can use the IronXL library to do this quickly. Furthermore, IronXL also offers a wide range of features to interact with Excel WorkBook, WorkSheet and Cells level such as converting between popular formats, cell data formatting, merging cells, inserting math functions, and even managing charts and adding images. To use it, you must import the library's DLL file or install it with NuGet Package Manager. Its free trial version can be used to test and ensure that all features function as intended. The trial version is free and does not expire until the end of 30 days. When you purchase IronXL, you will need to apply for the license key. If you decide that you like IronXL after reading the documentation, please make sure to buy it at an affordable price! Visit this licensing page for more information about licensing. Let's see how we can protect an Excel sheet using the IronXL C# Library: Create a C# .NET or VB .NET project in Visual Studio. Download the DLL file of the IronXL library or install it via NuGet Package Manager in Visual Studio. Apply for your License key using the provided tutorial. Use IronXL in your project. // Import IronXL to your project using IronXL; // Load the Excel workbook from the specified file path WorkBook wb = WorkBook.Load("fileaddress.xlsx"); // Select the specific sheet you want to lock WorkSheet ws = wb.GetWorkSheet("SheetName"); // Protect the sheet by setting a password ws.ProtectSheet("Password"); // Save the changes made to the workbook wb.Save(); // Import IronXL to your project using IronXL; // Load the Excel workbook from the specified file path WorkBook wb = WorkBook.Load("fileaddress.xlsx"); // Select the specific sheet you want to lock WorkSheet ws = wb.GetWorkSheet("SheetName"); // Protect the sheet by setting a password ws.ProtectSheet("Password"); // Save the changes made to the workbook wb.Save(); ' Import IronXL to your project Imports IronXL ' Load the Excel workbook from the specified file path Private wb As WorkBook = WorkBook.Load("fileaddress.xlsx") ' Select the specific sheet you want to lock Private ws As WorkSheet = wb.GetWorkSheet("SheetName") ' Protect the sheet by setting a password ws.ProtectSheet("Password") ' Save the changes made to the workbook wb.Save() $vbLabelText $csharpLabel You have now seen how easy it is to protect cells in Excel using the IronXL library, given a working knowledge of C#. You can also benefit from multiple tutorials on Excel operations in the IronXL official documentation. You can download a file project. Lock Cells in Microsoft Excel The lock cell function is a protection feature in Microsoft Excel that allows you to lock cells so that they cannot be edited. Let's look at how we can lock cells in Microsoft Excel. Protect the Entire Sheet Sometimes you have to give your document to someone else for read-only purposes. You can do this by protecting the sheet with a password. Let's see how we can do this: Open your file in Microsoft Excel. Go to the Home tab and click on the dialogue box launcher icon in the Alignment group. Navigate to "Format Cells" dialog in Excel A "Format Cells" section will open. Go to the Protection tab. Protection tab in "Format Cells" dialog Check the Locked check box and click on the OK button. Lock cells option Next, go to the Review tab and click the Protect Sheet option in the Protect group. Navigate to the "Protect Sheet" button in Excel Then, enter a password to protect the sheet and click the OK button. Enter a password in the Protect Sheet dialog Next, re-enter the password to verify your password. Re-enter a password After following these steps, your sheet will be completely protected. When someone tries to edit any cell in an Excel file, they will get an error prompt. Error on editing a locked sheet in Excel Lock Specific Cells Sometimes you will need to protect specific cells with data or formulas. If so, unprotect the rest of the spreadsheet before protecting those essential cells. Let's look at how we can lock specific cells in Excel. Open your Excel file in Microsoft Excel. Select the whole sheet and press Ctrl + 1 to open the "Format Cells" dialogue box. The "Format Cells" dialogue box will open. Format Cells dialog in Excel Go to the Protection tab, uncheck the 'Locked' option, and press the OK button. This will unlock cells in the Excel spreadsheet. Navigate to the Lock cell function Next, select the cells you want to protect and open the cell dialogue box using the Ctrl+1 keyboard shortcut key. Go to the Protection tab, check the locked option, and click the OK button. Lock cell in Format Cells dialog Go to the review tab and click "protect the sheet" from Protect Group. The protection dialogue box will open. Check the option "Protect worksheet and content of the cell." Enter the password to protect it. Protect Sheet dialog and enter a password By doing this, your selected cells will be protected. When you try to edit these cells, you will get the error message. Error on editing a locked sheet in Excel Lock Formula Cells You can choose to lock only formula cells to protect your calculations in Excel. Follow these steps to lock formula cells in Excel: Open your Excel file. Unlock the whole sheet by selecting all the cells, going to the cell group, and clicking on the Format menu. Select "format cells" from the dropdown. The "Format Cells" menu will open. Navigate to Format Cells function Go to the Protection tab, uncheck the 'Locked' option and click the OK button. Figure 15: Tick on the Locked option This will unlock the whole sheet. Next, find the formula cells. Go to the Home tab and click on the Find and Select menu. From the drop-down, select the "Go to special" menu. From the "Go To Special" dialogue box, select Formulas and press the OK button. Enable "Formulas" option in "Go To Special" dialogue box You will see that cells with formulas will be selected in the Excel sheet. Selected cells with formulas With these cells selected, go to the review tab and click "Protect Sheet". Enter the password to protect and re-enter the password to confirm. Protect formula cells Confirm password dialogue box The formula cells will now be protected. No one will be able to edit these cells without entering the correct password. Unprotect Sheet Follow these steps to enable editing: Select the whole sheet. Go to the review tab and click "Unprotect Sheet". Navigate to Unprotect Sheet function In the "Unprotect Sheet" dialogue box, enter the correct password. The sheet will now be open to edits. Enter the password to unlock the sheet Conclusion: The Benefits of Locked Cells vs. Unlocked Cells There are clearly benefits to locking cells, but the level of security you need depends on what you are using the cells for. The benefits of locking cells include: Preventing access to unauthorized users (e.g. children) Preventing people from tampering with an item Controlling the environment in which items are stored The drawbacks of locking cells include: Costs of purchasing and installing locks Difficulties in accessing items when they need to be retrieved (e.g., when a file has gone missing) Difficulty in moving items within a facility 常见问题解答 如何使用C#锁定Excel文件中的单元格? 您可以通过使用IronXL库在Excel文件中使用C#锁定单元格。首先,加载Excel工作簿并选择工作表。然后,使用ProtectSheet方法和密码锁定特定单元格。 使用C#保护整个Excel工作表涉及哪些步骤? 要使用C#保护整个Excel工作表,您可以使用IronXL库。加载工作簿,选择所需工作表,并应用ProtectSheet方法和密码以保护整个工作表。 是否可以用C#锁定Excel中仅某些特定的单元格? 是的,通过在C#中使用IronXL,您可以锁定Excel中特定的单元格。首先,解锁整个工作表,然后选择您要锁定的单元格,最后应用ProtectSheet方法和密码。 锁定Excel单元格有什么好处? 锁定Excel单元格提供了多种好处,包括防止未经授权的访问、确保数据的完整性以及保护敏感信息免受篡改。 IronXL可以用于锁定Excel中的公式单元格吗? 可以,IronXL可以用于锁定Excel中的公式单元格。解锁整个工作表,使用“定位条件”功能选择公式单元格,然后应用ProtectSheet方法和密码。 如何使用C#解锁Excel中的受保护工作表? 要使用C#解锁Excel中的受保护工作表,可以使用IronXL加载工作簿,选择工作表,并通过正确的密码应用UnprotectSheet方法。 锁定Excel单元格有哪些潜在的缺点? 锁定Excel单元格的潜在缺点包括在必要时难以访问和检索数据以及难以修改已锁定的内容,除非通过正确的密码解锁。 如何开始一个C#项目来使用IronXL操作Excel? 要开始一个使用IronXL的C#项目,在Visual Studio中创建一个新的C#项目,添加IronXL NuGet包,并应用您的IronXL许可证密钥以开始使用Excel文件。 我能找到有关使用IronXL进行Excel操作的教程吗? 是的,IronXL在其网站上提供了全面的文档和教程,涵盖了使用C#对Excel文件进行创建、编辑和保护等各种操作。 IronXL有供测试的试用版吗? IronXL提供一个免费试用版,允许您在30天内测试库的所有功能,而无需支付任何费用。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已发布十月 27, 2025 如何在 C# 中创建 Excel 数据透视表 学习通过这个清晰的分步指南使用C# Interop和IronXL在Excel中创建数据透视表。 阅读更多 已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多 已发布十月 27, 2025 如何在.NET Core中使用CSV Reader与IronXL 学习通过实际示例有效地使用IronXL作为.NET Core的CSV读取器。 阅读更多 在 C# 中读取 CSV 文件:教程如何在 Excel 中合并单元格...
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多