保护和取消保护工作表

保护工作表

当工作表受到保护时,用户可以执行的操作将受到限制。例如,他们不能输入数据,插入或删除行或列等。Microsoft Excel中的一般保护选项是:

  • 内容
  • 对象
  • 场景

受保护的工作表不会隐藏或保护敏感数据,因此它不同于文件加密。通常,工作表保护适用于演示目的。它可以防止最终用户修改工作表中的数据、内容和格式。

添加或删除保护

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

Worksheet 类提供了[保护](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#protect(int)用于对工作表应用保护的方法。 Protect 方法接受以下参数:

  • 保护类型,要在工作表上应用的保护类型。保护类型是在保护类型枚举。
  • 新密码,用于保护工作表的新密码。
  • Old Password,旧密码,如果工作表已经受密码保护。如果工作表尚未受到保护,则只需传递一个空值。

ProtectionType 枚举包含以下预定义的保护类型:

保护类型 描述
全部 用户不能修改此工作表上的任何内容
内容 用户不能在此工作表中输入数据
对象 用户不能修改绘图对象
场景 用户无法修改已保存的场景
结构体 用户不能修改保存的结构
视窗 用户不能修改保存的窗口
没有任何 无保护

下面的示例显示了如何使用密码保护工作表。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ProtectingWorksheet.class) + "worksheets/";
// Instantiating a Excel object by excel file path
Workbook excel = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
WorksheetCollection worksheets = excel.getWorksheets();
Worksheet worksheet = worksheets.get(0);
Protection protection = worksheet.getProtection();
// The following 3 methods are only for Excel 2000 and earlier formats
protection.setAllowEditingContent(false);
protection.setAllowEditingObject(false);
protection.setAllowEditingScenario(false);
// Protects the first worksheet with a password "1234"
protection.setPassword("1234");
// Saving the modified Excel file in default format
excel.save(dataDir + "ProtectingWorksheet_out.xls");
// Print Message
System.out.println("Sheet protected successfully.");

上面的代码对工作表进行保护后,打开工作表检查保护。打开文件并尝试向工作表添加一些数据后,将显示以下对话框:

警告用户无法修改工作表的对话框

待办事项:图片_替代_文本

要处理工作表,请通过选择取消保护工作表保护, 然后取消保护工作表来自工具菜单项如下图所示。

选择取消保护工作表菜单项

待办事项:图片_替代_文本

将打开一个对话框,提示输入密码。

输入密码以取消保护工作表

待办事项:图片_替代_文本

保护少数 Cells

在某些情况下,您可能只需要锁定工作表中的几个单元格。如果要锁定工作表中的某些特定单元格,则必须解锁工作表中的所有其他单元格。工作表中的所有单元格都已初始化为锁定,您可以检查此打开任何 excel 文件到 MS Excel 并单击格式 | Cells…以显示格式 Cells对话框,然后单击“保护”选项卡,可以看到标记为“已锁定”的复选框默认处于选中状态。

以下是实现任务的两种方法。

方法一:

以下几点描述了如何使用 MS Excel 锁定几个单元格。此方法适用于Microsoft Office Excel 97、2000、2002、2003及以上版本。

  1. 通过单击“全选”按钮(第 1 行行号正上方和列字母 A 左侧的灰色矩形)选择整个工作表。
  2. 单击格式菜单上的 Cells,单击保护选项卡,然后清除锁定复选框。

这将解锁工作表上的所有单元格

  1. 仅选择要锁定的单元格并重复步骤 2,但这次选中“锁定”复选框。
  2. 工具菜单,选择保护 , 点击保护表 然后点击好的.

方法二:

在这个方法中,我们只使用 Aspose.Cells API 来完成任务。

