From c8d19b7ccba335ad90c49d2efd25b8582f44541d Mon Sep 17 00:00:00 2001 From: KurmithaSF4004 Date: Wed, 9 Oct 2024 18:34:59 +0530 Subject: [PATCH] Use Cases --- .../Import data from dynamic collection.sln | 25 ++++ ...Import data from dynamic collection.csproj | 23 +++ .../Output/.gitkeep | 0 .../Program.cs | 139 ++++++++++++++++++ 4 files changed, 187 insertions(+) create mode 100644 Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection.sln create mode 100644 Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection/Import data from dynamic collection.csproj create mode 100644 Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection/Output/.gitkeep create mode 100644 Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection/Program.cs diff --git a/Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection.sln b/Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection.sln new file mode 100644 index 00000000..775670f7 --- /dev/null +++ b/Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34310.174 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Import data from dynamic collection", "Import data from dynamic collection\Import data from dynamic collection.csproj", "{07E13FCF-C312-4829-A2D2-76C62F3D3BDC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {07E13FCF-C312-4829-A2D2-76C62F3D3BDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {07E13FCF-C312-4829-A2D2-76C62F3D3BDC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {07E13FCF-C312-4829-A2D2-76C62F3D3BDC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {07E13FCF-C312-4829-A2D2-76C62F3D3BDC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D8546202-E535-49D9-BAD2-1AA8F12B5B08} + EndGlobalSection +EndGlobal diff --git a/Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection/Import data from dynamic collection.csproj b/Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection/Import data from dynamic collection.csproj new file mode 100644 index 00000000..5020fefb --- /dev/null +++ b/Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection/Import data from dynamic collection.csproj @@ -0,0 +1,23 @@ + + + + Exe + net8.0 + Import_data_from_dynamic_collection + enable + enable + + + + + + + + + Always + + + Always + + + diff --git a/Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection/Output/.gitkeep b/Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection/Program.cs b/Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection/Program.cs new file mode 100644 index 00000000..e79a3d4d --- /dev/null +++ b/Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection/Program.cs @@ -0,0 +1,139 @@ +using Syncfusion.XlsIO; +using System.Dynamic; + +namespace ImportDynamicCollection +{ + /// + /// Custom dynamic object class + /// + public class CustomDynamicObject : DynamicObject + { + /// + /// The dictionary property used store the data + /// + internal Dictionary properties = new Dictionary(); + /// + /// Provides the implementation for operations that get member values. + /// + /// Get Member Binder object + /// The result of the get operation. + /// true if the operation is successful; otherwise, false. + public override bool TryGetMember(GetMemberBinder binder, out object result) + { + result = default(object); + + if (properties.ContainsKey(binder.Name)) + { + result = properties[binder.Name]; + return true; + } + return false; + } + /// + /// Provides the implementation for operations that set member values. + /// + /// Set memeber binder object + /// The value to set to the member + /// true if the operation is successful; otherwise, false. + public override bool TrySetMember(SetMemberBinder binder, object value) + { + properties[binder.Name] = value; + return true; + } + /// + /// Return all dynamic member names + /// + /// the property name list + public override IEnumerable GetDynamicMemberNames() + { + return properties.Keys; + } + } + class Program + { + /// + /// Generates a dynamic collection of data representing members reports + /// + /// A list of ExpandoObject representing members + public static List GetMembersReport() + { + List report = new List(); + + ExpandoObject obj = new ExpandoObject(); + string propertyName = "Id"; + object propertyValue = 01; + AddDynamicProperty(obj, propertyName, propertyValue); + propertyName = "Name"; + propertyValue = "Karen Fillippe"; + AddDynamicProperty(obj, propertyName, propertyValue); + propertyName = "Age"; + propertyValue = 23; + AddDynamicProperty(obj, propertyName, propertyValue); + report.Add(obj); + + obj = new ExpandoObject(); + propertyName = "Id"; + propertyValue = 02; + AddDynamicProperty(obj, propertyName, propertyValue); + propertyName = "Name"; + propertyValue = "Andy Bernard"; + AddDynamicProperty(obj, propertyName, propertyValue); + propertyName = "Age"; + propertyValue = 20; + AddDynamicProperty(obj, propertyName, propertyValue); + report.Add(obj); + + obj = new ExpandoObject(); + propertyName = "Id"; + propertyValue = 03; + AddDynamicProperty(obj, propertyName, propertyValue); + propertyName = "Name"; + propertyValue = "Jim Halpert"; + AddDynamicProperty(obj, propertyName, propertyValue); + propertyName = "Age"; + propertyValue = 21; + AddDynamicProperty(obj, propertyName, propertyValue); + report.Add(obj); + + return report; + } + /// + /// Adds a dynamic property to an ExpandoObject + /// + /// The ExpandoObject to which the property will be added + /// The name of the property + /// The value of the property + public static void AddDynamicProperty(ExpandoObject dynamicObj, string propertyName, object propertyValue) + { + var expandoDict = dynamicObj as IDictionary; + expandoDict[propertyName] = propertyValue; + } + public static void Main(string[] args) + { + using (ExcelEngine excelEngine = new ExcelEngine()) + { + //Instantiate the excel application object. + IApplication application = excelEngine.Excel; + + //The workbook is created + IWorkbook workbook = application.Workbooks.Create(1); + + //Access the first worksheet + IWorksheet worksheet = workbook.Worksheets[0]; + + //Import the dynamic collection + worksheet.ImportData(GetMembersReport(), 1, 1, true); + + //Auto-fit the columns + worksheet.UsedRange.AutofitColumns(); + + //Saving the workbook + FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write); + workbook.SaveAs(outputStream); + + //Dispose streams + outputStream.Dispose(); + } + } + } +}