C++を使用してワークシートの保護と解除
Contents
[
Hide
]
ワークシート上のデータの変更、移動、または削除を他のユーザーが誤ってまたは意図的に防ぐために、Excelワークシートのセルをロックし、その後シートをパスワードで保護できます。
MS Excelのワークシートを保護および解除
- レビュー > ワークシートの保護 をクリックします。
- パスワードボックス にパスワードを入力します。
- 許可 オプションを選択します。
- OK を選択し、パスワードを再入力して確認し、その後再度 OK を選択します。
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();
}