自Excel XP以来的高级保护设置

介绍

这些保护设置限制或允许用户:

  • 删除行或列。
  • 编辑内容、对象或方案。
  • 格式化单元格、行或列。
  • 插入行、列或超链接。
  • 选择锁定或解锁的单元格。
  • 使用数据透视表等功能。

Aspose.Cells支持Excel XP或更高版本提供的所有高级保护设置。

使用Excel XP和更高版本的高级保护设置

要查看Excel XP中提供的保护设置:

  1. 工具菜单中选择保护,然后选择保护工作表。将显示一个对话框。

要查看Excel 2016中提供的保护设置

  1. 文件菜单中选择保护工作簿,然后选择保护当前工作表
  2. 审阅菜单中选择保护工作表

按上述步骤将显示一个对话框,您可以在其中允许或限制工作表功能或向工作表添加密码。

使用Aspose.Cells的高级保护设置

Aspose.Cells支持所有高级保护设置。

Aspose.Cells提供了一个代表Microsoft Excel文件的类WorkbookWorkbook类包含一个Worksheets集合,允许访问Excel文件中的每个工作表。一个工作表由Worksheet类表示。

Worksheet类提供了Protection属性,用于应用这些高级保护设置。Protection属性实际上是Protection类的对象,封装了用于禁用或启用限制的几个布尔属性。

下面是一个小例子应用程序。它打开一个 Excel 文件,并使用 Excel XP 及更新版本支持的大部分高级保护设置。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook excel = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = excel.Worksheets[0];
// Restricting users to delete columns of the worksheet
worksheet.Protection.AllowDeletingColumn = false;
// Restricting users to delete row of the worksheet
worksheet.Protection.AllowDeletingRow = false;
// Restricting users to edit contents of the worksheet
worksheet.Protection.AllowEditingContent = false;
// Restricting users to edit objects of the worksheet
worksheet.Protection.AllowEditingObject = false;
// Restricting users to edit scenarios of the worksheet
worksheet.Protection.AllowEditingScenario = false;
// Restricting users to filter
worksheet.Protection.AllowFiltering = false;
// Allowing users to format cells of the worksheet
worksheet.Protection.AllowFormattingCell = true;
// Allowing users to format rows of the worksheet
worksheet.Protection.AllowFormattingRow = true;
// Allowing users to insert columns in the worksheet
worksheet.Protection.AllowFormattingColumn = true;
// Allowing users to insert hyperlinks in the worksheet
worksheet.Protection.AllowInsertingHyperlink = true;
// Allowing users to insert rows in the worksheet
worksheet.Protection.AllowInsertingRow = true;
// Allowing users to select locked cells of the worksheet
worksheet.Protection.AllowSelectingLockedCell = true;
// Allowing users to select unlocked cells of the worksheet
worksheet.Protection.AllowSelectingUnlockedCell = true;
// Allowing users to sort
worksheet.Protection.AllowSorting = true;
// Allowing users to use pivot tables in the worksheet
worksheet.Protection.AllowUsingPivotTable = true;
// Saving the modified Excel file
excel.Save(dataDir + "output.xls", SaveFormat.Excel97To2003);
// Closing the file stream to free all resources
fstream.Close();

单元格锁定问题

如果您希望限制用户编辑单元格,则需要在应用任何保护设置之前锁定单元格。否则,即使工作表受到保护,用户也可以编辑单元格。在Microsoft Excel XP中,可以通过以下对话框锁定单元格:

在Excel XP中锁定单元格的对话框
todo:image_alt_text

也可以使用Aspose.Cells API锁定单元格。每个单元格可以获得包含布尔属性IsLockedStyle格式。将IsLocked属性设置为truefalse以锁定或解锁单元格。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["A1"].GetStyle().IsLocked = true;
// Finally, Protect the sheet now.
worksheet.Protect(ProtectionType.All);
workbook.Save(dataDir + "output.xlsx");