We may sometimes need to Unlock or Unprotect the Excel Workbook and also Excel Worksheet to view and then perform some read, write opera...

How to Unprotect or Un-Lock Excel Workbook and Worksheet using C#


We may sometimes need to Unlock or Unprotect the Excel Workbook and also Excel Worksheet to view and then perform some read, write operations using C#, for which below code will be very
useful.

using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace Purush_Excel
{
    class Purush_Excel_UnprotectWorkBook_Sheet
    {
        static void Main(string[] args)
        {

            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Purush\Desktop\Test.xlsx");
            xlWorkbook.Unprotect("Purush_Password_Book");//Workbook Password
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets["Purush_Sheet1"];
            xlWorksheet.Unprotect("Purush_Password_Sheet");// Worksheet Password
            xlWorkbook.Save();
            xlWorkbook.Close();
         }
    }
}



0 comments: