Working with Excel spreadsheets is an important part of automating business processes, data analysis, and reporting. However, when exchanging data or storing sensitive information, it becomes necessary to protect sheets or the entire workbook. Security mechanisms allow you to prevent unauthorized modification of the content, maintain confidentiality and ensure access control.
If you develop on the platform .If you want to programmatically ensure the security of Excel documents, then one of the solutions is to use the SautinSoft Excel .Net library is a powerful tool for automating work with Excel files. In this article, we will explain in detail how to use it to protect a separate sheet, what are the advantages of this approach, why it is needed, and how popular this practice is in development.
Advantages and practical benefits:
Practical use cases:
Practice shows that sheet protection is one of the most demanded functions when working with automated reporting and document management systems. In a corporate environment, programmers regularly implement similar mechanisms to: protect templates and forms, prevent accidental errors in documents, ensure data secrecy, and automate the processes of generating protected files. It is a common practice to use such solutions in the banking, insurance, and public administration sectors, as well as in any area where information security and access control are important.
Regarding this code example, it's worth noting that to prevent other users from accidentally or intentionally changing, moving, or deleting data on a sheet, you can lock cells in an Excel sheet and then password-protect the sheet.
Suppose you own a team status report sheet and want team members to only add data to specific cells and not edit anything else.
Sheet protection allows you to make only specific parts of the sheet editable, preventing users from editing data in other areas of the sheet.
Complete code
using SautinSoft.Excel;
using System.IO;
namespace Example
{
class Program
{
static void Main(string[] args)
{
// Get your free key here:
// https://sautinsoft.com/start-for-free/
ProtectWorksheet();
}
/// <summary>
/// Protect worksheet in the file without passwords.
/// </summary>
/// <remarks>
/// Details:
/// </remarks>
static void ProtectWorksheet()
{
string inpFile = @"..\..\..\Example.xlsx";
string outFile = @"..\..\..\Result.xlsx";
ExcelDocument excelDocument = ExcelDocument.Load(inpFile);
// To prevent other users from accidentally or deliberately changing, moving, or deleting data in a worksheet, you can lock the cells on your Excel worksheet and then protect the sheet with a password.
// Say you own the team status report worksheet, where you want team members to add data in specific cells only and not be able to modify anything else.
// With worksheet protection, you can make only certain parts of the sheet editable and users will not be able to modify data in any other region in the sheet.
excelDocument.Worksheets[0].Protected = true;
// Using MS Excel just click on File-> Info-> Unprotect.
excelDocument.Save(outFile);
// Important for Linux: Install MS Fonts
// sudo apt install ttf-mscorefonts-installer -y
// Open the result for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
}
}
}Imports SautinSoft.Excel
Imports System.IO
Namespace Example
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Get your free key here:
' https://sautinsoft.com/start-for-free/
ProtectWorksheet()
End Sub
''' <summary>
''' Protect worksheet in the file without passwords.
''' </summary>
''' <remarks>
''' Details:
''' </remarks>
Private Shared Sub ProtectWorksheet()
Dim inpFile As String = "..\..\..\Example.xlsx"
Dim outFile As String = "..\..\..\Result.xlsx"
Dim excelDocument As ExcelDocument = ExcelDocument.Load(inpFile)
' To prevent other users from accidentally or deliberately changing, moving, or deleting data in a worksheet, you can lock the cells on your Excel worksheet and then protect the sheet with a password.
' Say you own the team status report worksheet, where you want team members to add data in specific cells only and not be able to modify anything else.
' With worksheet protection, you can make only certain parts of the sheet editable and users will not be able to modify data in any other region in the sheet.
excelDocument.Worksheets(0).Protected = True
' Using MS Excel just click on File-> Info-> Unprotect.
excelDocument.Save(outFile)
' Important for Linux: Install MS Fonts
' sudo apt install ttf-mscorefonts-installer -y
' Open the result for demonstration purposes.
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
End Sub
End Class
End Namespace
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: