Using Spire.XLS, the password of an encrypted workbook can be removed or modified in case you know the open password. This article presents how to load a password protected Excel workbook, remove the protection or reset the password and then save the changes to the original file.
Step 1: Initialize an instance of Workbook class.
Workbook wb = new Workbook();
Step 2: Specify the open password and then load the encrypted Excel file.
wb.OpenPassword = "oldpassword"; wb.LoadFromFile(@"C:\Users\Administrator\Desktop\Encrypted.xlsx");
Step 3: Remove the password protection with UnProtect() method or reset the password by Protect() method.
//unprotect workbook
wb.UnProtect();
//reset password
wb.Protect("newpassword");
Step 4: Save the changes to file.
wb.SaveToFile(@"C:\Users\Administrator\Desktop\Encrypted.xlsx, ExcelVersion.Version2010");
Full Code:
[C#]
using Spire.Xls;
namespace RemovePassword
{
class Program
{
static void Main(string[] args)
{
Workbook wb = new Workbook();
wb.OpenPassword = "oldpassword";
wb.LoadFromFile(@"C:\Users\Administrator\Desktop\Encrypted.xlsx");
////unprotect workbook
//wb.UnProtect();
//reset password
wb.Protect("newpassword");
wb.SaveToFile(@"C:\Users\Administrator\Desktop\Encrypted.xlsx", ExcelVersion.Version2010);
}
}
}
[VB.NET]
Imports Spire.Xls
Namespace RemovePassword
Class Program
Private Shared Sub Main(args As String())
Dim wb As New Workbook()
wb.OpenPassword = "oldpassword"
wb.LoadFromFile("C:\Users\Administrator\Desktop\Encrypted.xlsx")
'''/unprotect workbook
'wb.UnProtect();
'reset password
wb.Protect("newpassword")
wb.SaveToFile("C:\Users\Administrator\Desktop\Encrypted.xlsx", ExcelVersion.Version2010)
End Sub
End Class
End Namespace
