diff --git a/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet.sln b/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet.sln new file mode 100644 index 00000000..cb36fb33 --- /dev/null +++ b/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36127.28 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lock cells in worksheet", "Lock cells in worksheet\Lock cells in worksheet.csproj", "{3EBFBBB9-0A14-4886-929C-4FB78EB16777}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3EBFBBB9-0A14-4886-929C-4FB78EB16777}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3EBFBBB9-0A14-4886-929C-4FB78EB16777}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3EBFBBB9-0A14-4886-929C-4FB78EB16777}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3EBFBBB9-0A14-4886-929C-4FB78EB16777}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {30267131-B722-4B00-8233-EC03587B9AB8} + EndGlobalSection +EndGlobal diff --git a/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet/Data/InputData.xlsx b/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet/Data/InputData.xlsx new file mode 100644 index 00000000..dde904d6 Binary files /dev/null and b/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet/Data/InputData.xlsx differ diff --git a/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet/Lock cells in worksheet.csproj b/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet/Lock cells in worksheet.csproj new file mode 100644 index 00000000..66387043 --- /dev/null +++ b/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet/Lock cells in worksheet.csproj @@ -0,0 +1,23 @@ + + + + Exe + net8.0 + Lock_cells_in_worksheet + enable + enable + + + + + + + + + Always + + + Always + + + diff --git a/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet/Output/.gitkeep b/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet/Program.cs b/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet/Program.cs new file mode 100644 index 00000000..136a2b0e --- /dev/null +++ b/Use Cases/Lock cells in worksheet/.NET/Lock cells in worksheet/Lock cells in worksheet/Program.cs @@ -0,0 +1,38 @@ +using Syncfusion.XlsIO; + +namespace Lock_Cells_In_Worksheet +{ + class Program + { + public static void Main(string[] args) + { + using (ExcelEngine excelEngine = new ExcelEngine()) + { + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputData.xlsx"), FileMode.Open, FileAccess.ReadWrite); + IWorkbook workbook = application.Workbooks.Open(inputStream); + IWorksheet worksheet = workbook.Worksheets[0]; + + //Unlock cells in the worksheet + worksheet.UsedRange.CellStyle.Locked = false; + + //Lock specific cells (A1:A5) + worksheet.Range["A1:A5"].CellStyle.Locked = true; + + //Protect worksheet to apply lock + worksheet.Protect("password", ExcelSheetProtection.All); + + #region Save + //Saving the workbook + FileStream outputStream = new FileStream(Path.GetFullPath("LockedCells.xlsx"), FileMode.Create, FileAccess.Write); + workbook.SaveAs(outputStream); + #endregion + + //Dispose streams + outputStream.Dispose(); + inputStream.Dispose(); + } + } + } +} \ No newline at end of file