Other API Samples

Live SpreadsheetGear API Samples

Workbook Miscellaneous Protect Workbook Structure and Windows

Prevent users from adding, removing, rearranging or otherwise modifying sheet tabs from a workbook by enabling this option (password is optional). Windows can also be protected, which disables users from moving, resizing, or closing the workbook window, or hide/unhide windows. However, SpreadsheetGear's WorkbookView does not implement this feature (the option is supported in the file format).

NOTE: this feature does not encrypt the workbook contents or require a password to be provided upon opening the workbook. To do this, use IWorkbook.SaveAs(...) with the overloaded option to provide a password.

// Create a new workbook with 3 worksheets.
SpreadsheetGear.IWorkbookSet workbookSet = SpreadsheetGear.Factory.GetWorkbookSet();
workbookSet.SheetsInNewWorkbook = 3;
SpreadsheetGear.IWorkbook workbook = workbookSet.Workbooks.Add();
SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"];
SpreadsheetGear.IRange cells = worksheet.Cells;

// Add a value to cell A1.
cells["A1"].Value = "Hello World!";

// Protect the workbook's structure and/or windows without a password with these properties.
workbook.ProtectStructure = true;
workbook.ProtectWindows = true;

// Protect with a password using this method.
// NOTE: this feature does not encrypt the workbook contents or require a password to be provided 
// upon opening the workbook.  To do this, use <kbd>IWorkbook.SaveAs(...)<kbd> with the overloaded 
// option to provide a password.
workbook.Protect("MyPassword1234", true, true);
Run This Sample
Download File

Download an Excel Open XML Workbook (*.xlsx) file with the results of this sample.