Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In the video tutorial, viewers are guided through the process of setting a password for an Excel workbook using IronXL in C#. The tutorial begins with ensuring that IronXL is installed and then proceeds to load an existing Excel file, 'sample.xlsx', into the workbook. It demonstrates how to handle both non-protected and protected files by passing a password as a second argument when necessary. The main focus is on applying a security measure to the spreadsheet by utilizing the encrypt
method to lock the workbook with a password. In this case, the password 'iron software' is used. After applying the password, the file is saved, and the project is executed, requiring the password to unlock the workbook. The successful application of password protection is confirmed by entering the password and accessing the file contents. The tutorial concludes by encouraging viewers to explore more functionalities of the IronXL library and inviting them to subscribe for additional tips and tricks. A link is provided in the description for downloading and installing IronXL to experience its capabilities firsthand.
Below is an example of how you can use IronXL in C# to password-protect an Excel workbook:
// Make sure to add IronXL to your project by installing the IronXL NuGet package
using IronXL;
class Program
{
static void Main()
{
// Load an existing Excel file
WorkBook workbook = WorkBook.Load("sample.xlsx");
// Set a password for the workbook using the Encrypt method
// The password used here is 'iron software'
workbook.Encrypt("iron software");
// Save the workbook as a new file, now password protected
workbook.SaveAs("protected_sample.xlsx");
// Inform the user that the workbook is now password protected
Console.WriteLine("The workbook has been protected with a password and saved as 'protected_sample.xlsx'.");
}
}
// Make sure to add IronXL to your project by installing the IronXL NuGet package
using IronXL;
class Program
{
static void Main()
{
// Load an existing Excel file
WorkBook workbook = WorkBook.Load("sample.xlsx");
// Set a password for the workbook using the Encrypt method
// The password used here is 'iron software'
workbook.Encrypt("iron software");
// Save the workbook as a new file, now password protected
workbook.SaveAs("protected_sample.xlsx");
// Inform the user that the workbook is now password protected
Console.WriteLine("The workbook has been protected with a password and saved as 'protected_sample.xlsx'.");
}
}
' Make sure to add IronXL to your project by installing the IronXL NuGet package
Imports IronXL
Friend Class Program
Shared Sub Main()
' Load an existing Excel file
Dim workbook As WorkBook = WorkBook.Load("sample.xlsx")
' Set a password for the workbook using the Encrypt method
' The password used here is 'iron software'
workbook.Encrypt("iron software")
' Save the workbook as a new file, now password protected
workbook.SaveAs("protected_sample.xlsx")
' Inform the user that the workbook is now password protected
Console.WriteLine("The workbook has been protected with a password and saved as 'protected_sample.xlsx'.")
End Sub
End Class
Install IronXL: You need to ensure that IronXL is installed in your project. This can be done via the NuGet Package Manager by searching for and installing the IronXL
package.
Loading the Workbook: The WorkBook.Load
method is used to load an existing Excel file (sample.xlsx
) into memory.
Encrypting the Workbook: The Encrypt
method is called on the WorkBook
object to set a password (iron software
). This method encodes the Excel file such that it requires a password to be opened again.
Saving the Protected Workbook: The SaveAs
method saves the current state of the workbook to a new file, protected_sample.xlsx
, which is now password protected.
Further Reading: How to Set a Password to Workbook