保护工作表

保护工作表

介绍

Microsoft Excel 中的常规保护选项是:

  • 内容
  • 对象
  • 场景

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

保护工作表

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

工作表类提供了保护用于在工作表上应用保护的方法。保护方法接受以下参数:

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

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

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

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

// 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];
// Protecting the worksheet with a password
worksheet.Protect(ProtectionType.All, "aspose", null);
// Saving the modified Excel file in default format
excel.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);
// Closing the file stream to free all resources
fstream.Close();

上面的代码对工作表进行了保护后,打开工作表就可以查看到保护情况了。打开文件并尝试向工作表中添加一些数据后,您将看到以下对话框:

警告用户无法修改工作表的对话框
待办事项:图片_替代_文本

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

选择取消保护工作表菜单项后,将打开一个对话框,提示您输入密码,以便您可以在工作表上工作,如下所示:

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

使用Microsoft Excel保护工作表中的几个Cells

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

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

  1. 通过单击选择整个工作表全选按钮(第 1 行行号正上方和列字母 A 左侧的灰色矩形)。
  2. 点击Cells格式菜单,单击保护选项卡,然后清除锁定复选框。 这将解锁工作表上的所有单元格 如果Cells命令不可用,部分工作表可能已被锁定。在工具菜单,指向保护 然后点击取消保护工作表.
  3. 只选择要锁定的单元格并重复步骤 2,但这次选择锁定复选框。
  4. 工具菜单,指向保护 , 点击保护表然后点击好的.
  5. 在里面保护表对话框中,您可以选择指定密码并选择您希望用户能够更改的元素。

保护Worksheet中的几个Cells Using Aspose Cells

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

示例:以下示例展示了如何保护工作表中的几个单元格。它首先解锁工作表中的所有单元格,然后锁定其中的 3 个单元格(A1、B1、C1)。最后,它保护工作表。这风格对象包含一个布尔属性,被锁住了 .你可以设置被锁住了属性为真或假并应用*列/行.ApplyStyle()*使用所需属性锁定或解锁行/列的方法。

// 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);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.Worksheets[0];
// Define the style object.
Style style;
// Define the styleflag object
StyleFlag styleflag;
// Loop through all the columns in the worksheet and unlock them.
for (int i = 0; i <= 255; i++)
{
style = sheet.Cells.Columns[(byte)i].GetStyle();
style.IsLocked = false;
styleflag = new StyleFlag();
styleflag.Locked = true;
sheet.Cells.Columns[(byte)i].ApplyStyle(style, styleflag);
}
// Lock the three cells...i.e. A1, B1, C1.
style = sheet.Cells["A1"].GetStyle();
style.IsLocked = true;
sheet.Cells["A1"].SetStyle(style);
style = sheet.Cells["B1"].GetStyle();
style.IsLocked = true;
sheet.Cells["B1"].SetStyle(style);
style = sheet.Cells["C1"].GetStyle();
style.IsLocked = true;
sheet.Cells["C1"].SetStyle(style);
// Finally, Protect the sheet now.
sheet.Protect(ProtectionType.All);
// Save the excel file.
wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);

保护工作表中的一行

Aspose.Cells 允许您轻松锁定工作表中的任何行。在这里,我们可以利用应用样式()的方法Aspose.Cells.Row类申请风格到工作表中的特定行。这个方法有两个参数:一个风格对象和风格旗帜具有与应用格式相关的所有成员的对象。

下面的示例演示如何保护工作表中的一行。它首先解锁工作表中的所有单元格,然后锁定其中的第一行。最后,它保护工作表。这风格对象包含一个布尔属性,被锁住了 .你可以设置被锁住了属性设置为 true 或 false 以使用风格旗帜目的。

// 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);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.Worksheets[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.Cells.Columns[(byte)i].GetStyle();
style.IsLocked = false;
flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.Columns[(byte)i].ApplyStyle(style, flag);
}
// Get the first row style.
style = sheet.Cells.Rows[0].GetStyle();
// Lock it.
style.IsLocked = true;
// Instantiate the flag.
flag = new StyleFlag();
// Set the lock setting.
flag.Locked = true;
// Apply the style to the first row.
sheet.Cells.ApplyRowStyle(0, style, flag);
// Protect the sheet.
sheet.Protect(ProtectionType.All);
// Save the excel file.
wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);

保护工作表中的列

Aspose.Cells 允许您轻松锁定工作表中的任何列。在这里,我们可以利用应用样式()的方法Aspose.Cells.Column类申请风格到工作表中的特定列。这个方法有两个参数:一个风格对象和风格旗帜具有与应用格式相关的所有成员的对象。

以下示例显示如何保护工作表中的列。它首先解锁工作表中的所有单元格,然后锁定其中的第一列。最后,它保护工作表。这风格对象包含一个布尔属性,被锁住了 .你可以设置被锁住了属性设置为 true 或 false 以使用风格旗帜目的。

// 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);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.Worksheets[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.Cells.Columns[(byte)i].GetStyle();
style.IsLocked = false;
flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.Columns[(byte)i].ApplyStyle(style, flag);
}
// Get the first column style.
style = sheet.Cells.Columns[0].GetStyle();
// Lock it.
style.IsLocked = true;
// Instantiate the flag.
flag = new StyleFlag();
// Set the lock setting.
flag.Locked = true;
// Apply the style to the first column.
sheet.Cells.Columns[0].ApplyStyle(style, flag);
// Protect the sheet.
sheet.Protect(ProtectionType.All);
// Save the excel file.
wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);

允许用户编辑范围

以下示例显示如何允许用户编辑受保护工作表中的范围。

// 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);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiate a new Workbook
Workbook book = new Workbook();
// Get the first (default) worksheet
Worksheet sheet = book.Worksheets[0];
// Get the Allow Edit Ranges
ProtectedRangeCollection allowRanges = sheet.AllowEditRanges;
// Define ProtectedRange
ProtectedRange proteced_range;
// Create the range
int idx = allowRanges.Add("r2", 1, 1, 3, 3);
proteced_range = allowRanges[idx];
// Specify the passoword
proteced_range.Password = "123";
// Protect the sheet
sheet.Protect(ProtectionType.All);
// Save the Excel file
book.Save(dataDir + "protectedrange.out.xls");