using IronXL;WorkBook workBook = WorkBook.Load("sample.xlsx");WorkSheet workSheet = workBook.DefaultWorkSheet;// Set protection for selected worksheetworkSheet.ProtectSheet("IronXL");workBook.Save();
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Set protection for selected worksheet
workSheet.ProtectSheet("IronXL");
workBook.Save();
ImportsIronXLPrivate workBook AsWorkBook = WorkBook.Load("sample.xlsx")Private workSheet AsWorkSheet = workBook.DefaultWorkSheet' Set protection for selected worksheetworkSheet.ProtectSheet("IronXL")workBook.Save()
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set protection for selected worksheet
workSheet.ProtectSheet("IronXL")
workBook.Save()
保护多个工作表
当处理包含多个工作表的复杂工作簿时,您可能需要应用不同的保护策略:
using IronXL;// Load the workbookWorkBook workBook = WorkBook.Load("financial-report.xlsx");// Protect each worksheet with a different passwordworkBook.GetWorkSheet("Summary").ProtectSheet("SummaryPass123");workBook.GetWorkSheet("Details").ProtectSheet("DetailsSecure456");workBook.GetWorkSheet("Charts").ProtectSheet("ChartsProtect789");// Save the workbook with all protections appliedworkBook.SaveAs("protected-financial-report.xlsx");
using IronXL;
// Load the workbook
WorkBook workBook = WorkBook.Load("financial-report.xlsx");
// Protect each worksheet with a different password
workBook.GetWorkSheet("Summary").ProtectSheet("SummaryPass123");
workBook.GetWorkSheet("Details").ProtectSheet("DetailsSecure456");
workBook.GetWorkSheet("Charts").ProtectSheet("ChartsProtect789");
// Save the workbook with all protections applied
workBook.SaveAs("protected-financial-report.xlsx");
ImportsIronXL' Load the workbookDim workBook AsWorkBook = WorkBook.Load("financial-report.xlsx")' Protect each worksheet with a different passwordworkBook.GetWorkSheet("Summary").ProtectSheet("SummaryPass123")workBook.GetWorkSheet("Details").ProtectSheet("DetailsSecure456")workBook.GetWorkSheet("Charts").ProtectSheet("ChartsProtect789")' Save the workbook with all protections appliedworkBook.SaveAs("protected-financial-report.xlsx")
Imports IronXL
' Load the workbook
Dim workBook As WorkBook = WorkBook.Load("financial-report.xlsx")
' Protect each worksheet with a different password
workBook.GetWorkSheet("Summary").ProtectSheet("SummaryPass123")
workBook.GetWorkSheet("Details").ProtectSheet("DetailsSecure456")
workBook.GetWorkSheet("Charts").ProtectSheet("ChartsProtect789")
' Save the workbook with all protections applied
workBook.SaveAs("protected-financial-report.xlsx")
using IronXL;// Load a workbook and protect specific worksheets based on contentWorkBook workBook = WorkBook.Load("employee-data.xlsx");foreach (WorkSheet sheet in workBook.WorkSheets){ // Check if the sheet name contains sensitive keywords if (sheet.Name.Contains("Salary") || sheet.Name.Contains("Personal")) { // Apply stronger password protection to sensitive sheets sheet.ProtectSheet($"Secure_{sheet.Name}_2024!"); } else { // Apply standard protection to other sheets sheet.ProtectSheet("StandardProtection"); }}// Save the selectively protected workbookworkBook.SaveAs("employee-data-protected.xlsx");
using IronXL;
// Load a workbook and protect specific worksheets based on content
WorkBook workBook = WorkBook.Load("employee-data.xlsx");
foreach (WorkSheet sheet in workBook.WorkSheets)
{
// Check if the sheet name contains sensitive keywords
if (sheet.Name.Contains("Salary") || sheet.Name.Contains("Personal"))
{
// Apply stronger password protection to sensitive sheets
sheet.ProtectSheet($"Secure_{sheet.Name}_2024!");
}
else
{
// Apply standard protection to other sheets
sheet.ProtectSheet("StandardProtection");
}
}
// Save the selectively protected workbook
workBook.SaveAs("employee-data-protected.xlsx");
ImportsIronXL' Load a workbook and protect specific worksheets based on contentDim workBook AsWorkBook = WorkBook.Load("employee-data.xlsx")For Each sheet AsWorkSheetIn workBook.WorkSheets ' Check if the sheet name contains sensitive keywords If sheet.Name.Contains("Salary") OrElse sheet.Name.Contains("Personal") Then ' Apply stronger password protection to sensitive sheets sheet.ProtectSheet($"Secure_{sheet.Name}_2024!") Else ' Apply standard protection to other sheets sheet.ProtectSheet("StandardProtection") End IfNext' Save the selectively protected workbookworkBook.SaveAs("employee-data-protected.xlsx")
Imports IronXL
' Load a workbook and protect specific worksheets based on content
Dim workBook As WorkBook = WorkBook.Load("employee-data.xlsx")
For Each sheet As WorkSheet In workBook.WorkSheets
' Check if the sheet name contains sensitive keywords
If sheet.Name.Contains("Salary") OrElse sheet.Name.Contains("Personal") Then
' Apply stronger password protection to sensitive sheets
sheet.ProtectSheet($"Secure_{sheet.Name}_2024!")
Else
' Apply standard protection to other sheets
sheet.ProtectSheet("StandardProtection")
End If
Next
' Save the selectively protected workbook
workBook.SaveAs("employee-data-protected.xlsx")
// Remove protection for selected worksheet. It works without password!workSheet.UnprotectSheet();
// Remove protection for selected worksheet. It works without password!
workSheet.UnprotectSheet();
' Remove protection for selected worksheet. It works without password!workSheet.UnprotectSheet()
' Remove protection for selected worksheet. It works without password!
workSheet.UnprotectSheet()
批量解除工作表保护
在处理多个受保护的工作表时,您可能需要同时取消所有工作表的保护。 以下是一种有效的方法:
using IronXL;using System;// Load the protected workbookWorkBook workBook = WorkBook.Load("multi-protected.xlsx");// Counter for tracking operationsint unprotectedCount = 0;// Iterate through all worksheets and remove protectionforeach (WorkSheet sheet in workBook.WorkSheets){ try { sheet.UnprotectSheet(); unprotectedCount++;Console.WriteLine($"Unprotected: {sheet.Name}"); } catch (Exception ex) {Console.WriteLine($"Failed to unprotect {sheet.Name}: {ex.Message}"); }}Console.WriteLine($"Successfully unprotected {unprotectedCount} worksheets");// Save the unprotected workbookworkBook.SaveAs("multi-unprotected.xlsx");
using IronXL;
using System;
// Load the protected workbook
WorkBook workBook = WorkBook.Load("multi-protected.xlsx");
// Counter for tracking operations
int unprotectedCount = 0;
// Iterate through all worksheets and remove protection
foreach (WorkSheet sheet in workBook.WorkSheets)
{
try
{
sheet.UnprotectSheet();
unprotectedCount++;
Console.WriteLine($"Unprotected: {sheet.Name}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to unprotect {sheet.Name}: {ex.Message}");
}
}
Console.WriteLine($"Successfully unprotected {unprotectedCount} worksheets");
// Save the unprotected workbook
workBook.SaveAs("multi-unprotected.xlsx");
ImportsIronXLImportsSystem' Load the protected workbookDim workBook AsWorkBook = WorkBook.Load("multi-protected.xlsx")' Counter for tracking operationsDim unprotectedCount AsInteger = 0' Iterate through all worksheets and remove protectionFor Each sheet AsWorkSheetIn workBook.WorkSheetsTry sheet.UnprotectSheet() unprotectedCount += 1Console.WriteLine($"Unprotected: {sheet.Name}")Catch ex AsExceptionConsole.WriteLine($"Failed to unprotect {sheet.Name}: {ex.Message}")EndTryNextConsole.WriteLine($"Successfully unprotected {unprotectedCount} worksheets")' Save the unprotected workbookworkBook.SaveAs("multi-unprotected.xlsx")
Imports IronXL
Imports System
' Load the protected workbook
Dim workBook As WorkBook = WorkBook.Load("multi-protected.xlsx")
' Counter for tracking operations
Dim unprotectedCount As Integer = 0
' Iterate through all worksheets and remove protection
For Each sheet As WorkSheet In workBook.WorkSheets
Try
sheet.UnprotectSheet()
unprotectedCount += 1
Console.WriteLine($"Unprotected: {sheet.Name}")
Catch ex As Exception
Console.WriteLine($"Failed to unprotect {sheet.Name}: {ex.Message}")
End Try
Next
Console.WriteLine($"Successfully unprotected {unprotectedCount} worksheets")
' Save the unprotected workbook
workBook.SaveAs("multi-unprotected.xlsx")
Can I integrate worksheet protection in complex Excel workflows?
Yes, IronXL's worksheet protection feature can be integrated into complex workflows, enabling you to create new spreadsheets with pre-configured protection settings, manage sensitive data, and automate data processing.
What happens if a user tries to modify a protected worksheet without the password?
If a user tries to modify a protected worksheet, Excel will prompt them to enter the correct password. Without it, they can only view the content but cannot make changes.
Does worksheet protection affect performance in IronXL?
While protection does not significantly impact performance, working with many protected worksheets in large workbooks may require optimization strategies to maintain efficiency.
How can I ensure compatibility of protected worksheets across different Excel versions?
Testing protected worksheets with various Excel versions before deployment ensures compatibility and helps identify any potential issues with different spreadsheet applications that support the Excel format.