Documents for Excel, Java Edition Documentation
Features / 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 setPassword method of XlsxSaveOptions class.

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

    Java
    Copy Code
    // Saving an excel file while setting password
    XlsxSaveOptions options = new XlsxSaveOptions();
    options.setPassword("123456");
    workbook.save("SaveExcelWithPassword.xlsx", options);

    Protect Workbook from Modification

    GcExcel allows you to protect a workbook from modification. Optionally, a password can be set to achieve the same.

    The Workbook class provides two overloaded protect methods, one of which takes password as a parameter. Both the methods have two optional parameters, structure and windows, which provide different types of modification protection when set.

    Refer to the following example code to protect the workbook from modification using password.

    Java
    Copy Code
     // Initialize workbook
     Workbook workbook = new Workbook();
    // Fetch default worksheet
     IWorksheet worksheet = workbook.getWorksheets().get(0);
    // Data
     Object data = new Object[][] { { "Name", "City", "Sex", "Weight", "Height", "Age" },
            { "Bob", "NewYork", "male", 80, 180, 56 }, { "Betty", "NewYork", "female", 72, 168, 45 },
            { "Gary", "NewYork", "male", 71, 179, 50 }, { "Hunk", "Washington", "male", 80, 171, 59 },
            { "Cherry", "Washington", "female", 58, 161, 34 }, { "Coco", "Virginia", "female", 58, 181, 45 },
            { "Lance", "Chicago", "female", 49, 160, 57 }, { "Eva", "Washington", "female", 71, 180, 81 } };
        
    // Set data        
    worksheet.getRange("A1:G9").setValue(data);
    //Protects the workbook with password so that other users cannot view hidden worksheets, add, move, delete, hide, or rename worksheets.
    workbook.protect("Ygs_87@ytr");
    // Save workbook to xlsx
    workbook.save("ProtectWorkbook.xlsx", SaveFileFormat.Xlsx);    

    Refer to the following example code to protect the workbook from modification without using password.

    Java
    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
    // Fetch default worksheet
    IWorksheet worksheet = workbook.getWorksheets().get(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 from Modification

    A protected workbook can be unprotected to make modifications using the Unprotect method of the Workbook class, which removes the protection from a workbook.

    To unprotect a password protected workbook, the correct password needs to be passed as a parameter to the Unprotect method. In case, the password is omitted or an incorrect password is passed, an exception message "Invalid Password" is thrown.

    Refer to the following example code to unprotect a password protected workbook.

    C#
    Copy Code
    // Initialize workbook
     Workbook workbook = new Workbook();
    // Fetch default worksheet 
     IWorksheet worksheet = workbook.getWorksheets().get(0);
     // Data
     Object data = new Object[][] { { "Name", "City", "Sex", "Weight", "Height", "Age" },
            { "Bob", "NewYork", "male", 80, 180, 56 }, { "Betty", "NewYork", "female", 72, 168, 45 },
            { "Gary", "NewYork", "male", 71, 179, 50 }, { "Hunk", "Washington", "male", 80, 171, 59 },
            { "Cherry", "Washington", "female", 58, 161, 34 }, { "Coco", "Virginia", "female", 58, 181, 45 },
            { "Lance", "Chicago", "female", 49, 160, 57 }, { "Eva", "Washington", "female", 71, 180, 81 } };
      // Set data    
      worksheet.getRange("A1:G9").setValue(data);
      workbook.protect("Ygs_87@ytr");
    //Removes the above protection from the workbook
      workbook.unprotect("Ygs_87@ytr");
    // Save workbook to xlsx
      workbook.save("UnProtectWorkbook.xlsx", SaveFileFormat.Xlsx);

    If a workbook is not protected with a password, the password argument is ignored by the Unprotect method.

    Refer to the following example code to unprotect the protected workbook.

    Java
    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
    // Fetch default worksheet
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    workbook.protect();
    // Removes the above protection from the workbook.
    workbook.unprotect();
    // Saving workbook to xlsx
    workbook.save("2-UnprotectWorkbook.xlsx", SaveFileFormat.Xlsx);