使用 C++ 保护和取消保护工作表

** 在 MS Excel 中保护和取消保护工作表**

保护和取消保护工作表

  1. 点击 审阅 > 保护工作表
  2. 密码框 中输入密码。
  3. 选择 允许 选项。
  4. 选择 确定,重新输入密码以确认,然后再次选择 确定

** 使用 Aspose.Cells for C++ 保护工作表**

只需要以下简单代码行来实现保护 Excel 文件的工作簿结构。

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

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

    // Create a new workbook
    Workbook workbook;

    // Get the first worksheet
    Worksheet sheet = workbook.GetWorksheets().Get(0);

    // Protect contents of the worksheet
    sheet.Protect(ProtectionType::Contents);

    // Protect worksheet with password
    sheet.GetProtection().SetPassword(u"test");

    // Save as Excel file
    workbook.Save(u"Book1.xlsx");

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

    Aspose::Cells::Cleanup();
}

** 使用 Aspose.Cells for C++ 取消保护工作表**

使用 Aspose.Cells API 轻松取消工作表保护。如果工作表受密码保护,需要正确的密码。

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

using namespace Aspose::Cells;

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

    // Create a new workbook
    Workbook workbook(u"Book1.xlsx");

    // Get the first worksheet
    Worksheet sheet = workbook.GetWorksheets().Get(0);

    // Unprotect the worksheet with password
    sheet.Unprotect(u"password");

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

    std::cout << "Worksheet unprotected successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

高级主题

  • 自 Excel XP 以来的高级保护设置
  • 检测工作表是否受密码保护
  • 保护工作表
  • 取消保护工作表
  • 验证用于保护工作表的密码