In today’s data-driven world, Excel workbooks often contain sensitive business, financial, or personal information. To ensure confidentiality and prevent unauthorized access, it’s essential to password protect Excel files or lock Excel worksheets. So, with just a few lines of C#, developers can easily encrypt Excel documents, protect individual worksheets, and manage secure workbooks without needing Microsoft Excel installed.

Excel Processing API

The Aspose.Cells Cloud SDK for .NET enables developers to password lock Excel files using simple API calls. It allows you to control access by assigning a secure password to a workbook or specific worksheet, preventing unauthorized edits or data exposure.

To get started, install the SDK via NuGet Package Manager:

Install-Package Aspose.Cells-Cloud

Then, sign up for a free account on the Aspose Cloud Dashboard to obtain your Client ID and Client Secret credentials.

Add a Password to Excel Workbook in C#

Here’s a practical example of how to password protect an Excel file in C# using Aspose.Cells Cloud SDK for .NET.

  1. Create an instance of CellsApi class where we use client credentials as argument.
string clientId = "YOUR_CLIENT_ID";
string clientSecret = "YOUR_CLIENT_SECRET";
var cellsApi = new CellsApi(clientId, clientSecret);
  1. Upload the input PDF to cloud storage.
cellsApi.UploadFile(inputExcel, System.IO.File.OpenRead(inputExcel));
  1. Create an instance of WorkbookEncryptionRequest class and define encryption type and password.
WorkbookEncryptionRequest encryption = new WorkbookEncryptionRequest
{
    Password = "SecurePass123",
    KeyLength = 128,
    EncryptionType = "XOR"
};
  1. Call the API to password protect the Excel workbook and save the updated file to cloud storage.
var response = cellsApi.CellsWorkbookPostEncryptDocument(inputExcel, encryption, null);
  • The API supports the following encryption types
  1. XOR (basic)
  2. Compatible
  3. EnhancedCryptographicProviderV1
  4. StrongCryptographicProvider

Password Protect Excel Worksheet using C#

Other than encrypting the whole workbook, the API also supports the feature to password protect Excel worksheets individually. This ensures users can view but not modify sensitive sheets.

Lock Excel File with Password using cURL

You can also password lock an Excel file using cURL and the Aspose.Cells Cloud REST API. This approach is great for automation and CI/CD workflows.

Step 1: Generate an Access Token:

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=XXXXXX-XXXXXX-XXXXXXX&client_secret=XXXXXXXXXXXXX" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Step 2: Upload the Excel File: Use the following command to upload input Excel to cloud storage:

curl -v "https://api.aspose.cloud/v3.0/cells/storage/file/{filePath}" \
-X PUT \
-F file=@{protectedFilePath} \
-H "Content-Type: multipart/form-data" \
-H "Authorization: Bearer {ACCESS_TOKEN}"

Step 3: Apply Password Protection:

curl -v "https://api.aspose.cloud/v3.0/cells/{excelFile}]/encryption" \
-X POST \
-H  "accept: application/json" \
-H  "authorization: Bearer {ACCESS_TOKEN}" \
-H  "Content-Type: application/json" \
-d "{  \"EncryptionType\": \"XOR\",  \"KeyLength\": 128,  \"Password\": \"Aspose123\"}"

Replace:

  • excelFile - Input Excel workbook.
  • ACCESS_TOKEN - JWT token generated above.
  • Once executed, your Excel document will be password protected and securely stored in the cloud.

Online Excel Password Protector App

If you prefer a no-code approach, try the free Online Excel Password Protect Tool.It’s powered by Aspose.Cells Cloud and allows you to upload, set a password, and download the protected Excel file instantly.

password protect excel

Concluding Remarks

In this article, we have learned that securing your Excel files is a vital step in data protection and compliance. With Aspose.Cells Cloud SDK for .NET, you can easily lock Excel files with passwords, protect worksheets, and encrypt entire workbooks without the need for Excel installed locally.

Frequently Asked Questions (FAQs)

  1. Can I lock only specific worksheets in Excel? Yes. You can use the CellsProtectSheet() method to password protect Excel worksheets individually while keeping others editable.

  2. Can I remove a password lock from an Excel file programmatically? Yes. You can use the CellsWorkbookDeleteDecryptDocument() API call to unlock a password-protected Excel file when the correct password is provided.

  3. Do I need Microsoft Excel installed for this API to work? No. The Aspose.Cells Cloud SDK works entirely in the cloud — no local Excel installation is required to password protect Excel files.

  4. Can I try password protecting Excel files for free? Yes! Visit the Aspose.Cells Cloud Free Trial or use the online password protection tool to test the functionality without coding.

Please visit the following links to learn more about: