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