Back to Devexpress

WorksheetCopyOptions.OverwriteProtectionOnLockedWorksheet Property

officefileapi-devexpress-dot-spreadsheet-dot-worksheetcopyoptions-8abc729e.md

latest3.5 KB
Original Source

WorksheetCopyOptions.OverwriteProtectionOnLockedWorksheet Property

Specifies whether to apply cell protection options of the source worksheet to cells in the protected destination worksheet.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
public bool OverwriteProtectionOnLockedWorksheet { get; set; }
vb
Public Property OverwriteProtectionOnLockedWorksheet As Boolean

Property Value

TypeDescription
Boolean

true if cells in the protected destination worksheet have the same protection options as the source worksheet cells; overwise, false (default).

|

Remarks

If a worksheet where you paste data is protected, you can set the OverwriteProtectionOnLockedWorksheet property to true to copy cell protection settings from the source worksheet and apply them to cells in the destination worksheet.

csharp
using DevExpress.Spreadsheet;
// ...

using (Workbook sourceWorkbook = new Workbook())
using (Workbook targetWorkbook = new Workbook())
{
    targetWorkbook.LoadDocument(@"Documents\Book1.xlsx");
    sourceWorkbook.LoadDocument(@"Documents\Book2.xlsx");
    // Add a new worksheet to the destination workbook.
    Worksheet targetSheet = targetWorkbook.Worksheets.Add("Sheet1_Copy");
    // Protect the worksheet.
    targetSheet.Protect("password", WorksheetProtectionPermissions.Default);
    // Specify copy options.
    var copyOptions = new WorksheetCopyOptions()
    {
        OverwriteProtectionOnLockedWorksheet = true
    };
    // Copy data from "Sheet1" in the source workbook
    // to the newly created worksheet in the destination workbook. 
    targetSheet.CopyFrom(sourceWorkbook.Worksheets["Sheet1"], copyOptions);
    targetWorkbook.SaveDocument("Result.xlsx");
}
vb
Imports DevExpress.Spreadsheet
' ...

Using sourceWorkbook As New Workbook()
Using targetWorkbook As New Workbook()
    targetWorkbook.LoadDocument("Documents\Book1.xlsx")
    sourceWorkbook.LoadDocument("Documents\Book2.xlsx")
    ' Add a new worksheet to the destination workbook.
    Dim targetSheet As Worksheet = targetWorkbook.Worksheets.Add("Sheet1_Copy")
    ' Protect the worksheet.
    targetSheet.Protect("password", WorksheetProtectionPermissions.Default)
    ' Specify copy options.
    Dim copyOptions As New WorksheetCopyOptions() With {
      .OverwriteProtectionOnLockedWorksheet = True
      }
    ' Copy data from "Sheet1" in the source workbook
    ' to the newly created worksheet in the destination workbook. 
    targetSheet.CopyFrom(sourceWorkbook.Worksheets("Sheet1"), copyOptions)
    targetWorkbook.SaveDocument("Result.xlsx")
End Using
End Using

See Also

WorksheetCopyOptions Class

WorksheetCopyOptions Members

DevExpress.Spreadsheet Namespace