Skip to content

Commit c8d19b7

Browse files
committed
Use Cases
1 parent 6f6c0c3 commit c8d19b7

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34310.174
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
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}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{07E13FCF-C312-4829-A2D2-76C62F3D3BDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{07E13FCF-C312-4829-A2D2-76C62F3D3BDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{07E13FCF-C312-4829-A2D2-76C62F3D3BDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{07E13FCF-C312-4829-A2D2-76C62F3D3BDC}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {D8546202-E535-49D9-BAD2-1AA8F12B5B08}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Import_data_from_dynamic_collection</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\*">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\*">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
</Project>

Use Cases/Import data from dynamic collection/.NET/Import data from dynamic collection/Import data from dynamic collection/Output/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using Syncfusion.XlsIO;
2+
using System.Dynamic;
3+
4+
namespace ImportDynamicCollection
5+
{
6+
/// <summary>
7+
/// Custom dynamic object class
8+
/// </summary>
9+
public class CustomDynamicObject : DynamicObject
10+
{
11+
/// <summary>
12+
/// The dictionary property used store the data
13+
/// </summary>
14+
internal Dictionary<string, object> properties = new Dictionary<string, object>();
15+
/// <summary>
16+
/// Provides the implementation for operations that get member values.
17+
/// </summary>
18+
/// <param name="binder">Get Member Binder object</param>
19+
/// <param name="result">The result of the get operation.</param>
20+
/// <returns>true if the operation is successful; otherwise, false.</returns>
21+
public override bool TryGetMember(GetMemberBinder binder, out object result)
22+
{
23+
result = default(object);
24+
25+
if (properties.ContainsKey(binder.Name))
26+
{
27+
result = properties[binder.Name];
28+
return true;
29+
}
30+
return false;
31+
}
32+
/// <summary>
33+
/// Provides the implementation for operations that set member values.
34+
/// </summary>
35+
/// <param name="binder">Set memeber binder object</param>
36+
/// <param name="value">The value to set to the member</param>
37+
/// <returns>true if the operation is successful; otherwise, false.</returns>
38+
public override bool TrySetMember(SetMemberBinder binder, object value)
39+
{
40+
properties[binder.Name] = value;
41+
return true;
42+
}
43+
/// <summary>
44+
/// Return all dynamic member names
45+
/// </summary>
46+
/// <returns>the property name list</returns>
47+
public override IEnumerable<string> GetDynamicMemberNames()
48+
{
49+
return properties.Keys;
50+
}
51+
}
52+
class Program
53+
{
54+
/// <summary>
55+
/// Generates a dynamic collection of data representing members reports
56+
/// </summary>
57+
/// <returns>A list of ExpandoObject representing members</returns>
58+
public static List<ExpandoObject> GetMembersReport()
59+
{
60+
List<ExpandoObject> report = new List<ExpandoObject>();
61+
62+
ExpandoObject obj = new ExpandoObject();
63+
string propertyName = "Id";
64+
object propertyValue = 01;
65+
AddDynamicProperty(obj, propertyName, propertyValue);
66+
propertyName = "Name";
67+
propertyValue = "Karen Fillippe";
68+
AddDynamicProperty(obj, propertyName, propertyValue);
69+
propertyName = "Age";
70+
propertyValue = 23;
71+
AddDynamicProperty(obj, propertyName, propertyValue);
72+
report.Add(obj);
73+
74+
obj = new ExpandoObject();
75+
propertyName = "Id";
76+
propertyValue = 02;
77+
AddDynamicProperty(obj, propertyName, propertyValue);
78+
propertyName = "Name";
79+
propertyValue = "Andy Bernard";
80+
AddDynamicProperty(obj, propertyName, propertyValue);
81+
propertyName = "Age";
82+
propertyValue = 20;
83+
AddDynamicProperty(obj, propertyName, propertyValue);
84+
report.Add(obj);
85+
86+
obj = new ExpandoObject();
87+
propertyName = "Id";
88+
propertyValue = 03;
89+
AddDynamicProperty(obj, propertyName, propertyValue);
90+
propertyName = "Name";
91+
propertyValue = "Jim Halpert";
92+
AddDynamicProperty(obj, propertyName, propertyValue);
93+
propertyName = "Age";
94+
propertyValue = 21;
95+
AddDynamicProperty(obj, propertyName, propertyValue);
96+
report.Add(obj);
97+
98+
return report;
99+
}
100+
/// <summary>
101+
/// Adds a dynamic property to an ExpandoObject
102+
/// </summary>
103+
/// <param name="dynamicObj">The ExpandoObject to which the property will be added</param>
104+
/// <param name="propertyName">The name of the property</param>
105+
/// <param name="propertyValue">The value of the property</param>
106+
public static void AddDynamicProperty(ExpandoObject dynamicObj, string propertyName, object propertyValue)
107+
{
108+
var expandoDict = dynamicObj as IDictionary<string, object>;
109+
expandoDict[propertyName] = propertyValue;
110+
}
111+
public static void Main(string[] args)
112+
{
113+
using (ExcelEngine excelEngine = new ExcelEngine())
114+
{
115+
//Instantiate the excel application object.
116+
IApplication application = excelEngine.Excel;
117+
118+
//The workbook is created
119+
IWorkbook workbook = application.Workbooks.Create(1);
120+
121+
//Access the first worksheet
122+
IWorksheet worksheet = workbook.Worksheets[0];
123+
124+
//Import the dynamic collection
125+
worksheet.ImportData(GetMembersReport(), 1, 1, true);
126+
127+
//Auto-fit the columns
128+
worksheet.UsedRange.AutofitColumns();
129+
130+
//Saving the workbook
131+
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
132+
workbook.SaveAs(outputStream);
133+
134+
//Dispose streams
135+
outputStream.Dispose();
136+
}
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)