跳至页脚内容
使用 IRONXL

如何在 C# 中禁用 Excel 受保护视图

How to Disable Protected View in Excel Using IronXL

This article explains how you can easily disable the Excel-protected view message using the advanced C# Excel library, IronXL.

1. IronXL

IronXL is a powerful C# library that provides developers with an efficient and straightforward way to work with Excel files within their applications. Whether you need to read, write, or manipulate Excel data, IronXL simplifies the process and empowers you to accomplish complex tasks with ease. With its intuitive API and extensive functionality, IronXL eliminates the need for manual Excel file handling, saving you valuable time and effort. This guide will explore the key features and capabilities of IronXL, demonstrating how it can be utilized to streamline Excel-related operations and enhance your C# applications. Whether you are building a data analysis tool, generating reports, or performing data migrations, IronXL is a valuable asset that unlocks the full potential of Excel integration in your C# projects.

2. Prerequisites

Before you can start working with IronXL and disable protected view in Excel files using the C# programming language, there are a few prerequisites that need to be in place. These prerequisites include:

  1. Development Environment: Ensure that you have a suitable development environment set up on your computer. This typically involves having an integrated development environment (IDE) such as Visual Studio installed.
  2. C# Knowledge: It's important to have a basic understanding of C#.
  3. IronXL Library: You'll need to have the IronXL library installed in your project. This can be accomplished by using the NuGet Package Manager within Visual Studio or through the command-line interface. Installing the library will allow you to leverage its functionalities for Excel file manipulation, including cell formatting.

By ensuring that these prerequisites are met, you'll be ready to dive into the process of working with IronXL.

3. Create a New C# Project

To begin working with IronXL, the first step is to create a new project in Visual Studio.

  1. Open Visual Studio, go to File and click on the New Project button.

    How To Disable Protected View in Excel Using C#, Figure 1: New Project in Visual Studio New Project in Visual Studio

  2. A new window will appear. In this window, select "Console Application" and click on the Next button.

    How To Disable Protected View in Excel Using C#, Figure 2: Create a new Console Application Create a new Console Application

  3. In the last window, select the target framework, set the location and name of this new project, and click on the Create button.

Your new project in Visual Studio is created.

4. Installing IronXL

IronXL offers several ways to install this library, with the following being the most effective approaches:

  1. Install IronXL Using NuGet Package Manager.
  2. Install IronXL Using Package Manager Console.

4.1 Install IronXL Using NuGet Package Manager

Use the NuGet Package Manager to add the IronXL library to your Visual Studio project. Search for IronXL in the Browse tab:

Simply choose the package and begin the installation procedure after finding IronXL in the search results. After the installation is complete, you may begin using the IronXL library in your project.

The following screenshot shows how to use Visual Studio to access the NuGet Package Manager.

How To Disable Protected View in Excel Using C#, Figure 3: Navigate to NuGet Package Manager Navigate to NuGet Package Manager

IronXL in search results:

How To Disable Protected View in Excel Using C#, Figure 4: Search and install the IronXL package in NuGet Package Manager UI Search and install the IronXL package in NuGet Package Manager UI

4.2 Install IronXL Using the Visual Studio Command Line

The command-line interface is a common approach for package installation among developers. Follow these instructions to install IronXL from the command line:

  1. Navigate to Tools > NuGet Package Manager > Package Manager Console in Visual Studio.
  2. Navigate to the Package Manager Console tab.
  3. In the console, type the following command:

    Install-Package IronXL
    Install-Package IronXL
    SHELL
  4. To run the command, press Enter. This will start the IronXL package download and installation for the current project.

When the procedure is finished, you may begin using the IronXL library in your project.

How To Disable Protected View in Excel Using C#, Figure 5: Install IronXL in the Package Manager Console Install IronXL in the Package Manager Console

5. Disable Protected View in Excel Files using IronXL

IronXL is a top-of-the-line Excel Library, using its advanced features you can easily disable protected view messages using C# code. First, we need an example Excel file that has protected view enabled.

How To Disable Protected View in Excel Using C#, Figure 6: An Excel file with the protection view enabled An Excel file with the protection view enabled

You can easily disable the protected view message with the following code example:

using IronXL;

class Program
{
    static void Main(string[] args)
    {
        // Load the Excel workbook with protection view enabled.
        WorkBook workBook = WorkBook.Load("sample1.xlsx");

        // Get the default worksheet from the workbook.
        WorkSheet workSheet = workBook.DefaultWorkSheet;

        // Disable the protection on the worksheet.
        workSheet.UnprotectSheet();

        // Save the modified workbook to a new file.
        workBook.SaveAs("sample2.xlsx");
    }
}
using IronXL;

class Program
{
    static void Main(string[] args)
    {
        // Load the Excel workbook with protection view enabled.
        WorkBook workBook = WorkBook.Load("sample1.xlsx");

        // Get the default worksheet from the workbook.
        WorkSheet workSheet = workBook.DefaultWorkSheet;

        // Disable the protection on the worksheet.
        workSheet.UnprotectSheet();

        // Save the modified workbook to a new file.
        workBook.SaveAs("sample2.xlsx");
    }
}
Imports IronXL

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Load the Excel workbook with protection view enabled.
		Dim workBook As WorkBook = WorkBook.Load("sample1.xlsx")

		' Get the default worksheet from the workbook.
		Dim workSheet As WorkSheet = workBook.DefaultWorkSheet

		' Disable the protection on the worksheet.
		workSheet.UnprotectSheet()

		' Save the modified workbook to a new file.
		workBook.SaveAs("sample2.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel

In this code, we open an Excel workbook using the WorkBook.Load method and then load the default Excel worksheet using the workBook.DefaultWorkSheet property. Next, workSheet.UnprotectSheet disables the Protected View as required, and finally, workBook.SaveAs is used to save the modifications to a new Excel file.

Here is the output screenshot.

How To Disable Protected View in Excel Using C#, Figure 7: The output Excel file The output Excel file

As you can see, Microsoft Excel's enable editing functions are now available.

6. Conclusion

IronXL is a powerful C# library that simplifies working with Microsoft Office Excel files in your applications. By following the steps outlined in this guide, you can easily disable the Protected View feature in Excel using C#.

IronXL's intuitive API and extensive functionality make it a valuable tool for manipulating Excel files, saving you time and effort in your development process. By installing the IronXL library, creating a new project in Visual Studio, and utilizing the provided code examples, you can effectively disable Protected View and unlock the full potential of Excel integration in your C# projects.

Furthermore, IronXL offers a wide range of features to interact with Excel WorkBooks, WorkSheets, and Cells level such as converting between popular formats, cell data formatting, merging cells, inserting math functions, and even managing charts and adding images.

For more information on how protected view messages work, visit this blog. Please visit other tutorials and code examples to read Excel files.

IronXL is available to users for a free trial and can be licensed for commercial use with its Lite package starting from $799 only.

常见问题解答

如何使用 C# 禁用 Excel 文件中的安全视图?

您可以通过加载 Excel 工作簿、访问工作表、使用 workSheet.UnprotectSheet() 方法来禁用保护,然后保存工作簿来使用 IronXL 禁用 Excel 文件中的安全视图。

在 C# 中处理 Excel 文件的最佳方法是什么?

使用 IronXL 是处理 C# 中 Excel 文件的高效方法,因为它为读取、写入和操作 Excel 数据提供了简单的 API。

在使用 IronXL 进行 Excel 操作之前,我应该知道些什么?

在使用 IronXL 之前,您应该设置一个开发环境,例如 Visual Studio,并对 C# 有基本的了解。还应在项目中安装 IronXL 库。

如何使用 NuGet 包管理器安装 IronXL?

在 Visual Studio 中,打开 NuGet 包管理器,在“浏览”选项卡下搜索 IronXL,选择它,然后按照安装说明将其添加到项目中。

我可以使用 IronXL 将 Excel 文件转换为其他格式吗?

是的,IronXL 允许您在不同的电子表格格式之间进行转换,例如从 Excel 到 CSV 或 JSON,从而提供数据管理的灵活性。

IronXL 提供哪些许可选项?

IronXL 提供免费试用,并提供商业许可选项,起始价格点为 Lite 套餐。

如何在 Visual Studio 中启动使用 IronXL 的新 C# 项目?

要启动新的 C# 项目,打开 Visual Studio,转到“文件”,点击“新建项目”,选择“控制台应用程序”,选择目标框架,设置项目位置和名称,然后点击“创建”。

IronXL 提供哪些功能用于 Excel 文件操作?

IronXL 提供了一系列功能,包括数据格式化、单元格合并、插入数学函数以及向 Excel 文件添加图片或图表。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。