常用的对excel 格式文件保护的方法有保护整个工作薄或保护指定的工作表,以及标记最终状态,设置用户可以编辑区域等。本文将介绍使用spire.xls实现不同的保护类和移除密码保护。
保护工作薄
我们常用方法workbook.protect() 来使用密码保护工作薄,读者需要输入密码才能打开工作薄。同时,也可以保护工作薄结构,防止对工作薄进行不需要的更改,例如添加工作表或删除工作表。
c#
//初始化一个workbook实例并加载文档
workbook workbook = new workbook();
workbook.loadfromfile("test.xlsx");
////使用密码保护工作薄
//workbook.protect("123");
//密码保护工作薄,并保护工作薄结构
workbook.protect("123",true,true);
workbook.savetofile("protectexcel.xlsx", excelversion.version2013);
vb.net
'初始化一个workbook实例并加载文档
dim workbook as new workbook()
workbook.loadfromfile("test.xlsx")
'使用密码保护工作薄
'workbook.protect("123");
'密码保护工作薄,并保护工作薄结构
workbook.protect("123", true, true)
workbook.savetofile("protectexcel.xlsx", excelversion.version2013)
保护工作表
我们可以使用spire.xls只对某个特定的工作表进行保护,读者可以打开并查看工作表,但是不能对这个工作表内容进行编辑,操作。下面表格详细描述了spire.xls提供的18类工作表保护类型。
none | represents none flags. | ||
object | protects shapes. | ||
scenarios | protects scenarios. | ||
formattingcells | allows users to format any cells on a protected worksheet. | ||
formattingcolumns | allows users to format any columns on a protected worksheet. | ||
formattingrows | allows users to format any rows on a protected worksheet. | ||
insertingcolumns | allows users to insert columns on a protected worksheet. | ||
insertingrows | allows users to insert rows on a protected worksheet. | ||
insertinghyperlinks | allows users to insert hyperlinks on a protected worksheet. | ||
deletingcolumns | allows users to delete columns on a protected worksheet. | ||
deletingrows | allows users to delete rows on a protected worksheet. | ||
lockedcells | protects locked cells. | ||
sorting | allows users to sort on a protected worksheet. | ||
filtering | allows users to set filters on a protected worksheet. | ||
usingpivottable | allows users to use pivot table reports on a protected worksheet. | ||
unlockedcells | protects users interface, but not macros. | ||
contents | represents all flags. | ||
all | represents default protection. |
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile("test.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//保护第一个工作表并设置保护类型
sheet.protect("123", sheetprotectiontype.all);
workbook.savetofile("protectsheet1.xlsx", excelversion.version2013);
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("test.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'保护第一个工作表并设置保护类型
sheet.protect("123", sheetprotectiontype.all)
workbook.savetofile("protectsheet1.xlsx", excelversion.version2013)
取消密码保护工作薄
//初始化一个workbook实例
workbook workbook = new workbook();
//输入密码并加载文档
workbook.openpassword = ("123");
workbook.loadfromfile("sample.xlsx", excelversion.version2013);
//取消保护
workbook.unprotect();
//保存文档
workbook.savetofile("output.xlsx", excelversion.version2013);
vb.net
'初始化一个workbook实例
dim workbook as new workbook()
'输入密码并加载文档
workbook.openpassword = ("123")
workbook.loadfromfile("sample.xlsx", excelversion.version2013)
'取消保护
workbook.unprotect()
'保存文档
workbook.savetofile("output.xlsx", excelversion.version2013)
取消密码保护工作表
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//输入密码取消保护工作表
sheet.unprotect("123");
//保存文档
workbook.savetofile("result.xlsx", excelversion.version2013);
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("sample.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'输入密码取消保护工作表
sheet.unprotect("123")
'保存文档
workbook.savetofile("result.xlsx", excelversion.version2013)