在VSTO和Aspose.Cells中保护和取消保护工作簿
Contents
[
Hide
]
要打开现有的 Microsoft Excel 文件,请保护工作簿的结构和 Windows 属性,并保存文件。
下面是VSTO(C#)和Aspose.Cells for .NET(C#)的并行代码片段,演示了如何保护工作簿。
VSTO
保护工作簿
//Instantiate the Application object.
Excel.Application excelApp = Application;
//Excel.Application excelApp = Application;
//Specify the template excel file path.
string myPath = "MyBook.xls";
//Open the excel file.
excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
//Protect the workbook specifying a password with Structure and Windows attributes.
excelApp.ActiveWorkbook.Protect("007", true, true);
//Save the file.
excelApp.ActiveWorkbook.Save();
//Quit the Application.
excelApp.Quit();
取消保护工作簿
//Unprotect the workbook specifying its password.
excelApp.ActiveWorkbook.Unprotect("007");
Aspose.Cells
保护工作簿
//Specify the template excel file path.
string myPath = "Book1.xls";
//Instantiate a new Workbook.
//Open the excel file.
Workbook workbook = new Workbook(myPath);
//Protect the workbook specifying a password with Structure and Windows attributes.
workbook.Protect(ProtectionType.All, "007");
//Save As the excel file.
workbook.Save("MyBook.xls");
取消保护工作簿
//Unprotect the workbook specifying its password.
workbook.Unprotect("007");