Microsoft Excel is a popular software for managing data in organizations. It is mainly due to its advanced data manipulation and representation features. In this technological age, documents are shared virtually instead of printed copies. These files may contain complex and sensitive data. So to maintain such files’ integrity, you might need to guard them against modification. Excel’s protection features come in handy for this purpose. In this article, you will learn how to protect and unprotect Excel files programmatically using C++.
- C++ API to Protect and Unprotect Excel Files
- Protect Excel Files using C++
- Unprotect Excel Workbook using C++
- Protect Excel Worksheet using C++
- Unprotect Excel Worksheet using C++
- Get a Free License
C++ API to Protect and Unprotect Excel Files – Free Download
Aspose.Cells for C++ is a native C++ library that allows you to manipulate Excel files without requiring Microsoft Excel to be installed. Therefore, it is used for automating many Excel workflows. The API provides a bunch of useful features, including protecting and unprotecting Excel files. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.Cells.Cpp
Protect Excel Files using C++
Aspose.Cells for C++ provides the following protection types for protecting Excel files.
- All: Protect the whole data so that the user cannot modify anything.
- Contents: Protect the content of the worksheet.
- Objects: Protect the objects in the worksheet so that the user cannot modify drawing objects.
- Scenarios: Protect the scenarios in the worksheet.
- Structure: Protect the structure of the workbook.
- Windows: Protect the saved windows from modification.
- None: No protection applied.
The following are the steps for protecting Excel files using C++.
- Load the Excel file using the IWorkbook class.
- Protect the file with IWorkbook->Protect(Aspose::Cells::ProtectionType protectionType, intrusive_ptr<Aspose::Cells::Systems::String>password) method.
- Save the protected Excel file with the IWorkbook->Save(intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following code sample shows how to protect Excel files using C++.
Unprotect Excel Workbook using C++
The following are the steps to unprotect an Excel file.
- Load the Excel file using the IWorkbook class.
- Unprotect the file with the IWorkbook->Unprotect(intrusive_ptr<Aspose::Cells::Systems::String> password) method.
- Set the password to NULL using the IWorkbook->GetISettings()->SetPassword(intrusive_ptr<Aspose::Cells::Systems::String> value) method.
- Save the unprotected Excel file with the IWorkbook->Save(intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following code sample shows how to unprotect Excel files.
Protect Excel Worksheet using C++
In addition to protecting Excel files, you can also protect individual worksheets. Please use the following steps to safeguard Excel worksheets.
- Firstly, load the Excel file using the IWorkbook class.
- Get the worksheet that you want to protect from the worksheet collection using the IWorkbook->GetIWorksheets()->GetObjectByIndex( Aspose::Cells::Systems::Int32 index) method.
- Get the worksheet’s protection using the IWorksheet->GetIProtection() method.
- Set the protection options such as SetAllowEditingContent (bool value).
- Set the password using the IProtection->SetPassword (intrusive_ptr<Aspose::Cells::Systems::String> value) method.
- Finally, save the Excel file with the protected worksheet using IWorkbook->Save(intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following code sample shows how to protect Excel worksheet.
Unprotect Excel Worksheet using C++
The following are the steps for unprotecting Excel worksheets.
- Load the Excel file using the IWorkbook class.
- Access the protected worksheet using IWorkbook->GetIWorksheets()->GetObjectByIndex( Aspose::Cells::Systems::Int32 index) method.
- Unprotect the worksheet using the IWorksheet->Unprotect(intrusive_ptr<Aspose::Cells::Systems::String> password) method.
- Lastly, save the Excel file with the unprotected worksheet using IWorkbook->Save(intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following code sample shows how to unprotect Excel worksheet using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to protect and unprotect Excel files using C++. In addition to that, you have also learned how to guard worksheets against modifications. Aspose.Cells for C++ is a vast Excel manipulation API for automating tasks such as replacing text in cells or processing complex data represented in graphs and charts. You can explore the API in detail by using the official documentation.