forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
217 lines (197 loc) · 6.3 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#region Copyright Syncfusion Inc. 2001-2022.
// Copyright Syncfusion Inc. 2001-2022. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using Syncfusion.XlsIO;
using System.Xml;
using Foundation;
namespace SampleBrowser
{
public static class TypeExtension
{
public static PropertyInfo[] GetProperties(this Type type)
{
IEnumerator<PropertyInfo> propertyEnum = type.GetTypeInfo().DeclaredProperties.GetEnumerator();
IList<PropertyInfo> listProperties = new List<PropertyInfo>();
while (propertyEnum.MoveNext())
{
listProperties.Add(propertyEnum.Current);
}
return listProperties.ToArray<PropertyInfo>();
}
public static PropertyInfo GetProperty(this Type type,string name)
{
IEnumerator<PropertyInfo> propertyEnum = type.GetTypeInfo().DeclaredProperties.GetEnumerator();
while (propertyEnum.MoveNext())
{
if (propertyEnum.Current.Name == name)
return propertyEnum.Current;
}
return null;
}
}
public class XlsIOExtensions
{
/// <summary>
/// Import XML file into XlsIO
/// </summary>
/// <param name="fileStream">XML file stream</param>
/// <param name="sheet">Worksheet to import</param>
/// <param name="row">Row to which import begin</param>
/// <param name="col">Column to which import begin</param>
/// <param name="header">Imports header if true</param>
public void ImportXML(Stream fileStream, IWorksheet sheet, int row, int col, bool header)
{
XmlReader reader = XmlReader.Create(fileStream);
IEnumerable<Customers> customers = GetData(reader);
PropertyInfo[] propertyInfo = null;
bool headerXML = true; int newCol = col;
foreach (object obj in customers)
{
if (obj != null)
{
propertyInfo = obj.GetType().GetProperties();
if (header && headerXML)
{
foreach (var cell in propertyInfo)
{
sheet[row, newCol].Text = cell.Name;
newCol++;
}
row++;
headerXML = false;
}
newCol = col;
foreach (var cell in propertyInfo)
{
Type currentRecordType = obj.GetType();
PropertyInfo property = currentRecordType.GetProperty(cell.Name);
sheet[row, newCol].Value2 = property.GetValue(obj, null);
newCol++;
}
headerXML = false;
row++;
}
}
}
private IEnumerable<Customers> GetData(XmlReader reader )
{
List<Customers> collection = new List<Customers>();
string customers = "";
string companyName = "";
string contactName = "";
string contactTitle = "";
string address = "";
string city = "";
string postalCode = "";
string country = "";
string phone = "";
string fax = "";
while (reader.Read ())
{
if (reader.IsStartElement ())
{
switch (reader.Name)
{
case "Customers":
while (reader.Read ()) {
if (reader.IsStartElement ()) {
switch (reader.Name) {
case "CustomerID":
reader.Read ();
customers = reader.Value;
break;
case "CompanyName":
reader.Read ();
companyName = reader.Value;
break;
case "ContactName":
reader.Read ();
contactName = reader.Value;
break;
case "ContactTitle":
reader.Read ();
contactTitle = reader.Value;
break;
case "Address":
reader.Read ();
address = reader.Value;
break;
case "City":
reader.Read ();
city = reader.Value;
break;
case "PostalCode":
reader.Read ();
postalCode = reader.Value;
break;
case "Country":
reader.Read ();
country = reader.Value;
break;
case "Phone":
reader.Read ();
phone = reader.Value;
break;
case "Fax":
reader.Read ();
fax = reader.Value;
Customers temp = new Customers (customers, companyName, contactName, contactTitle, address, city, postalCode, country, phone, fax);
collection.Add (temp);
break;
}
} else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Customers")
break;
}
break;
}
}
}
return collection.AsEnumerable();
}
}
[Preserve(AllMembers = true)]
public class Customers
{
public Customers(string id, string company,string name, string title,string address,string city,string code,string country,string phone,string fax)
{
CustomerID = id;
CompanyName = company;
ContactName = name;
ContactTitle = title;
Address = address;
City = city;
PostalCode = code;
Country = country;
Phone = phone;
Fax = fax;
}
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
}
[Preserve(AllMembers = true)]
public class CLRObject
{
public string SalesPerson { get; set; }
public int SalesJanJune { get; set; }
public int SalesJulyDec { get; set; }
public int Change { get; set; }
}
}