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

介绍

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

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

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

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

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

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

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

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

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

使用Aspose.Cells for Python via .NET实现高级保护设置

Aspose.Cells for Python via .NET 支持所有的高级保护设置。

Aspose.Cells for Python via .NET 提供了一个类,Workbook,代表一个Microsoft Excel文件。Workbook 类包含一个 worksheets 集合,可以访问Excel文件中的每个工作表。一个工作表由 Worksheet 类表示。

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

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

from aspose.cells import SaveFormat, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
excel = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = excel.worksheets[0]
# Restricting users to delete columns of the worksheet
worksheet.protection.allow_deleting_column = False
# Restricting users to delete row of the worksheet
worksheet.protection.allow_deleting_row = False
# Restricting users to edit contents of the worksheet
worksheet.protection.allow_editing_content = False
# Restricting users to edit objects of the worksheet
worksheet.protection.allow_editing_object = False
# Restricting users to edit scenarios of the worksheet
worksheet.protection.allow_editing_scenario = False
# Restricting users to filter
worksheet.protection.allow_filtering = False
# Allowing users to format cells of the worksheet
worksheet.protection.allow_formatting_cell = True
# Allowing users to format rows of the worksheet
worksheet.protection.allow_formatting_row = True
# Allowing users to insert columns in the worksheet
worksheet.protection.allow_formatting_column = True
# Allowing users to insert hyperlinks in the worksheet
worksheet.protection.allow_inserting_hyperlink = True
# Allowing users to insert rows in the worksheet
worksheet.protection.allow_inserting_row = True
# Allowing users to select locked cells of the worksheet
worksheet.protection.allow_selecting_locked_cell = True
# Allowing users to select unlocked cells of the worksheet
worksheet.protection.allow_selecting_unlocked_cell = True
# Allowing users to sort
worksheet.protection.allow_sorting = True
# Allowing users to use pivot tables in the worksheet
worksheet.protection.allow_using_pivot_table = True
# Saving the modified Excel file
excel.save(dataDir + "output.xls", SaveFormat.EXCEL_97_TO_2003)
# Closing the file stream to free all resources
fstream.close()

单元格锁定问题

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

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

也可以使用Aspose.Cells for Python via .NET API对单元格进行锁定。每个单元格可以获得 Style 格式,该格式包含一个布尔属性 is_locked。将 is_locked 属性设置为 truefalse 来锁定或解锁单元格。

from aspose.cells import ProtectionType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
workbook = Workbook(dataDir + "Book1.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
worksheet.cells.get("A1").get_style().is_locked = True
# Finally, Protect the sheet now.
worksheet.protect(ProtectionType.ALL)
workbook.save(dataDir + "output.xlsx")