XLSX write protection
The following example shows how you create a write protected XLSX file using GemBox.Spreadsheet in C# and VB.NET.
using GemBox.Spreadsheet;
class Program
{
static void Main()
{
// If using the Professional version, put your serial key below.
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("Sheet1");
worksheet.Cells["A1"].Value = "This spreadsheet has been opened in read-only mode.";
worksheet.Cells["A2"].Value = "Changes cannot be made to the original spreadsheet.";
worksheet.Cells["A3"].Value = "To save changes a new copy of the spreadsheet must be created.";
WriteProtection protection = workbook.WriteProtectionSettings;
protection.SetPassword("pass");
workbook.Save("XLSX Write Protection.xlsx");
}
}
Imports GemBox.Spreadsheet
Module Program
Sub Main()
' If using the Professional version, put your serial key below.
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
Dim workbook As New ExcelFile()
Dim worksheet = workbook.Worksheets.Add("Sheet1")
worksheet.Cells("A1").Value = "This spreadsheet has been opened in read-only mode."
worksheet.Cells("A2").Value = "Changes cannot be made to the original spreadsheet."
worksheet.Cells("A3").Value = "To save changes a new copy of the spreadsheet must be created."
Dim protection = workbook.WriteProtectionSettings
protection.SetPassword("pass")
workbook.Save("XLSX Write Protection.xlsx")
End Sub
End Module
XLSX write protection enables you to lock your original Excel workbook to prevent any changes being made to it. To enable the write protection, you need to set the password using the WriteProtection.SetPassword
method.
An Excel file with write protection is opened in Excel applications with a Read-Only mode. The spreadsheet's content can be edited, but it cannot be re-saved with the same file name.
Note, with GemBox.Spreadsheet you can modify the original Excel workbook even if it has write protection.
You can also specify the usage of Read-Only mode without providing the password, by enabling the WriteProtection.ReadOnlyRecommended
property. In Microsoft Excel, that would be the following option: