Detect if an Excel Workbook is Password Protected in C#, VB.NET

2017-01-09 03:49:03 Written by  support iceblue
Rate this item
(0 votes)

Spire.XLS provides a class named Workbook that represents an Excel workbook. This class contains a method named IsPasswordProtected(string fileName) which returns a boolean value. If the value is true, means the workbook is encrypted with password, otherwise it's not.

This is an Excel workbook protected with password:

Detect if an Excel Workbook is Password Protected in C#, VB.NET

Now refer below code to detect if the Excel workbook is password protected:

[C#]
using System;
using Spire.Xls;

namespace Detect_if_workbook_is_password_protected
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "E:\\Program Files\\Sample.xlsx";
            //Detect if the Excel workbook is password protected.
            bool value = Workbook.IsPasswordProtected(fileName);
            Console.WriteLine(value);
            Console.ReadKey();
        }
    }
}
[VB.NET]
Imports Spire.Xls

Namespace Detect_if_workbook_is_password_protected
	Class Program
		Private Shared Sub Main(args As String())
			Dim fileName As String = "E:\Program Files\Sample.xlsx"
			'Detect if the Excel workbook is password protected.
			Dim value As Boolean = Workbook.IsPasswordProtected(fileName)
			Console.WriteLine(value)
			Console.ReadKey()
		End Sub
	End Class
End Namespace

After running the project, we get the Output that shows the workbook is password protected:

Detect if an Excel Workbook is Password Protected in C#, VB.NET

Additional Info

  • tutorial_title: Detect if an Excel Workbook is Password Protected
Last modified on Monday, 06 September 2021 02:06