Unlock Sheet in Excel File in C#, VB.NET

2011-07-28 05:23:41 Written by  support iceblue
Rate this item
(0 votes)

This section aims at providing developers a solution to unlock sheet in Excel workbook with C#, VB.NET via this Excel library Spire.XLS for .NET.

Spire.XLS for .NET enables you to unlock any sheet in Excel file only by one line of key code: Spire.Xls.Worksheet.Unprotect(string password); Besides, as an MS Excel component, Spire.XLS for .NET also enables you to create, read and handle Excel files with fast speed. Below is an Excel file with protected worksheets which will be unlocked in my task.

Unlock Excel Worksheet

Since you will use Spire.XLS for .NET, you have to download Spire.XLS for .NET and install it on system. When you create your project, please do not forget to add Spire.XLS.dll as reference from Bin folder. The default path is "..\Spire.XLS\Bin\NET4.0\Spire.XLS.dll". Please note that Spire.XLS for .NET supports .NET Framework 2.0 and above. Here is the whole code for unlocking Excel sheet:

[C#]
namespace UnlockExcelSheet
{
    class Program
    {
        static void Main(string[] args)
        {
            //initialize an instance of Workbook
            Workbook workbook = new Workbook();
            //Load an Excel file with protected worksheet
            workbook.LoadFromFile(@"..\Unlock Excel Worksheet.xlsx");
            //get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];
            //Unprotect worksheet
            sheet.Unprotect("iceblue");
            //Save the file
            workbook.SaveToFile("Sample.xlsx",ExcelVersion.Version2010);
            //Launch the file
            System.Diagnostics.Process.Start("Sample.xlsx");
        }
    }
}
          
[VB.NET]
Namespace UnlockExcelSheet
	Class Program
		Private Shared Sub Main(args As String())
			'initialize an instance of Workbook
			Dim workbook As New Workbook()
			'Load an Excel file with protected worksheet
			workbook.LoadFromFile("..\Unlock Excel Worksheet.xlsx")
			'get the first worksheet
			Dim sheet As Worksheet = workbook.Worksheets(0)
			'Unprotect worksheet
			sheet.Unprotect("iceblue")
			'Save the file
			workbook.SaveToFile("Sample.xlsx",ExcelVersion.Version2010)
			'Launch the file
			System.Diagnostics.Process.Start("Sample.xlsx")
		End Sub
	End Class
End Namespace
          

After executing above code, you can see that the protected worksheet in the original Excel file has been unlocked, we can edit it also. Please see following image.

Unlock Excel Worksheet

In this section, I have introduced the solution to unlock any sheet in Excel file via Spire.XLS for .NET. I hope it can help you. If you have any questions, feedbacks and advice, you can put them on E-iceblue Forum. We will promise a prompt reply.

Additional Info

  • tutorial_title: Unlock Excel Worksheet
Last modified on Wednesday, 07 December 2022 01:10