以下示例展示了如何保护工作表中的几个单元格。它首先解锁工作表中的所有单元格,然后锁定其中的 3 个单元格(A1、B1、C1)。最后,它保护工作表。行/列有一个 Style API,它进一步包含一个 set Locked 方法。您可以使用此方法锁定或解锁行/列。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ProtectingSpecificCellsinaWorksheet.class) + "worksheets/";
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.getWorksheets().get(0);
// Define the style object.
Style style;
// Define the styleflag object.
StyleFlag flag;
flag = new StyleFlag();
flag.setLocked(true);
// Loop through all the columns in the worksheet and unlock them.
for (int i = 0; i <= 255; i++) {
style = sheet.getCells().getColumns().get(i).getStyle();
style.setLocked(false);
sheet.getCells().getColumns().get(i).applyStyle(style, flag);
}
// Lock the three cells...i.e. A1, B1, C1.
style = sheet.getCells().get("A1").getStyle();
style.setLocked(true);
sheet.getCells().get("A1").setStyle(style);
style = sheet.getCells().get("B1").getStyle();
style.setLocked(true);
sheet.getCells().get("B1").setStyle(style);
style = sheet.getCells().get("C1").getStyle();
style.setLocked(true);
sheet.getCells().get("C1").setStyle(style);
// Save the excel file.
wb.save(dataDir + "PSpecificCellsinaWorksheet_out.xls", FileFormatType.EXCEL_97_TO_2003);
// Print Message
System.out.println("Cells protected successfully.");

保护工作表中的一行

Aspose.Cells 允许您轻松锁定工作表中的任何行。在这里,我们可以利用[应用样式()](https://reference.aspose.com/cells/java/com.aspose.cells/row#applyStyle(com.aspose.cells.Style,%20com.aspose.cells.StyleFlag) ) 的方法类将 Style 应用于工作表中的特定行。这个方法有两个参数:一个风格对象和风格旗帜结构,其中包含与应用格式相关的所有成员。

下面的示例演示如何保护工作表中的一行。它首先解锁工作表中的所有单元格,然后锁定其中的第一行。最后,它保护工作表。行/列的样式 API 进一步包含 setCellLocked 方法。您可以使用 StyleFlag 结构锁定或解锁行/列。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ProtectRowWorksheet.class) + "worksheets/";
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.getWorksheets().get(0);
// Define the style object.
Style style;
// Define the styleflag object.
StyleFlag flag;
// Loop through all the columns in the worksheet and unlock them.
for (int i = 0; i <= 255; i++) {
style = sheet.getCells().getRows().get(i).getStyle();
style.setLocked(false);
flag = new StyleFlag();
flag.setLocked(true);
sheet.getCells().getRows().get(i).applyStyle(style, flag);
}
// Get the first Roww style.
style = sheet.getCells().getRows().get(1).getStyle();
// Lock it.
style.setLocked(true);
// Instantiate the flag.
flag = new StyleFlag();
// Set the lock setting.
flag.setLocked(true);
// Apply the style to the first row.
sheet.getCells().getRows().get(1).applyStyle(style, flag);
sheet.protect(ProtectionType.ALL);
// Save the excel file.
wb.save(dataDir + "ProtectRowWorksheet_out.xls", FileFormatType.EXCEL_97_TO_2003);
// Print Message
System.out.println("Row protected successfully.");

保护工作表中的列

