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.
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.
/// </summary>
/// <remarks>
/// Details:
/// </remarks>
static void ProtectWorksheet()
{
string inpFile = @"..\..\..\Example.xlsx";
string outFile = @"..\..\..\Result.xlsx";
ExcelDocument excelDocument = ExcelDocument.Load(inpFile);
excelDocument.Worksheets[0].Protected = true;
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.
''' </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)
excelDocument.Worksheets(0).Protected = True
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: