Excel管理控件Aspose.Cells开发者指南(三十八):保护工作表

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

Aspose.Cells for .NET是Excel电子表格编程API,可加快电子表格管理和处理任务,支持构建具有生成,修改,转换,呈现和打印电子表格功能的跨平台应用程序。

在接下来的系列教程中,将为开发者带来Aspose.Cells for .NET的一系列使用教程,例如关于加载保存转换、字体、渲染、绘图、智能标记等等。本文重点介绍如何设置打印选项。

>>Aspose.Cells for .NET已经更新至v20.9,支持获取/设置切片器形状属性,新增客户端api用于添加/删除GridWeb的注释,冻结和拆分工作表的功能增强体验

好消息来啦!整合所有格式API处理控件Aspose永久授权正在网火热销售中,新购乐享85折起!联系客服立马1分钟了解全部咨询!

第九章:关于保护功能

▲第一节:保护工作表

Aspose.Cells提供了一个表示Microsoft Excel文件的类–Workbook,该类包含一个工作表集合,允许访问Excel文件中的每个工作表。Workbook类包含一个Worksheets集合,允许访问Excel文件中的每个工作表。一个工作表由Worksheet类来表示。

Worksheet类提供了Protect方法,用于对工作表进行保护。Protect方法接受以下参数。

  • 保护类型,适用于工作表的保护类型。保护类型是在ProtectionType 枚举的帮助下应用的。
  • 新密码,用于保护工作表的新密码。
  • 旧密码,旧密码(如果工作表已受密码保护)。如果工作表尚未受到保护,则只需传递null。

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

保护类型 描述
All 用户无法修改此工作表上的任何内容
内容 用户无法在此工作表中输入数据
对象 用户无法修改图形对象
情境 用户无法修改已保存的方案
结构体 用户无法修改结构
视窗 保护已应用于Windows
None 未应用保护

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

// 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 openedFileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);// Instantiating a Workbook object// Opening the Excel file through the file streamWorkbook excel = new Workbook(fstream);// Accessing the first worksheet in the Excel fileWorksheet worksheet = excel.Worksheets[0];// Protecting the worksheet with a passwordworksheet.Protect(ProtectionType.All, "aspose", null);// Saving the modified Excel file in default formatexcel.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);// Closing the file stream to free all resourcesfstream.Close();

在上面的代码用于保护工作表之后,您可以通过打开工作表来检查保护。打开文件并尝试向工作表中添加一些数据后,您将看到以下对话框:

Excel管理控件Aspose.Cells开发者指南(三十八):保护工作表

要在工作表上工作,请选择“ 保护”,然后从“ 工具”菜单项中选择“取消保护工作表”来取消保护工作表。选择“取消保护工作表”菜单项后,将打开一个对话框,提示您输入密码,以便您可以在工作表上进行操作,如下所示:

Excel管理控件Aspose.Cells开发者指南(三十八):保护工作表

使用Aspose单元格保护工作表中的一些单元格

在此方法中,我们仅使用Aspose.Cells API来完成任务。

示例:以下示例展示了如何保护工作表中的一些单元格。它首先解锁工作表中的所有单元格,然后锁定其中的3个单元格(A1,B1,C1)。最后,它保护了工作表。该样式对象包含了一个布尔属性,IsLocked。您可以将IsLocked 属性设置为true或false,然后应用Column / Row.ApplyStyle()方法来锁定或解锁具有所需属性的行/列。

// 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 objectStyleFlag 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].Style; 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); 

还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群,我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年8月12日
下一篇 2020年8月12日

相关推荐

发表回复

登录后才能评论