C++: Protect or Unprotect Excel Documents

When an Excel document contains some sensitive financial or confidential data, it is essential to protect it during transmission. In MS Excel, you can encrypt an entire workbook with a password to prevent unauthorized access, or just lock selected Excel sheets or individual cells to avoid unwanted modifications. In this article, you will learn how to programmatically protect and unprotect a workbook or a worksheet using Spire.XLS for C++.

Install Spire.XLS for C++

There are two ways to integrate Spire.XLS for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.XLS for C++ in a C++ Application

Password Protect an Entire Workbook in C++

By encrypting an Excel document with a password, you ensure that only you and authorized individuals can read or edit it. The following are the steps to password protect a workbook.

  • Create a Workbook object.
  • Load an Excel document using Workbook->LoadFromFile() method.
  • Protect the Excel workbook with a password using Workbook->Protect() method.
  • Save the result document using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\Budget.xlsx";
	std::wstring outputFile = L"Output\\EncryptWorkbook.xlsx";

	//Create a Workbook object
	Workbook* workbook = new Workbook();

	//Load an Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Protect workbook with a password
	workbook->Protect(L"e-iceblue");

	//Save the result document
	workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013);
	workbook->Dispose();
}

C++: Protect or Unprotect Excel Documents

Protect a Worksheet with a Specific Protection Type in C++

If you wish to grant people permission to read your Excel document but restrict the types of modifications they are allowed to make on a worksheet, you can protect the worksheet with a specific protection type. The table below lists a variety of pre-defined protection types under the SheetProtectionType enumeration.

Protection Type Allow users to
Content Modify or insert content.
DeletingColumns Delete columns.
DeletingRows Delete rows.
Filtering Set filters.
FormattingCells Format cells.
FormattingColumns Format columns.
FormattingRows Format rows.
InsertingColumns Insert columns.
InsertingRows Insert rows.
InsertingHyperlinks Insert hyperlinks.
LockedCells Select locked cells.
UnlockedCells Select unlocked cells.
Objects Modify drawing objects.
Scenarios Modify saved scenarios.
Sorting Sort data.
UsingPivotTables Use pivot table and pivot chart.
All Do any operations listed above on the protected worksheet.
None Do nothing on the protected worksheet.

The following are the steps to protect a worksheet with a specific protection type.

  • Create a Workbook object.
  • Load an Excel document using Workbook->LoadFromFile() method.
  • Get a specified worksheet using Workbook->GetWorksheets()->Get() method.
  • Protect the worksheet with a protection type using Worksheet->XlsWorksheetBase::Protect (LPCWSTR_S password, SheetProtectionType options) method.
  • Save the result document using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\Budget.xlsx";
	std::wstring outputFile = L"Output\\ProtectWorksheet.xlsx";

	//Create a Workbook object
	Workbook* workbook = new Workbook();

	//Load an Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Get the first worksheet
	Worksheet* sheet = workbook->GetWorksheets()->Get(0);
	
	//Protect the worksheet with the permission password and the specific protect type
	sheet->XlsWorksheetBase::Protect(L"e-iceblue", SheetProtectionType::None);

	//Save the result document
	workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013);
	workbook->Dispose();
}

C++: Protect or Unprotect Excel Documents

Allow Users to Edit Ranges in a Protected Worksheet in C++

In certain cases, you may need to allow users to be able to edit selected ranges in a protected worksheet. The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document using Workbook->LoadFromFile() method.
  • Get a specified worksheet using Workbook->GetWorksheets()->Get() method.
  • Specify the editable cell ranges using Worksheet->AddAllowEditRange() method.
  • Protect the worksheet with a password using Worksheet->XlsWorksheetBase::Protect() method.
  • Save the result document using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\Budget.xlsx";
	std::wstring outputFile = L"Output\\AllowEditRange.xlsx";

	//Create a Workbook object
	Workbook* workbook = new Workbook();

	//Load an Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Get the first worksheet
	Worksheet* sheet = workbook->GetWorksheets()->Get(0);

	//Define a range that allow users to edit while sheet is protected
	sheet->AddAllowEditRange(L"EditableRange", sheet->GetRange(L"A8:A12"));

	//Protect the worksheet with a password
	sheet->XlsWorksheetBase::Protect(L"TestPassword");

	//Save the result document
	workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013);
	workbook->Dispose();
}

