How to Open Password Protected Excel File using C#

Are you developing a C# program that automates the password protected excel file? You might be in irritative situation as when your excel file is opened by C# code, it will prompt you to enter password manually. As this is done for automation and you want that there should not be any manual intervention. Your program must run smoothly. So, you need to provide password to that excel file at the time of opening.

Below given C# code snippet is going to guide you to open password protected excel file. At the time of calling Open method, you can provide the password.

objExcel.Workbooks.Open(excelPath, Password: "mypassword");

Here excelPath variable stores the full file path of excel file.

You can refer this post to know about the automation of excel file by C#. The complete code would look like.

public void workOnExcel()
{
 // creates excel application object
 excel.Application objExcel = new excel.Application();
 // creates new excel workbook object
 excel.Workbook excelworkBook;
 // creates new excel sheet object
 excel.Worksheet excelSheet;

 string excelPath = "D:/Test Excel";
 // opens excel workbook on the specified path
 objExcel.Workbooks.Open(excelPath, Password: "mypassword");
// Now the password protected excel file is opened without any prompt.
// you can do with it whatever you require.
}

Leave a Reply