Protect Specific Row In Excel Worksheet

Protecting confidential data in an Excel spreadsheet is essential to ensure information security. Aspose.Cells for .NET offers a powerful solution to protect specific rows in an Excel spreadsheet. This guide will walk you through how to protect a specific row in an Excel worksheet using the provided C# source code. Follow these simple steps to set up row protection in your Excel files.

Step 1: Import required libraries

To get started, make sure you have Aspose.Cells for .NET installed on your system. You also need to add the appropriate references in your C# project to be able to use the functionality of Aspose.Cells. Here is the code to import the required libraries:

// Add the necessary references
using Aspose.Cells;

Step 2: Creating an Excel workbook and spreadsheet

After importing the required libraries, you can create a new Excel workbook and a new worksheet. Here’s how to do it:

// The path to the documents directory.
string dataDir = "YOUR DOCUMENTS DIRECTORY";

// Create a directory if it doesn't already exist.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
     System.IO.Directory.CreateDirectory(dataDir);

// Create a new workbook.
Workbook wb = new Workbook();

// Create a spreadsheet object and get the first sheet.
Worksheet sheet = wb.Worksheets[0];

Step 3: Setting the Style and Style Flag

Now we will set the cell style and style flag to unlock all columns in the worksheet. Here is the necessary code:

// Set the style object.
Styling styling;

// Set the styleflag object.
StyleFlag flag;

// Loop through all columns in the worksheet and unlock them.
for (int i = 0; i <= 255; i++)
{
     style = sheet.Cells.Columns[(byte)i].Style;
     style. IsLocked = false;
     flag = new StyleFlag();
     flag. Locked = true;
     sheet.Cells.Columns[(byte)i].ApplyStyle(style, flag);
}

Step 4: Protect the specific line

Now we will protect the specific row in the worksheet. We are going to lock the first row to prevent any modification. Here’s how:

// Get the style of the first line.
style = sheet.Cells.Rows[0].Style;

// Lock it.
style. IsLocked = true;

// Instantiate the flag.
flag = new StyleFlag();

// Set the lock parameter.
flag. Locked = true;

// Apply the style to the first line.
sheet.Cells.ApplyRowStyle(0, style, flag);

Step 5: Protecting the worksheet

Finally, we will protect the entire Excel worksheet to prevent unauthorized modification. Here’s how:

// Protect the worksheet.
sheet.Protect(ProtectionType.All);

Step 6: Save the protected Excel file

Once you are done protecting the specific row in the Excel worksheet, you can save the protected Excel file to your system. Here’s how:

// Save the Excel file.
wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);

After following these steps, you will have successfully protected a specific row in your Excel spreadsheet using Aspose.Cells for .NET.

Sample source code for Protect Specific Row In Excel Worksheet using Aspose.Cells for .NET

// The path to the documents directory.
string dataDir = "YOUR DOCUMENT DIRECTORY";
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
    System.IO.Directory.CreateDirectory(dataDir);
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.Worksheets[0];
// Define the style object.
Style style;
// Define the styleflag object.
StyleFlag flag;
// Loop through all the columns in the worksheet and unlock them.
for (int i = 0; i <= 255; i++)
{
    style = sheet.Cells.Columns[(byte)i].Style;
    style.IsLocked = false;
    flag = new StyleFlag();
    flag.Locked = true;
    sheet.Cells.Columns[(byte)i].ApplyStyle(style, flag);
}
// Get the first row style.
style = sheet.Cells.Rows[0].Style;
// Lock it.
style.IsLocked = true;
// Instantiate the flag.
flag = new StyleFlag();
// Set the lock setting.
flag.Locked = true;
// Apply the style to the first row.
sheet.Cells.ApplyRowStyle(0, style, flag);
// Protect the sheet.
sheet.Protect(ProtectionType.All);
// Save the excel file.
wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);

Conclusion

Protecting data in Excel files is crucial to prevent unauthorized access or unwanted modification. Using the Aspose.Cells library for .NET, you can easily protect specific rows in an Excel spreadsheet using the provided C# source code. Follow this step-by-step guide to add an extra layer of security to your Excel files.

FAQs

Does specific row protection work in all versions of Excel?

Yes, specific row protection using Aspose.Cells for .NET works in all supported versions of Excel.

Can I protect multiple specific rows in an Excel spreadsheet?

Yes, you can protect multiple specific rows using similar methods described in this guide.

How can I unlock a specific row in an Excel spreadsheet?

To unlock a specific row, you must modify the source code accordingly using the IsLocked method of the Style object.