New to Telerik Document Processing? Download free 30-day trial

Protect Specific Worksheet Cells Using RadSpreadProcessing

Environment

Version Product Author
2025.4.1104 RadSpreadProcessing Yoan Karamanov

Description

This article shows how to protect only certain cells in a Worksheet using SpreadProcessing. The example demonstrates the most efficient way to keep all cells unlocked except the first row of a used range, which will remain locked and protected.

Solution

In SpreadProcessing, Worksheet protection locks all cells by default. To protect only specific cells:

  1. Protect the worksheet.
  2. Unlock all columns (faster than iterating rows).
  3. Determine the used cell range.
  4. Lock only the first row cells within the used range.
  5. Export the result.

Below is a complete example:

Workbook workbook;
IWorkbookFormatProvider xlsxFormatProvider = new XlsxFormatProvider();

using (Stream input = new FileStream("input.xlsx", FileMode.Open))
{
    workbook = xlsxFormatProvider.Import(input, TimeSpan.FromSeconds(10));
}

Worksheet worksheet = workbook.ActiveWorksheet;

// Protect the worksheet (default options)
worksheet.Protect("telerik", WorksheetProtectionOptions.Default);

// Unlock all columns in the worksheet (empty ones included)
for (int columnIndex = 0; columnIndex <= SpreadsheetDefaultValues.ColumnCount - 1; columnIndex++)
{
    worksheet.Columns[columnIndex].SetIsLocked(false);
}

// Get the used cells range
CellRange usedCellRange = worksheet.UsedCellRange;

// Lock all first row cells of the used range
for (int columnIndex = usedCellRange.FromIndex.ColumnIndex; columnIndex <= usedCellRange.ToIndex.ColumnIndex; columnIndex++)
{
    CellSelection cell = worksheet.Cells[0, columnIndex];
    cell.SetIsLocked(true);
}

// Export to XLSX
string xlsxOutputPath = "output.xlsx";
using (Stream output = new FileStream(xlsxOutputPath, FileMode.Create))
{
    xlsxFormatProvider.Export(workbook, output, TimeSpan.FromSeconds(10));
}

Key Points

  • Protecting columns instead of rows is significantly faster because the maximum number of columns (16,384) is far smaller than the maximum number of rows (1,048,576).
  • By unlocking all columns first, you can selectively lock only the cells you need before applying protection.

See Also

In this article