You may also like these articles
- Restrict User to Upload Excel File in Asp.net Core MVC Using jQuery
- Validate Uploaded File Extension in C# .Net Core
- Read Excel File Data into DataSet C#, .Net Core
- Read Excel file in C# Without Using Interop
- Upload, Read and Display Excel File in .Net Core Using C#.net Windows Application
- Step‑by‑Step Guide: Import and Display Excel Data in ASP.NET Core MVC with C#.
So here I have created a password protected excel file. When we open this file excel will ask for password. So, to verify an excel file is having password or not we will create a new .net core application and install the NuGet package named as "ExcelDataReader".
In above code i have taken a windows forma and in this form i have added a button. On button click event i have called a method named as ExcelStaus to verify that the provided excel file is password protected or not.
Now we use the below code to verify the excel file encryption status.
C#.Net
using ExcelDataReader;
using ExcelDataReader.Exceptions;
using System.Text;
namespace WinFormsApp3
{
public partial class VerifyExcelFileEncrypted : Form
{
public VerifyExcelFileEncrypted()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Change file path as per you file location
string excelFilePath = "C:\\Personal\\Files\\Test.xlsx";
bool status = ExcelStatus(excelFilePath);
if (status)
{
MessageBox.Show("The Excel file is encrypted.");
}
else
{
MessageBox.Show("The Excel file is not encrypted.");
}
}
private bool ExcelStatus(string filePath)
{
try
{
using (var stream = File.OpenRead(filePath))
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
using (ExcelReaderFactory.CreateReader(stream)) { }
}
}
catch (InvalidPasswordException)
{
return true;
}
return false;
}
}
}
VB.Net
Imports ExcelDataReader
Imports ExcelDataReader.Exceptions
Imports System.Text
Imports System.IO
Imports System.Windows.Forms
Namespace WinFormsApp3
Public Partial Class VerifyExcelFileEncrypted
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
'Change file path as per you file location
Dim excelFilePath As String = "C:\Personal\Files\Test.xlsx"
Dim status As Boolean = ExcelStatus(excelFilePath)
If status Then
MessageBox.Show("The Excel file is encrypted.")
Else
MessageBox.Show("The Excel file is not encrypted.")
End If
End Sub
Private Function ExcelStatus(filePath As String) As Boolean
Try
Using stream = File.OpenRead(filePath)
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)
Using ExcelReaderFactory.CreateReader(stream)
End Using
End Using
Catch ex As InvalidPasswordException
Return True
End Try
Return False
End Function
End Class
End Namespace Here i am using .net core 10. So the given piece of code is very important in .net core. "Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);" . If you dont use it will throw an exception. The code line "using (ExcelReaderFactory.CreateReader(stream)) { }" us used to verify that file is encrypted or not. Now let's run the code to check the output.
Verify Excel File Encrypted.zip
19KiB