Aspose.Cells 允许您轻松锁定工作表中的任何列。在这里,我们可以利用[应用样式()](https://reference.aspose.com/cells/java/com.aspose.cells/column#applyStyle(com.aspose.cells.Style,%20com.aspose.cells.StyleFlag) ) 的方法柱子类将 Style 应用于工作表中的特定列。这个方法有两个参数:一个风格对象和风格旗帜结构,其中包含与应用格式相关的所有成员。

以下示例显示如何保护工作表中的列。它首先解锁工作表中的所有单元格,然后锁定其中的第一列。最后,它保护工作表。行/列有一个 Style API,它进一步包含一个 set Locked 方法。您可以使用 StyleFlag 结构锁定或解锁行/列。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ProtectColumnWorksheet.class) + "worksheets/";
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.getWorksheets().get(0);
// Define the style object.
Style style;
// Define the styleflag object.
StyleFlag flag;
// Loop through all the columns in the worksheet and unlock them.
for (int i = 0; i <= 255; i++) {
style = sheet.getCells().getColumns().get(i).getStyle();
style.setLocked(false);
flag = new StyleFlag();
flag.setLocked(true);
sheet.getCells().getColumns().get(i).applyStyle(style, flag);
}
// Get the first column style.
style = sheet.getCells().getColumns().get(0).getStyle();
// Lock it.
style.setLocked(true);
// Instantiate the flag.
flag = new StyleFlag();
// Set the lock setting.
flag.setLocked(true);
// Apply the style to the first column.
sheet.getCells().getColumns().get(0).applyStyle(style, flag);
sheet.protect(ProtectionType.ALL);
// Save the excel file.
wb.save(dataDir + "PColumnWorksheet_out.xls", FileFormatType.EXCEL_97_TO_2003);
// Print Message
System.out.println("Column protected successfully.");

取消保护工作表

保护工作表自 Excel XP 以来的高级保护设置讨论了保护工作表的不同方法。如果开发人员需要在运行时从受保护的工作表中删除保护,以便可以对文件进行一些更改,该怎么办?使用 Aspose.Cells 可以轻松完成此操作。

使用 Microsoft Excel

要取消对工作表的保护:

来自工具菜单,选择保护其次是取消保护工作表.

选择取消保护工作表

待办事项:图片_替代_文本

保护被移除,除非工作表受密码保护。在这种情况下,会出现一个对话框提示输入密码。

输入密码以取消保护工作表

待办事项:图片_替代_文本

使用 Aspose.Cells

可以通过调用工作表班级'[取消保护](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#unprotect()) 方法。这[取消保护](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#unprotect()方法可以以两种方式使用,如下所述。

取消保护简单保护的工作表

简单保护的工作表是不受密码保护的工作表。可以通过调用 unprotect 方法而不传递参数来解除对此类工作表的保护。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(UnprotectingSimplyProtectedWorksheet.class) + "worksheets/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.get(0);
Protection protection = worksheet.getProtection();
// The following 3 methods are only for Excel 2000 and earlier formats
protection.setAllowEditingContent(false);
protection.setAllowEditingObject(false);
protection.setAllowEditingScenario(false);
// Unprotecting the worksheet
worksheet.unprotect();
// Save the excel file.
workbook.save(dataDir + "USPWorksheet_out.xls", FileFormatType.EXCEL_97_TO_2003);
// Print Message
System.out.println("Worksheet unprotected successfully.");

取消保护受密码保护的工作表

受密码保护的工作表是受密码保护的工作表。通过调用将密码作为参数的 Unprotect 方法的重载版本,可以解除对此类工作表的保护。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(UnprotectProtectSheet.class) + "worksheets/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.get(0);
// Unprotecting the worksheet
worksheet.unprotect("aspose");
// Save the excel file.
workbook.save(dataDir + "UnprotectProtectSheet_out.xls", FileFormatType.EXCEL_97_TO_2003);
// Print Message
System.out.println("Worksheet unprotected successfully.");

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

保护工作表在 Microsoft Excel 97 和 2000 中讨论了保护工作表。但是自从 Excel 2002 或 XP 发布以来,Microsoft 增加了许多高级保护设置。这些保护设置限制或允许用户:

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

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

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

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

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

    在 Excel XP 中显示保护选项的对话框

待办事项:图片_替代_文本

  1. 允许或限制工作表功能或应用密码。

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AdvancedProtectionSettingsUsingAsposeCells.class) + "worksheets/";
// Instantiating a Workbook object by excel file path
Workbook excel = new Workbook(dataDir + "book1.xlsx");
WorksheetCollection worksheets = excel.getWorksheets();
Worksheet worksheet = worksheets.get(0);
Protection protection = worksheet.getProtection();
// Restricting users to delete columns of the worksheet
protection.setAllowDeletingColumn(false);
// Restricting users to delete row of the worksheet
protection.setAllowDeletingRow(false);
// Restricting users to edit contents of the worksheet
protection.setAllowEditingContent(false);
// Restricting users to edit objects of the worksheet
protection.setAllowEditingObject(false);
// Restricting users to edit scenarios of the worksheet
protection.setAllowEditingScenario(false);
// Restricting users to filter
protection.setAllowFiltering(false);
// Allowing users to format cells of the worksheet
protection.setAllowFormattingCell(true);
// Allowing users to format rows of the worksheet
protection.setAllowFormattingRow(true);
// Allowing users to insert columns in the worksheet
protection.setAllowInsertingColumn(true);
// Allowing users to insert hyperlinks in the worksheet
protection.setAllowInsertingHyperlink(true);
// Allowing users to insert rows in the worksheet
protection.setAllowInsertingRow(true);
// Allowing users to select locked cells of the worksheet
protection.setAllowSelectingLockedCell(true);
// Allowing users to select unlocked cells of the worksheet
protection.setAllowSelectingUnlockedCell(true);
// Allowing users to sort
protection.setAllowSorting(true);
// Allowing users to use pivot tables in the worksheet
protection.setAllowUsingPivotTable(true);
// Saving the modified Excel file Excel XP format
excel.save(dataDir + "APSettingsUsingAsposeCells_out.xls", FileFormatType.EXCEL_97_TO_2003);
// Print Message
System.out.println("Worksheet protected successfully.");

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AdvancedProtection.class) + "worksheets/";
// Instantiating a Workbook object by excel file path
Workbook excel = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = excel.getWorksheets();
Worksheet worksheet = worksheets.get(0);
Protection protection = worksheet.getProtection();
// Restricting users to delete columns of the worksheet
protection.setAllowDeletingColumn(false);
// Restricting users to delete row of the worksheet
protection.setAllowDeletingRow(false);
// Restricting users to edit contents of the worksheet
protection.setAllowEditingContent(false);
// Restricting users to edit objects of the worksheet
protection.setAllowEditingObject(false);
// Restricting users to edit scenarios of the worksheet
protection.setAllowEditingScenario(false);
// Restricting users to filter
protection.setAllowFiltering(false);
// Allowing users to format cells of the worksheet
protection.setAllowFormattingCell(true);
// Allowing users to format rows of the worksheet
protection.setAllowFormattingRow(true);
// Allowing users to insert columns in the worksheet
protection.setAllowInsertingColumn(true);
// Allowing users to insert hyperlinks in the worksheet
protection.setAllowInsertingHyperlink(true);
// Allowing users to insert rows in the worksheet
protection.setAllowInsertingRow(true);
// Allowing users to select locked cells of the worksheet
protection.setAllowSelectingLockedCell(true);
// Allowing users to select unlocked cells of the worksheet
protection.setAllowSelectingUnlockedCell(true);
// Allowing users to sort
protection.setAllowSorting(true);
// Allowing users to use pivot tables in the worksheet
protection.setAllowUsingPivotTable(true);
// Saving the modified Excel file Excel XP format
excel.save(dataDir + "AdvancedProtection_out.xls", FileFormatType.EXCEL_97_TO_2003);
// Print Message
System.out.println("Worksheet protected successfully.");

Cell 锁定问题

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

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

待办事项:图片_替代_文本

也可以使用 Aspose.Cells API 锁定单元格。每个单元格都有一个 Style API,它还包含一个 setLocked 方法。使用它锁定或解锁单元格。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(LockCell.class) + "worksheets/";
// Instantiating a Workbook object by excel file path
Workbook excel = new Workbook(dataDir + "Book1.xlsx");
WorksheetCollection worksheets = excel.getWorksheets();
Worksheet worksheet = worksheets.get(0);
worksheet.getCells().get("A1").getStyle().setLocked(true);
// Saving the modified Excel file Excel XP format
excel.save(dataDir + "LockCell_out.xls", FileFormatType.EXCEL_97_TO_2003);
// Print Message
System.out.println("Cell Locked successfully.");