内容表
概述
SpreadsheetLocker
在 Aspose.Cells.LowCode
通过使用密码来简化安全化 Excel 文件,以限制访问和编辑能力. 非常适合在您的 .NET 应用程序中轻松地保护敏感数据并满足遵守要求。
为什么要保护Excel扩展表?
- 安全敏感数据:防止未经授权的访问和修改。
- 保持数据完整性:关闭完成的报告,以保持准确性和一致性。
- 确保遵守:通过提供关键业务信息来满足监管标准。
使用 SpreadsheetLocker 的好处
- 易于实施:用最小编码应用或删除密码。
- 灵活的密码管理:开放和编辑文档的单独密钥。
- Dynamic Security:使用提供商以动态的方式安全地创建或获取密码。
- 快速集成(Quick Integration):与现有 .NET 应用程序无缝整合。
原則
- 通过 NuGet 安装 Aspose.Cells.LowCode:
Install-Package Aspose.Cells.LowCode
- .NET 6.0 或更高版本
- 进口所需的名称空间:
using Aspose.Cells;
using Aspose.Cells.LowCode;
步骤防护指南
使用密码保护
快速应用密码到 Excel 文件:
public class SimpleProtectionProvider : AbstractLowCodeProtectionProvider
{
private readonly string openPwd, writePwd;
public SimpleProtectionProvider(string openPwd, string writePwd)
{
this.openPwd = openPwd;
this.writePwd = writePwd;
}
public override string GetOpenPassword() => openPwd;
public override string GetWritePassword() => writePwd;
}
var loadOpts = new LowCodeLoadOptions { InputFile = "sensitive.xlsx" };
var saveOpts = new LowCodeSaveOptions { SaveFormat = SaveFormat.Xlsx, OutputFile = "protected.xlsx" };
var provider = new SimpleProtectionProvider("open123", "modify123");
SpreadsheetLocker.Process(loadOpts, saveOpts, provider);
删除密码保护
删除以前使用的保护:
var removeProvider = new SimpleProtectionProvider(string.Empty, string.Empty);
SpreadsheetLocker.Process(
new LowCodeLoadOptions { InputFile = "protected.xlsx" },
new LowCodeSaveOptions { SaveFormat = SaveFormat.Xlsx, OutputFile = "unlocked.xlsx" },
removeProvider
);
完整的C#代码示例
应用和删除 Excel 密码保护的终端示范:
using System;
using Aspose.Cells.LowCode;
namespace ProtectionExample
{
public class SimpleProtectionProvider : AbstractLowCodeProtectionProvider
{
private readonly string openPwd, writePwd;
public SimpleProtectionProvider(string openPwd, string writePwd)
{
this.openPwd = openPwd;
this.writePwd = writePwd;
}
public override string GetOpenPassword() => openPwd;
public override string GetWritePassword() => writePwd;
}
class Program
{
static void Main()
{
// Apply Protection
var loadOpts = new LowCodeLoadOptions { InputFile = "report.xlsx" };
var saveOpts = new LowCodeSaveOptions { SaveFormat = SaveFormat.Xlsx, OutputFile = "report_protected.xlsx" };
var provider = new SimpleProtectionProvider("OpenMe", "EditMe");
SpreadsheetLocker.Process(loadOpts, saveOpts, provider);
Console.WriteLine("Workbook protected successfully.");
// Remove Protection
var removeProvider = new SimpleProtectionProvider(string.Empty, string.Empty);
SpreadsheetLocker.Process(
new LowCodeLoadOptions { InputFile = "report_protected.xlsx" },
new LowCodeSaveOptions { SaveFormat = SaveFormat.Xlsx, OutputFile = "report_unlocked.xlsx" },
removeProvider
);
Console.WriteLine("Protection removed successfully.");
}
}
}
性能与安全提示
- Batch Protection:使用插槽或插件脚本的多个文件的自动安全性。
- Dynamic Password Retrieval:实施安全的密码从漏洞或用户输入获取。
- 审计登录(Audit Logging):注册密码应用和删除行动用于审查。
常见问题和故障排除
问题 | 解决方案 |
---|---|
不正确的密码错误 | 仔细检查密码;它们是案例敏感的。 |
文件锁定问题 | 确保 Excel 文件不在其他程序中打开。 |
未支持的文件格式 | 查看支持的文件格式(XLS、XLSX、XLTM、 XLSM)。 |
常见问题(FAQ)
Q1:单个表可以单独保护吗?
是的,使用 Aspose.Cells’ 完整 API (Worksheet.Protect
).
Q2:如何让用户安全输入密码? 创建一个自定义提供商,以确保安全的密码输入。
Q3:是否可以完全删除保护? 是的,使用如上所示的空密码。