-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f6c0c3
commit c8d19b7
Showing
4 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...llection/.NET/Import data from dynamic collection/Import data from dynamic collection.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
23 changes: 23 additions & 0 deletions
23
...collection/Import data from dynamic collection/Import data from dynamic collection.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<RootNamespace>Import_data_from_dynamic_collection</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="Data\*"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
<None Update="Output\*"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
</Project> |
Empty file.
139 changes: 139 additions & 0 deletions
139
...n/.NET/Import data from dynamic collection/Import data from dynamic collection/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
using Syncfusion.XlsIO; | ||
using System.Dynamic; | ||
|
||
namespace ImportDynamicCollection | ||
{ | ||
/// <summary> | ||
/// Custom dynamic object class | ||
/// </summary> | ||
public class CustomDynamicObject : DynamicObject | ||
{ | ||
/// <summary> | ||
/// The dictionary property used store the data | ||
/// </summary> | ||
internal Dictionary<string, object> properties = new Dictionary<string, object>(); | ||
/// <summary> | ||
/// Provides the implementation for operations that get member values. | ||
/// </summary> | ||
/// <param name="binder">Get Member Binder object</param> | ||
/// <param name="result">The result of the get operation.</param> | ||
/// <returns>true if the operation is successful; otherwise, false.</returns> | ||
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; | ||
} | ||
/// <summary> | ||
/// Provides the implementation for operations that set member values. | ||
/// </summary> | ||
/// <param name="binder">Set memeber binder object</param> | ||
/// <param name="value">The value to set to the member</param> | ||
/// <returns>true if the operation is successful; otherwise, false.</returns> | ||
public override bool TrySetMember(SetMemberBinder binder, object value) | ||
{ | ||
properties[binder.Name] = value; | ||
return true; | ||
} | ||
/// <summary> | ||
/// Return all dynamic member names | ||
/// </summary> | ||
/// <returns>the property name list</returns> | ||
public override IEnumerable<string> GetDynamicMemberNames() | ||
{ | ||
return properties.Keys; | ||
} | ||
} | ||
class Program | ||
{ | ||
/// <summary> | ||
/// Generates a dynamic collection of data representing members reports | ||
/// </summary> | ||
/// <returns>A list of ExpandoObject representing members</returns> | ||
public static List<ExpandoObject> GetMembersReport() | ||
{ | ||
List<ExpandoObject> report = new List<ExpandoObject>(); | ||
|
||
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; | ||
} | ||
/// <summary> | ||
/// Adds a dynamic property to an ExpandoObject | ||
/// </summary> | ||
/// <param name="dynamicObj">The ExpandoObject to which the property will be added</param> | ||
/// <param name="propertyName">The name of the property</param> | ||
/// <param name="propertyValue">The value of the property</param> | ||
public static void AddDynamicProperty(ExpandoObject dynamicObj, string propertyName, object propertyValue) | ||
{ | ||
var expandoDict = dynamicObj as IDictionary<string, object>; | ||
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(); | ||
} | ||
} | ||
} | ||
} |