在自动化数据处理或为最终用户准备报告时,解锁受保护的 Excel 工作表是常见需求。 Aspose.Cells Cloud SDK for .NET 提供强大的 API,允许您在无需手动操作的情况下移除工作表保护。在本指南中,您将学习如何配置 SDK、上传工作簿、调用取消保护操作以及获取已解锁的文件,所有示例均提供可直接运行的代码和 cURL 示例。
设置 Aspose.Cells Cloud SDK for .NET
在开始之前,请确保您具备以下条件:
- .NET 6.0 或更高版本已安装。
- 拥有 Client Id 和 Client Secret 的 Aspose Cloud 账户。
- 可以运行 .NET 命令的终端或 IDE 的访问权限。
通过 NuGet 安装 SDK:
dotnet add package Aspose.cells-Cloud
将您的凭据设置为环境变量,以便 SDK 能够自动进行身份验证:
// No additional code needed here; the SDK reads these variables at runtime.
SDK 将从环境变量中读取 ASPOSE_CLIENT_ID 和 ASPOSE_CLIENT_SECRET。安装包并配置变量后,您即可编写与 Aspose.Cells Cloud 存储交互的代码。下一节将逐步演示每个编程步骤。
Aspose.Cells Cloud .NET 取消保护工作表的逐步指南
步骤 1:加载凭证并创建 API 客户端
首先,从环境中获取您的客户端 ID 和密钥,并创建一个 Configuration 对象。该对象随后用于实例化 CellsApi。
var clientId = Environment.GetEnvironmentVariable("ASPOSE_CLIENT_ID");
var clientSecret = Environment.GetEnvironmentVariable("ASPOSE_CLIENT_SECRET");
var config = new Configuration
{
ClientId = clientId,
ClientSecret = clientSecret,
BasePath = "https://api.aspose.cloud"
};
var cellsApi = new CellsApi(config);
步骤 2:上传受保护的工作簿
将本地 Excel 文件上传到 Aspose Cloud storage。SDK 直接流式传输文件,避免需要在内存中保留整个文件。
using (var fileStream = File.OpenRead(localInputPath))
{
var uploadResult = await cellsApi.UploadFileAsync(remoteFilePath, fileStream);
}
步骤 3:检索工作簿信息并选择工作表
上传后,请求工作簿元数据以获取工作表列表。本示例选择第一张工作表,但您可以指定任意名称。
var workbookResponse = await cellsApi.GetWorkbookAsync(remoteFileName, folder: remoteFolder);
string sheetName = workbookResponse.Workbook.Worksheets[0].Name;
第4步:取消工作表保护
调用 UnprotectWorksheetAsync 方法,传入工作表名称和密码(如果未设置密码,则传 null)。这就是 Aspose.Cells cloud .NET unprotect worksheet 操作的核心。
string password = "myPassword"; // replace with actual password or set to null
var unprotectResponse = await cellsApi.UnprotectWorksheetAsync(remoteFileName, sheetName, password, remoteFolder);
步骤 5:下载已更新的工作簿
最后,将修改后的工作簿下载回本地机器,以验证保护已被移除。
var downloadStream = await cellsApi.DownloadFileAsync(remoteFilePath);
using (var outputStream = File.Create(localOutputPath))
{
await downloadStream.CopyToAsync(outputStream);
}
这些步骤展示了一个完整的 Aspose.Cells cloud .NET unprotect worksheet 工作流,从身份验证到文件检索。
Aspose.Cells Cloud .NET 取消保护工作表的完整工作示例
以下代码展示了在单个控制台应用程序中的完整流程。它使用 async/await 实现非阻塞 I/O,并演示了最佳实践,例如在每个 API 调用后进行错误检查。
using System;
using System.IO;
using System.Threading.Tasks;
using Aspose.Cells.Cloud.SDK.Api;
using Aspose.Cells.Cloud.SDK.Client;
using Aspose.Cells.Cloud.SDK.Model;
namespace AsposeCellsUnprotectDemo
{
class Program
{
static async Task Main(string[] args)
{
// Aspose Cloud credentials – set these environment variables before running
var clientId = Environment.GetEnvironmentVariable("ASPOSE_CLIENT_ID");
var clientSecret = Environment.GetEnvironmentVariable("ASPOSE_CLIENT_SECRET");
var config = new Configuration
{
ClientId = clientId,
ClientSecret = clientSecret,
BasePath = "https://api.aspose.cloud"
};
var cellsApi = new CellsApi(config);
// Local file to upload (must exist) and remote storage details
string localInputPath = "ProtectedWorkbook.xlsx";
string remoteFolder = "Temp";
string remoteFileName = "ProtectedWorkbook.xlsx";
string remoteFilePath = $"{remoteFolder}/{remoteFileName}";
// Upload the protected workbook to Aspose Cloud storage
using (var fileStream = File.OpenRead(localInputPath))
{
var uploadResult = await cellsApi.UploadFileAsync(remoteFilePath, fileStream);
if (uploadResult == null || (uploadResult.Errors != null && uploadResult.Errors.Count > 0))
{
Console.WriteLine("Upload failed.");
return;
}
}
// Retrieve workbook information to get worksheet names
var workbookResponse = await cellsApi.GetWorkbookAsync(remoteFileName, folder: remoteFolder);
if (workbookResponse?.Workbook?.Worksheets == null || workbookResponse.Workbook.Worksheets.Count == 0)
{
Console.WriteLine("Unable to retrieve workbook or worksheets.");
return;
}
// Choose the first worksheet (or specify a name directly)
string sheetName = workbookResponse.Workbook.Worksheets[0].Name;
// Unprotect the worksheet – provide password if the sheet is password‑protected; otherwise null
string password = "myPassword"; // replace with actual password or set to null
var unprotectResponse = await cellsApi.UnprotectWorksheetAsync(remoteFileName, sheetName, password, remoteFolder);
if (unprotectResponse == null || unprotectResponse.Code != 200)
{
Console.WriteLine($"Failed to unprotect worksheet '{sheetName}'.");
return;
}
// Download the updated workbook
var downloadStream = await cellsApi.DownloadFileAsync(remoteFilePath);
string localOutputPath = "UnprotectedWorkbook.xlsx";
using (var outputStream = File.Create(localOutputPath))
{
await downloadStream.CopyToAsync(outputStream);
}
Console.WriteLine($"Worksheet '{sheetName}' has been unprotected and saved as '{localOutputPath}'.");
}
}
}
注意: 此代码示例演示了核心功能。在将其用于项目之前,请确保更新所有文件路径和配置值以匹配实际环境,验证已正确安装所有必需的依赖项,并在开发环境中进行彻底测试。如果遇到任何问题,请参阅官方文档或联系支持团队获取帮助。
使用 cURL 和 REST API 解锁受保护的 Excel 工作表
如果您不想编写 C# 代码,也可以使用普通的 HTTP 调用来实现相同的结果。以下是所需的 cURL 命令。
- 获取访问令牌
curl -X POST "https://api.aspose.cloud/connect/token" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
- 上传受保护的工作簿
curl -X PUT "https://api.aspose.cloud/v3.0/cells/storage/file/Temp/ProtectedWorkbook.xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-T "ProtectedWorkbook.xlsx"
- 取消工作表保护
curl -X POST "https://api.aspose.cloud/v3.0/cells/ProtectedWorkbook.xlsx/worksheets/Sheet1/unprotect?folder=Temp&password=myPassword" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
- 下载未受保护的工作簿
curl -X GET "https://api.aspose.cloud/v3.0/cells/storage/file/Temp/ProtectedWorkbook.xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o "UnprotectedWorkbook.xlsx"
这些命令使用 REST 接口执行相同的 Aspose.Cells cloud .NET unprotect worksheet 操作。欲了解更多详情,请参阅 官方 API 文档。
优化工作表解除保护性能
- 重用
CellsApi实例 - 为每个请求创建新客户端会增加开销。保持在应用程序生命周期内使用单个实例。 - 使用流式处理文件而不是将其全部加载到内存 - SDK 的
UploadFileAsync和DownloadFileAsync方法已经使用流,这可以减少大型工作簿的内存压力。 - 批量取消保护调用 - 如果需要解锁多个工作表,请一次检索工作表列表,并在循环中调用
UnprotectWorksheetAsync,而不是每次都重新查询工作簿。 - 运行异步方法而不阻塞 - 使用
await可以让线程在云操作完成期间处理其他工作,从而提升整体吞吐量。
总结
The Aspose.Cells cloud .NET unprotect worksheet 功能让您能够以编程方式移除 Excel 工作表的保护,便于集成到自动化数据流水线中。
按照上述步骤,您可以使用 .NET SDK 或直接的 REST 调用来进行身份验证、上传、解除保护并下载工作簿。
请记得为生产环境获取合适的许可证;在评估产品时,您可以先使用临时许可证页面。
如需进一步了解,请查阅Aspose.Cells Cloud SDK for .NET文档,并尝试其他工作表操作功能。
常见问题
-
Aspose.Cells cloud .NET 的 unprotect worksheet 如何处理受密码保护的工作表?
当您在UnprotectWorksheetAsync调用中提供正确的密码时,API 会移除保护。密码错误或缺失会导致错误响应。 -
我可以在不编写代码的情况下使用 Aspose.Cells .NET 解锁受保护的 Excel 工作表吗?
是的。可以通过 REST API 使用 cURL 或任何 HTTP 客户端执行相同的操作,如本文的 cURL 部分所示。 -
What licensing is required for Aspose.Cells Cloud SDK for .NET in production?
在生产部署中需要商业许可证。您可以在测试期间从临时许可证页面获取临时许可证。 -
有没有办法一次性批量取消保护一个工作簿中的多个工作表?
您可以使用GetWorkbookAsync检索工作表列表,然后遍历每个名称,对每个工作表调用UnprotectWorksheetAsync。这种方法可以通过 SDK 以编程方式实现。