How To Disable Protected View in Excel Using C#

Disabling the Protected View in Excel is a common requirement when working with automated tasks or accessing external data sources. Protected View is a security feature in Excel that helps prevent potentially harmful content from being opened. However, in certain scenarios, such as when developing C# applications that interact with Excel files, it may be necessary to disable Protected View to enable seamless automation and data retrieval. In this guide, we will explore how to accomplish this using C# programming language. By following the steps outlined in this tutorial, you will be able to disable Protected View in Excel, allowing your C#

In this article we will see how you can easily disable the Excel protected view message using Advance 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. In this guide, we 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#

2. Prerequisites

Before you can start working with IronXL and disable protected view in Excel files using 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 the 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#

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

  1. Open Visual Studio, and go to Files and click on New project.

    How To Disable Protected View in Excel Using C#: Figure 1

  2. A new window will appear, in this new window select "Console Application" and click on next.

    How To Disable Protected View in Excel Using C#: Figure 2

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

Your new project in Visual Studio is created.

4. Installing IronXL

IronXL offers many ways to install this library, but we will only discuss the two of these those are:

  1. Install IronXL Using NuGet Package Manager.
  2. Install IronXL Using NuGet 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, and then 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

IronXL in search results:

How To Disable Protected View in Excel Using C#: Figure 4

4.2 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:

    Install-Package IronXL.Excel
  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

5. Disable Protection 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 message using C# Code easily. First we need an example Excel file that has protection view enabled.

How To Disable Protected View in Excel Using C#: Figure 6

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

using IronXL;

WorkBook workBook = WorkBook.Load("sample1.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
workSheet.UnprotectSheet();
workBook.SaveAs("sample2.xlsx");
using IronXL;

WorkBook workBook = WorkBook.Load("sample1.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
workSheet.UnprotectSheet();
workBook.SaveAs("sample2.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("sample1.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
workSheet.UnprotectSheet()
workBook.SaveAs("sample2.xlsx")
VB   C#

The above code opens an Excel workbook using WorkBook.Load("sample1.xlsx"), and then loads the default Excel worksheet using workBook.DefaultWorkSheet property. Next, workSheet.UnprotectSheet() disables Protected View as required, and workBook.SaveAs("sample2.xlsx") method.

Here is the output screenshot.

How To Disable Protected View in Excel Using C#: Figure 7

As you can clearly 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.

For more information on how protected view messages work visit this link. For tutorial and code examples please visit this link.

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