|
| 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