使用 C++ 保护和取消保护工作表
Contents
[
Hide
]
为防止其他用户意外或故意更改、移动或删除工作表中的数据,您可以锁定 Excel 工作表上的单元格,然后使用密码保护工作表。
** 在 MS Excel 中保护和取消保护工作表**

- 点击 审阅 > 保护工作表。
- 在 密码框 中输入密码。
- 选择 允许 选项。
- 选择 确定,重新输入密码以确认,然后再次选择 确定。
** 使用 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();
}