Documents for Excel, .NET Edition Documentation
Customize User Interaction / Manage Workbook / Protect Workbook
In This Topic
    Protect Workbook
    In This Topic

    GcExcel allows you to protect the workbook in case it contains any critical and confidential information that cannot be shared with others. Additionally, you can also protect it from modification so that other users can't perform certain operations on the workbook.

    To protect or unprotect a workbook, you can perform the following tasks:

    Protect workbook using password

    GcExcel enables users to protect a workbook by encrypting it with a password. This is important when you have a business-critical workbook containing sensitive data that cannot be shared with everyone. You can secure a workbook using the Password property of XlsxSaveOptions class.

    Refer to the following example code to make your workbook password protected.

    C#
    Copy Code
    // Save the Excel file and protect it using password.
    XlsxSaveOptions options = new XlsxSaveOptions();
    options.Password = "123456";
    workbook.Save(@"createWorkbook.xlsx", options);

    Protect workbook modification

    You can also protect a workbook from modification using the Protect method of the Workbook class. The method has two optional parameters, structure and windows, which provide different types of modification protection when set.

    Refer to the following example code to protect your workbook from modification.

    C#
    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
    // Fetch default worksheet 
    IWorksheet workSheet = workbook.Worksheets[0];
    
    //Protects the workbook so that other users cannot view hidden worksheets, add, move, delete, hide, or rename worksheets.
    workbook.Protect();
    // Saving workbook to xlsx
    workbook.Save(@"1-ProtectWorkbook.xlsx", SaveFileFormat.Xlsx);

    Unprotect workbook

    You can easily unprotect the protected workbook by using the Unprotect method of the Workbook class, which removes the protection from the workbook.

    Refer to the following example code to unprotect your workbook.

    C#
    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
    // Fetch default worksheet 
    IWorksheet workSheet = workbook.Worksheets[0];
    
    workbook.Protect();
    //Removes the above protection from the workbook.
    workbook.Unprotect();
    // Saving workbook to xlsx
    workbook.Save(@"2-UnprotectWorkbook.xlsx", SaveFileFormat.Xlsx);