保护工作表
保护工作表
介绍
Microsoft Excel中的常规保护选项包括:
- 内容
- 对象
- 方案
受保护的工作表不会隐藏或保护敏感数据,因此它与文件加密不同。通常,工作表保护适用于展示目的。它防止最终用户修改工作表中的数据、内容和格式。
保护工作表
Aspose.Cells提供了Workbook类,表示Microsoft Excel文件。Workbook类包含Worksheets集合,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。
Worksheet类提供Protect方法,用于在工作表上应用保护。Protect方法接受以下参数:
- 保护类型,工作表上应用的保护类型。 保护类型是用 ProtectionType 枚举帮助应用的。
- 新密码,用于保护工作表的新密码。
- 旧密码,如果工作表已经受到密码保护,则传入旧密码。如果工作表尚未受到保护,则传递 null。
ProtectionType枚举包含以下预定义的保护类型:
保护类型 | 描述 |
---|---|
All | 用户无法修改工作表中的任何内容 |
Contents | 用户无法在工作表中输入数据 |
Objects | 用户无法修改绘图对象 |
Scenarios | 用户无法修改已保存的情景 |
Structure | 用户无法修改结构 |
Windows | 保护应用于窗口 |
None | 不应用任何保护 |
下例显示如何使用密码保护工作表。
// 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保护工作表的部分单元格
可能会有某些场景需要仅锁定工作表中的一些单元格。如果要锁定工作表中的特定单元格,必须解锁工作表中的所有其他单元格。工作表中的所有单元格已初始化为锁定,您可以在 Microsoft Excel 中打开任何 Excel 文件并单击 格式 | 单元格 来显示 单元格格式 对话框,然后单击 保护 选项卡,查看一个复选框标记为“已锁定”默认为选中状态。
以下描述如何使用 MS Excel 锁定一些单元格。此方法适用于 Microsoft Office Excel 97、2000、2002、2003 及更高版本。
- 点击全选按钮(位于行号1上方和列号A左侧的灰色矩形)来选择整个工作表。
- 在格式菜单上点击单元格,点击保护选项卡,然后取消锁定复选框。 这将解锁工作表上的所有单元格。 如果单元格命令不可用,工作表的部分可能已被锁定。在工具菜单上,指向保护,然后点击取消保护工作表。
- 仅选择您想要锁定的单元格,然后重复第2步,但这次选择锁定复选框。
- 在工具菜单上,指向保护,点击保护工作表,然后点击确定。
- 在保护工作表对话框中,您可以选择指定密码并选择要允许用户更改的元素。
使用Aspose Cells在工作表中保护一些单元格
在这种方法中,我们只使用Aspose.Cells API来执行此任务。
示例:以下示例展示了如何在工作表中保护一些单元格。首先解锁工作表中的所有单元格,然后锁定其中的3个单元格(A1、B1、C1)。最后,保护工作表。Style 对象包含一个布尔属性,IsLocked。您可以将 IsLocked属性设置为true或false,并应用*Column/Row.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类的ApplyStyle()方法来对工作表中的特定行应用Style。该方法接受两个参数:Style对象和包含所有与应用格式相关的成员的StyleFlag对象。
下面的示例展示了如何在工作表中保护一行。首先解锁工作表中的所有单元格,然后锁定其中的第一行。最后,保护工作表。Style对象包含一个布尔属性,IsLocked。您可以将IsLocked属性设置为true或false,以使用StyleFlag对象锁定或解锁行/列。
// 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类的ApplyStyle()方法来对工作表中的特定列应用Style。该方法接受两个参数:Style对象和包含所有与应用格式相关的成员的StyleFlag对象。
下面的示例展示了如何在工作表中保护一列。首先解锁工作表中的所有单元格,然后锁定其中的第一列。最后,保护工作表。Style对象包含一个布尔属性,IsLocked。您可以将IsLocked属性设置为true或false,以使用StyleFlag对象锁定或解锁行/列。
// 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"); |