Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 1.42 KB

Sheet-Protection.md

File metadata and controls

40 lines (32 loc) · 1.42 KB
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");