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

介绍

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

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

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

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

查看 Excel XP 中可用的保护设置:

  1. 来自工具菜单,选择保护其次是保护表.将显示一个对话框。

查看 Excel 2016 中可用的保护设置

  1. 来自文件菜单,选择保护工作簿其次是保护当前工作表.
  2. 选择保护表在里面审查菜单。

按照上面提到的步骤将显示一个对话框,您可以在其中允许或限制工作表功能或将密码应用于工作表。

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

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

Aspose.Cells提供了一个类,工作簿,代表一个 Microsoft Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。工作表由工作表班级。

工作表类提供了保护用于应用这些高级保护设置的属性。这保护财产实际上是保护封装了几个用于禁用或启用限制的布尔属性的类。

下面是一个小的示例应用程序。它打开一个 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();

Cell 锁定问题

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

在 Excel XP 中锁定单元格的对话框
待办事项:图片_替代_文本

也可以使用 Aspose.Cells API 锁定单元格。每个细胞都能得到风格包含布尔属性的格式,被锁住了.设置被锁住了财产给真的要么错误的锁定或解锁单元格。

// 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");