Protect and Unprotect Workbook Structure with C++

Protect and Unprotect Workbook Structure in MS Excel

protect and unprotect workbook structure

  1. Click Review > Protect Workbook.
  2. Enter a password in the Password box.
  3. Select OK, re-enter the password to confirm it, and then select OK again.

Protect Workbook Structure Using Aspose.Cells for C++

Only need the following simple lines of code to implement protecting workbook structure of Excel files.

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Create a new workbook
    Workbook workbook;

    // Protect workbook structure with a password
    workbook.Protect(ProtectionType::Structure, u"password");

    // Save the workbook to a file
    workbook.Save(u"Book1.xlsx");

    std::cout << "Workbook created and protected successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

Unprotect Workbook Structure Using Aspose.Cells for C++

Unprotecting workbook structure is easy with Aspose.Cells API.

#include <iostream>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Open an Excel file which workbook structure is protected.
    U16String inputFilePath = u"Book1.xlsx";
    Workbook workbook(inputFilePath);

    // Unprotect workbook structure.
    workbook.Unprotect(u"password");

    // Save Excel file.
    workbook.Save(inputFilePath);

    std::cout << "Workbook structure unprotected and saved successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}