Protect excel worksheet

  • NPOI
  • Spire.XLS
  • Download Sample Code

using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NPOI
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create workbook
            IWorkbook workbook = new XSSFWorkbook();

            //Create a new sheet
            ISheet sheet = workbook.CreateSheet("newSheet");

            //Protect the sheet
            sheet.ProtectSheet("test");          

            //Save the file
            FileStream file = File.Create("Protected.xlsx");
            workbook.Write(file);
            file.Close();

            //Launch
            System.Diagnostics.Process.Start("Protected.xlsx");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Xls;

namespace Spire.XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create workbook
            Workbook workbook = new Workbook();

            //Create an empty worksheet
            Worksheet sheet = workbook.CreateEmptySheet("newSheet");

            //Protect the sheet
            sheet.Protect("test");

            //Save and Launch
            workbook.SaveToFile("Protected.xlsx", ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("Protected.xlsx");
        }
    }
}