C++: Protect or Unprotect Excel Documents

Lock Specific Cells in a Worksheet in C++

Normally, the locked option is enabled for all cells in a worksheet. Therefore, before locking a cell or range of cells, all cells must be unlocked. Keep in mind that locking cells doesn't take effect until the worksheet is protected. The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document using Workbook->LoadFromFile() method.
  • Get a specified worksheet using Workbook->GetWorksheets()->Get() method.
  • Access the used range in the worksheet and then unlock all cells in the range.
  • Access specific cells and then lock them by setting the parameter of XlsRange->GetStyle()->SetLocked() method to true.
  • Protect the worksheet using Worksheet->XlsWorksheetBase::Protect() method.
  • Save the result document using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\Budget.xlsx";
	std::wstring outputFile = L"Output\\LockCells.xlsx";

	//Create a Workbook object
	Workbook* workbook = new Workbook();

	//Load an Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Get the first worksheet
	Worksheet* sheet = workbook->GetWorksheets()->Get(0);
	
	//Unlock all cells in the used range of the worksheet
	sheet->XlsWorksheet::GetRange()->GetStyle()->SetLocked(false);

	//Lock specific cells
	XlsRange* cells = sheet->GetRange(L"A1:D1");
	cells->GetStyle()->SetLocked(true);

	//Protect the worksheet with password
	sheet->XlsWorksheetBase::Protect(L"TestPassword");

	//Save the result document
	workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013);
	workbook->Dispose();
}

C++: Protect or Unprotect Excel Documents

Unprotect a Password Protected Worksheet in C++

To remove the protection of a password-protected worksheet, invoke the Worksheet->XlsWorksheetBase::Unprotect() method and pass in the original password as a parameter. The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document using Workbook->LoadFromFile() method.
  • Get a specified worksheet using Workbook->GetWorksheets()->Get() method.
  • Unprotect the worksheet with the original password using Worksheet->XlsWorksheetBase::Unprotect(LPCWSTR_S password) method.
  • Save the result document using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\ProtectWorksheet.xlsx";
	std::wstring outputFile = L"Output\\UnprotectWorksheet.xlsx";

	//Create a Workbook object
	Workbook* workbook = new Workbook();

	//Load an Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Get the first worksheet
	Worksheet* sheet = workbook->GetWorksheets()->Get(0);

	//Unprotect the worksheet using the specified password
	sheet->XlsWorksheetBase::Unprotect(L"e-iceblue");

	//Save the result document
	workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013);
	workbook->Dispose();
}

Remove or Reset Password of an Encrypted Workbook in C++

To remove or reset password of an encrypted workbook, you can use the Workbook->UnProtect() method and the Workbook->Protect() method respectively. The following are the steps to load an encrypted Excel document and delete or change the password of it.

  • Create a Workbook object.
  • Specify the open password using Workbook->SetOpenPassword() method.
  • Load an Excel document using Workbook->LoadFromFile() method.
  • Remove the encryption using Workbook->UnProtect() method. Or change the password using Workbook->Protect() method.
  • Save the result document using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

	//Specify the input and output file paths
	std::wstring inputFile = L"Output\\EncryptWorkbook.xlsx";
	std::wstring outputFile = L"Output\\Unprotect.xlsx";

	//Create a Workbook object
	Workbook* workbook = new Workbook();

	//Specify the open password
	workbook->SetOpenPassword(L"e-iceblue");

	//Load an Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Unprotect workbook
	workbook->UnProtect();

	//Reset password
	//workbook->Protect(L"newpassword");

	//Save the result document
	workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013);
	workbook->Dispose();
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.