Encrypt Excel Workbook in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to encrypt Excel workbook via Spire.XLS.

using Spire.Xls;

namespace Encrypt
{
    class Program
    {
        static void Main(string[] args)
        {
            //create workbook
            Workbook workbook =new Workbook ();
            Worksheet worksheet = workbook.Worksheets[0];
            workbook.LoadFromFile(@"..\..\..\..\Data\employee.xls",ExcelVersion.Version97to2003);
            
            //encrypt the workbook
            workbook.Protect("iceblue");

            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
            System.Diagnostics.Process.Start(workbook.FileName);                     
        }
    }
}

Imports Spire.Xls

Namespace Encrypt
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			'create workbook
			Dim workbook As New Workbook()
			Dim worksheet As Worksheet = workbook.Worksheets(0)
			workbook.LoadFromFile("..\..\..\..\Data\employee.xls",ExcelVersion.Version97to2003)

			'encrypt the workbook
			workbook.Protect("iceblue")

			workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003)
			System.Diagnostics.Process.Start(workbook.FileName)
		End Sub
	End Class
End Namespace