This project has moved. For the latest updates, please go here.

Sheet Protection

            var wb = new XLWorkbook();
            var ws = wb.Worksheets.Add("Protected No-Password");

            ws.Protect()            // On this sheet we will only allow:
                .SetFormatCells()   // Cell Formatting
                .SetInsertColumns() // Inserting Columns
                .SetDeleteColumns() // Deleting Columns
                .SetDeleteRows();   // Deleting Rows

            ws.Cell("A1").SetValue("Locked, No Hidden (Default):").Style.Font.SetBold().Fill.SetBackgroundColor(XLColor.Cyan);
            ws.Cell("B1").Style
                .Border.SetOutsideBorder(XLBorderStyleValues.Medium);

            ws.Cell("A2").SetValue("Locked, Hidden:").Style.Font.SetBold().Fill.SetBackgroundColor(XLColor.Cyan);
            ws.Cell("B2").Style
                .Protection.SetHidden()
                .Border.SetOutsideBorder(XLBorderStyleValues.Medium);

            ws.Cell("A3").SetValue("Not Locked, Hidden:").Style.Font.SetBold().Fill.SetBackgroundColor(XLColor.Cyan);
            ws.Cell("B3").Style
                .Protection.SetLocked(false)
                .Protection.SetHidden()
                .Border.SetOutsideBorder(XLBorderStyleValues.Medium);

            ws.Cell("A4").SetValue("Not Locked, Not Hidden:").Style.Font.SetBold().Fill.SetBackgroundColor(XLColor.Cyan);
            ws.Cell("B4").Style
                .Protection.SetLocked(false)
                .Border.SetOutsideBorder(XLBorderStyleValues.Medium);

            ws.Columns().AdjustToContents();

            // Protect a sheet with a password
            var protectedSheet = wb.Worksheets.Add("Protected Password = 123");
            var protection = protectedSheet.Protect("123");
            protection.InsertRows = true;
            protection.InsertColumns = true;

            wb.SaveAs("SheetProtection.xlsx");

Last edited Apr 4, 2011 at 3:57 AM by MDeLeon, version 1