forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImportNestedCollectionPage.cs
391 lines (343 loc) · 14.2 KB
/
ImportNestedCollectionPage.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#region Copyright Syncfusion Inc. 2001-2017.
// Copyright Syncfusion Inc. 2001-2017. 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 Syncfusion.XlsIO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Android.Content;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using System.ComponentModel;
using System.Xml.Serialization;
namespace SampleBrowser
{
public partial class ImportNestedCollectionPage : SamplePage
{
private Context m_context;
private Spinner spinner;
private LinearLayout advancedLinear1;
private LinearLayout advancedLinear2;
RadioGroup radioGroup;
RadioButton expandButton;
RadioButton collapseButton;
EditText editText;
private LinearLayout linear;
private Switch switch1;
private Button button1;
public override View GetSampleContent (Context con)
{
int numerHeight = getDimensionPixelSize(con, Resource.Dimension.numeric_txt_ht);
int width = con.Resources.DisplayMetrics.WidthPixels - 40;
linear = new LinearLayout(con);
linear.SetBackgroundColor(Color.White);
linear.Orientation = Orientation.Vertical;
linear.SetPadding(10, 10, 10, 10);
linear.DividerPadding = 0;
TextView text2 = new TextView(con);
text2.TextSize = 17;
text2.TextAlignment = TextAlignment.Center;
text2.Text = "This sample demonstrates how to import data from nested collection with layout and group options.";
text2.SetTextColor(Color.ParseColor("#262626"));
text2.SetPadding(5, 5, 5, 5);
linear.AddView(text2);
TextView space1 = new TextView (con);
space1.TextSize = 10;
linear.AddView (space1);
m_context = con;
LinearLayout subLinear = new LinearLayout(con);
subLinear.Orientation = Orientation.Horizontal;
TextView space2 = new TextView (con);
space2.TextSize = 17;
space2.TextAlignment = TextAlignment.Center;
space2.Text = "Layout Options";
space2.SetTextColor(Color.ParseColor("#262626"));
space2.LayoutParameters = new ViewGroup.LayoutParams(width / 2, numerHeight);
subLinear.AddView (space2);
string[] list = { "Default", "Merge", "Repeat" };
ArrayAdapter<String> array = new ArrayAdapter<String>(con,Android.Resource.Layout.SimpleSpinnerItem , list);
spinner = new Spinner (con);
spinner.Adapter = array;
spinner.SetSelection (0);
subLinear.AddView (spinner);
linear.AddView(subLinear);
advancedLinear1 = new LinearLayout(con);
advancedLinear1.Orientation = Orientation.Horizontal;
TextView space4 = new TextView(con);
space4.TextSize = 17;
space4.TextAlignment = TextAlignment.Center;
space4.Text = "Apply Group";
space4.SetTextColor(Color.ParseColor("#262626"));
space4.LayoutParameters = new ViewGroup.LayoutParams(width / 2, numerHeight);
advancedLinear1.AddView(space4);
switch1 = new Switch(con);
switch1.Checked = false;
advancedLinear1.AddView(switch1);
linear.AddView(advancedLinear1);
LinearLayout advancedLinear3 = new LinearLayout(con);
advancedLinear3.Orientation = Orientation.Horizontal;
TextView space6 = new TextView(con);
space6.TextSize = 17;
space6.TextAlignment = TextAlignment.Center;
space6.Text = "";
space6.SetTextColor(Color.ParseColor("#262626"));
space6.LayoutParameters = new ViewGroup.LayoutParams(width / 2, numerHeight);
advancedLinear3.AddView(space6);
advancedLinear2 = new LinearLayout(con);
advancedLinear2.Orientation = Orientation.Vertical;
TextView space5 = new TextView(con);
space5.TextSize = 17;
space5.TextAlignment = TextAlignment.Center;
space5.Text = "Options";
space5.SetTextColor(Color.ParseColor("#262626"));
space5.LayoutParameters = new ViewGroup.LayoutParams(width / 2, numerHeight - 90);
advancedLinear2.AddView(space5);
radioGroup = new RadioGroup(con);
radioGroup.TextAlignment = TextAlignment.Center;
radioGroup.Orientation = Orientation.Vertical;
expandButton = new RadioButton(con);
expandButton.Text = "Expand";
radioGroup.AddView(expandButton);
collapseButton = new RadioButton(con);
collapseButton.Text = "Collapse at Level";
radioGroup.AddView(collapseButton);
advancedLinear2.AddView(radioGroup);
editText = new EditText(con);
editText.TextSize = 17;
editText.TextAlignment = TextAlignment.Center;
editText.Text = "1";
editText.SetTextColor(Color.ParseColor("#262626"));
advancedLinear3.AddView(advancedLinear2);
button1 = new Button (con);
button1.Text = "Generate Excel";
button1.Click += OnButtonClicked;
linear.AddView(button1);
expandButton.Checked = true;
switch1.CheckedChange += (object sender, CompoundButton.CheckedChangeEventArgs e) =>
{
if (switch1.Checked)
{
linear.RemoveView(button1);
linear.AddView(advancedLinear3);
linear.AddView(button1);
}
else
{
linear.RemoveView(advancedLinear3);
}
};
collapseButton.CheckedChange += (object sender, CompoundButton.CheckedChangeEventArgs e) =>
{
if (collapseButton.Checked)
{
linear.RemoveView(button1);
linear.RemoveView(advancedLinear3);
advancedLinear3.RemoveView(advancedLinear2);
advancedLinear2.AddView(editText);
advancedLinear3.AddView(advancedLinear2);
linear.AddView(advancedLinear3);
linear.AddView(button1);
}
else
{
linear.RemoveView(button1);
linear.RemoveView(advancedLinear3);
advancedLinear3.RemoveView(advancedLinear2);
advancedLinear2.RemoveView(editText);
advancedLinear3.AddView(advancedLinear2);
linear.AddView(advancedLinear3);
linear.AddView(button1);
}
};
return linear;
}
void OnButtonClicked(object sender, EventArgs e)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = excelEngine.Excel.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
IList<Brands> list = GetVehicleDetails();
ExcelImportDataOptions importDataOptions = new ExcelImportDataOptions();
importDataOptions.FirstRow = 4;
if (spinner.SelectedItemPosition == 0)
importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Default;
else if (spinner.SelectedItemPosition == 1)
importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Merge;
else if (spinner.SelectedItemPosition == 2)
importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Repeat;
if (switch1.Checked)
{
if (expandButton.Checked)
{
importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Expand;
}
else if (collapseButton.Checked)
{
importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Collapse;
if (editText.Text != string.Empty)
{
importDataOptions.CollapseLevel = int.Parse(editText.Text);
}
}
}
worksheet.ImportData(list, importDataOptions);
#region Define Styles
IStyle pageHeader = workbook.Styles.Add("PageHeaderStyle");
IStyle tableHeader = workbook.Styles.Add("TableHeaderStyle");
pageHeader.Font.FontName = "Calibri";
pageHeader.Font.Size = 16;
pageHeader.Font.Bold = true;
pageHeader.Color = Syncfusion.Drawing.Color.FromArgb(0, 146, 208, 80);
pageHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;
pageHeader.VerticalAlignment = ExcelVAlign.VAlignCenter;
tableHeader.Font.Bold = true;
tableHeader.Font.FontName = "Calibri";
tableHeader.Color = Syncfusion.Drawing.Color.FromArgb(0, 146, 208, 80);
#endregion
#region Apply Styles
// Apply style for header
worksheet["A1:C2"].Merge();
worksheet["A1"].Text = "Automobile Brands in the US";
worksheet["A1"].CellStyle = pageHeader;
worksheet["A4:C4"].CellStyle = tableHeader;
worksheet["A1:C1"].CellStyle.Font.Bold = true;
worksheet.UsedRange.AutofitColumns();
#endregion
MemoryStream stream = new MemoryStream();
workbook.SaveAs(stream);
workbook.Close();
excelEngine.Dispose();
if (stream != null)
{
SaveAndroid androidSave = new SaveAndroid ();
androidSave.Save ("ImportData.xlsx", "application/msexcel", stream, m_context);
}
}
private int getDimensionPixelSize(Context con, int id)
{
return con.Resources.GetDimensionPixelSize(id);
}
#region Helper Methods
private IList<Brands> GetVehicleDetails()
{
XmlSerializer deserializer = new XmlSerializer(typeof(BrandObjects));
string resourcePath = "SampleBrowser.Samples.XlsIO.Template.ExportData.xml";
Assembly assembly = Assembly.GetExecutingAssembly();
Stream fileStream = assembly.GetManifestResourceStream(resourcePath);
TextReader textReader = new StreamReader(fileStream);
BrandObjects brands = (BrandObjects)deserializer.Deserialize(textReader);
List<Brands> list = new List<Brands>();
string brandName = brands.BrandObject[0].BrandName;
string vehicleType = brands.BrandObject[0].VahicleType;
string modelName = brands.BrandObject[0].ModelName;
Brands brand = new Brands(brandName);
brand.VehicleTypes = new List<VehicleTypes>();
VehicleTypes vehicle = new VehicleTypes(vehicleType);
vehicle.Models = new List<ModelObject>();
ModelObject model = new ModelObject(modelName);
brand.VehicleTypes.Add(vehicle);
list.Add(brand);
foreach (BrandObject brandObj in brands.BrandObject)
{
if (brandName == brandObj.BrandName)
{
if (vehicleType == brandObj.VahicleType)
{
vehicle.Models.Add(new ModelObject(brandObj.ModelName));
continue;
}
else
{
vehicle = new VehicleTypes(brandObj.VahicleType);
vehicle.Models = new List<ModelObject>();
vehicle.Models.Add(new ModelObject(brandObj.ModelName));
brand.VehicleTypes.Add(vehicle);
vehicleType = brandObj.VahicleType;
}
continue;
}
else
{
brand = new Brands(brandObj.BrandName);
vehicle = new VehicleTypes(brandObj.VahicleType);
vehicle.Models = new List<ModelObject>();
vehicle.Models.Add(new ModelObject(brandObj.ModelName));
brand.VehicleTypes = new List<VehicleTypes>();
brand.VehicleTypes.Add(vehicle);
vehicleType = brandObj.VahicleType;
list.Add(brand);
brandName = brandObj.BrandName;
}
}
textReader.Close();
return list;
}
#endregion
}
#region Helper Class
[XmlRootAttribute("BrandObjects")]
public class BrandObjects
{
[XmlElement("BrandObject")]
public BrandObject[] BrandObject { get; set; }
}
public class BrandObject
{
public string BrandName { get; set; }
public string VahicleType { get; set; }
public string ModelName { get; set; }
}
//[Serializable]
public class Brands
{
private string m_brandName;
[DisplayNameAttribute("Brand")]
public string BrandName
{
get { return m_brandName; }
set { m_brandName = value; }
}
private IList<VehicleTypes> m_vehicles;
public IList<VehicleTypes> VehicleTypes
{
get { return m_vehicles; }
set { m_vehicles = value; }
}
public Brands(string brandName)
{
m_brandName = brandName;
}
}
public class VehicleTypes
{
private string m_vehicle;
[DisplayNameAttribute("Vehicle Type")]
public string Vehicle
{
get { return m_vehicle; }
set { m_vehicle = value; }
}
private IList<ModelObject> m_models;
public IList<ModelObject> Models
{
get { return m_models; }
set { m_models = value; }
}
public VehicleTypes(string vehicle)
{
m_vehicle = vehicle;
}
}
#endregion
}