Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add samples for importing dynamic data into an Excel #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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>
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();
}
}
}
}
Loading