forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportNestedCollection.cs
613 lines (537 loc) · 22.5 KB
/
ImportNestedCollection.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#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 Syncfusion.XlsIO;
using System.IO;
using Syncfusion.Drawing;
using System.Reflection;
using System.Collections.Generic;
using Syncfusion.iOS.Buttons;
using System.ComponentModel;
using System.Xml.Serialization;
#if __UNIFIED__
using Foundation;
using UIKit;
using CoreGraphics;
#else
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using CGRect = System.Drawing.RectangleF;
using CGPoint = System.Drawing.PointF;
using CGSize = System.Drawing.SizeF;
#endif
namespace SampleBrowser
{
public partial class ImportNestedCollection : SampleView
{
public ImportNestedCollection()
{
this.layoutList.Add((NSString)"Default");
this.layoutList.Add((NSString)"Merge");
this.layoutList.Add((NSString)"Repeat");
}
CGRect frameRect = new CGRect ();
float frameMargin = 8.0f;
private readonly IList<string> layoutList = new List<string>();
string selectedLayout;
UILabel layoutLabel;
UIButton layoutDoneButton;
UIButton layoutButton;
UIPickerView layoutPicker;
UILabel groupLabel;
UISwitch groupSwitch;
UILabel groupOptionsLabel;
SfRadioGroup radioGroup = new SfRadioGroup();
SfRadioButton expandButton = new SfRadioButton();
SfRadioButton collapseButton = new SfRadioButton();
UITextField textField;
UIButton button;
bool isLoaded = false;
bool isGroupEnabled = false;
bool isTextEnabled = false;
void LoadAllowedTextsLabel()
{
#region Description Label
UILabel label = new UILabel ();
label.Frame = frameRect;
label.TextColor = UIColor.FromRGB (38/255.0f, 38/255.0f, 38/255.0f);
label.Text = "This sample demonstrates how to filter data within a range of Excel worksheet.";
label.Font = UIFont.SystemFontOfSize(15);
label.Lines = 0;
label.LineBreakMode = UILineBreakMode.WordWrap;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) {
label.Font = UIFont.SystemFontOfSize(18);
label.Frame = new CGRect (5, 5,frameRect.Location.X + frameRect.Size.Width ,35);
}
else {
label.Frame = new CGRect(5, 5, frameRect.Location.X + frameRect.Size.Width ,50);
}
this.AddSubview (label);
#endregion
#region Layout Region
layoutLabel = new UILabel();
layoutDoneButton = new UIButton();
layoutButton = new UIButton();
layoutPicker = new UIPickerView();
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
layoutLabel.Font = UIFont.SystemFontOfSize(18);
layoutLabel.Frame = new CGRect(10, 45, this.Frame.Size.Width / 2, 50);
layoutButton.Frame = new CGRect(150, 45, this.Frame.Size.Width / 2, 40);
}
else
{
layoutLabel.Frame = new CGRect(10, 50, this.Frame.Size.Width / 2, 50);
layoutButton.Frame = new CGRect(150, 50, this.Frame.Size.Width / 2, 40);
}
//filter Label
layoutLabel.TextColor = UIColor.Black;
layoutLabel.BackgroundColor = UIColor.Clear;
layoutLabel.Text = @"Layout Options";
layoutLabel.TextAlignment = UITextAlignment.Left;
layoutLabel.Font = UIFont.FromName("Helvetica", 16f);
this.AddSubview(layoutLabel);
//filter button
layoutButton.SetTitle("Default", UIControlState.Normal);
layoutButton.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
layoutButton.BackgroundColor = UIColor.Clear;
layoutButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
layoutButton.Hidden = false;
layoutButton.Layer.BorderColor = UIColor.FromRGB(246, 246, 246).CGColor;
layoutButton.Layer.BorderWidth = 4;
layoutButton.Layer.CornerRadius = 8;
layoutButton.Font = UIFont.FromName("Helvetica", 16f);
this.AddSubview(layoutButton);
layoutButton.TouchUpInside += ShowLayoutPicker;
//filterpicker
PickerModel layoutPickermodel = new PickerModel(this.layoutList);
layoutPickermodel.PickerChanged += (sender, e) =>
{
this.selectedLayout = e.SelectedValue;
layoutButton.SetTitle(selectedLayout, UIControlState.Normal);
};
layoutPicker = new UIPickerView();
layoutPicker.ShowSelectionIndicator = true;
layoutPicker.Hidden = true;
layoutPicker.Model = layoutPickermodel;
layoutPicker.BackgroundColor = UIColor.White;
//filterDoneButtonn
layoutDoneButton = new UIButton();
layoutDoneButton.SetTitle("Done\t", UIControlState.Normal);
layoutDoneButton.HorizontalAlignment = UIControlContentHorizontalAlignment.Right;
layoutDoneButton.BackgroundColor = UIColor.FromRGB(240, 240, 240);
layoutDoneButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
layoutDoneButton.Hidden = true;
layoutDoneButton.TouchUpInside += HideLayoutPicker;
layoutPicker.Frame = new CGRect(150, 50, this.Frame.Size.Width / 2, this.Frame.Size.Height / 3);
layoutDoneButton.Frame = new CGRect(150, 55, this.Frame.Size.Width / 2, 40);
this.AddSubview(layoutPicker);
this.AddSubview(layoutDoneButton);
#endregion
#region Group Region
//unique records label
groupLabel = new UILabel();
groupLabel.TextColor = UIColor.Black;
groupLabel.BackgroundColor = UIColor.Clear;
groupLabel.Text = @"Apply Group";
groupLabel.TextAlignment = UITextAlignment.Left;
groupLabel.Font = UIFont.FromName("Helvetica", 16f);
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
groupLabel.Frame = new CGRect(60, 105, this.Frame.Size.Width / 2, 40);
else
groupLabel.Frame = new CGRect(60, 110, this.Frame.Size.Width / 2, 40);
this.AddSubview(groupLabel);
//unique records switch
groupSwitch = new UISwitch();
groupSwitch.On = false;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
groupSwitch.Frame = new CGRect(150, 105, this.Frame.Size.Width / 2, 40);
else
groupSwitch.Frame = new CGRect(150, 110, this.Frame.Size.Width / 2, 40);
groupSwitch.TouchUpInside += ShowGroupOptions;
this.AddSubview(groupSwitch);
#endregion
#region Group Options Region
//filter Label
groupOptionsLabel = new UILabel();
groupOptionsLabel.TextColor = UIColor.Black;
groupOptionsLabel.BackgroundColor = UIColor.Clear;
groupOptionsLabel.Text = "Options";
groupOptionsLabel.TextAlignment = UITextAlignment.Left;
groupOptionsLabel.Font = UIFont.FromName("Helvetica", 16f);
this.AddSubview(groupOptionsLabel);
groupOptionsLabel.Hidden = true;
radioGroup.Axis = UILayoutConstraintAxis.Vertical;
expandButton.SetTitle("Expand", UIControlState.Normal);
radioGroup.AddArrangedSubview(expandButton);
collapseButton.SetTitle("Group at Level", UIControlState.Normal);
radioGroup.AddArrangedSubview(collapseButton);
expandButton.IsChecked = true;
radioGroup.Distribution = UIStackViewDistribution.FillProportionally;
this.AddSubview(radioGroup);
radioGroup.Hidden = true;
expandButton.StateChanged += ShowHideTextField;
collapseButton.StateChanged += ShowHideTextField;
textField = new UITextField();
textField.TextColor = UIColor.Black;
textField.BackgroundColor = UIColor.Clear;
textField.Text = "1";
textField.TextAlignment = UITextAlignment.Left;
textField.Font = UIFont.FromName("Helvetica", 16f);
this.AddSubview(textField);
textField.Hidden = true;
#endregion
#region Button Region
//button
button = new UIButton (UIButtonType.System);
button.SetTitle("Generate Excel", UIControlState.Normal);
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
button.Frame = new CGRect(0, 155, frameRect.Location.X + frameRect.Size.Width, 20);
}
else
{
button.Frame = new CGRect(0, 160, frameRect.Location.X + frameRect.Size.Width, 20);
}
button.TouchUpInside += OnButtonClicked;
this.AddSubview (button);
#endregion
isLoaded = true;
}
void OnButtonClicked(object sender, EventArgs e)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
IList<Brands> list = GetVehicleDetails();
int index = layoutList.IndexOf(selectedLayout);
ExcelImportDataOptions importDataOptions = new ExcelImportDataOptions();
importDataOptions.FirstRow = 4;
if (index == 0)
importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Default;
else if (index == 1)
importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Merge;
else if (index == 2)
importDataOptions.NestedDataLayoutOptions = ExcelNestedDataLayoutOptions.Repeat;
if (groupSwitch.Selected)
{
if (expandButton.IsChecked.Value)
{
importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Expand;
}
else if (collapseButton.IsChecked.Value)
{
importDataOptions.NestedDataGroupOptions = ExcelNestedDataGroupOptions.Collapse;
if (textField.Text != string.Empty)
{
importDataOptions.CollapseLevel = int.Parse(textField.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 = Color.FromArgb(0, 146, 208, 80);
pageHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;
pageHeader.VerticalAlignment = ExcelVAlign.VAlignCenter;
tableHeader.Font.Bold = true;
tableHeader.Font.FontName = "Calibri";
tableHeader.Color = 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)
{
SaveiOS iOSSave = new SaveiOS ();
iOSSave.Save ("ImportData.xlsx", "application/msexcel", stream);
}
}
public override void LayoutSubviews ()
{
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) {
frameMargin = 0.0f;
}
frameRect = Bounds;
frameRect.Location = new CGPoint (frameRect.Location.X + frameMargin, frameRect.Location.Y + 1.5f * frameMargin);
frameRect.Size = new CGSize(frameRect.Size.Width - (frameRect.Location.X + frameMargin), frameRect.Size.Height - (frameRect.Location.Y + 1.5f * frameMargin));
if(!isLoaded)
LoadAllowedTextsLabel ();
base.LayoutSubviews();
}
void ShowLayoutPicker(object sender, EventArgs e)
{
layoutDoneButton.Hidden = false;
layoutPicker.Hidden = false;
groupLabel.Hidden = true;
groupSwitch.Hidden = true;
groupOptionsLabel.Hidden = true;
radioGroup.Hidden = true;
textField.Hidden = true;
button.Hidden = true;
this.BecomeFirstResponder();
}
void HideLayoutPicker(object sender, EventArgs e)
{
layoutDoneButton.Hidden = true;
layoutPicker.Hidden = true;
groupLabel.Hidden = false;
groupSwitch.Hidden = false;
if (isGroupEnabled)
{
groupOptionsLabel.Hidden = false;
radioGroup.Hidden = false;
if (isTextEnabled)
{
textField.Hidden = false;
}
}
button.Hidden = false;
this.BecomeFirstResponder();
}
void ShowGroupOptions(object sender, EventArgs e)
{
isGroupEnabled = true;
if (groupSwitch.On)
{
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
groupOptionsLabel.Frame = new CGRect(150, 150, this.Frame.Size.Width / 2, 25);
else
groupOptionsLabel.Frame = new CGRect(150, 155, this.Frame.Size.Width / 2, 25);
groupOptionsLabel.Hidden = false;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
radioGroup.Frame = new CGRect(150, 180, this.Frame.Size.Width / 2, 80);
}
else
{
radioGroup.Frame = new CGRect(150, 185, this.Frame.Size.Width / 2, 80);
}
radioGroup.Hidden = false;
if (isTextEnabled)
{
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
textField.Frame = new CGRect(160, 270, this.Frame.Size.Width / 2, 30);
else
textField.Frame = new CGRect(160, 275, this.Frame.Size.Width / 2, 30);
textField.Hidden = false;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
button.Frame = new CGRect(0, 310, frameRect.Location.X + frameRect.Size.Width, 20);
}
else
{
button.Frame = new CGRect(0, 315, frameRect.Location.X + frameRect.Size.Width, 20);
}
}
else
{
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
button.Frame = new CGRect(0, 270, frameRect.Location.X + frameRect.Size.Width, 20);
}
else
{
button.Frame = new CGRect(0, 275, frameRect.Location.X + frameRect.Size.Width, 20);
}
}
}
else
{
groupOptionsLabel.Hidden = true;
radioGroup.Hidden = true;
textField.Hidden = true;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
button.Frame = new CGRect(0, 155, frameRect.Location.X + frameRect.Size.Width, 20);
}
else
{
button.Frame = new CGRect(0, 160, frameRect.Location.X + frameRect.Size.Width, 20);
}
isGroupEnabled = false;
}
this.BecomeFirstResponder();
}
void ShowHideTextField(object sender, EventArgs e)
{
if(collapseButton.IsChecked.Value)
{
isTextEnabled = true;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
textField.Frame = new CGRect(160, 270, this.Frame.Size.Width / 2, 30);
else
textField.Frame = new CGRect(160, 275, this.Frame.Size.Width / 2, 30);
textField.Hidden = false;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
button.Frame = new CGRect(0, 310, frameRect.Location.X + frameRect.Size.Width, 20);
}
else
{
button.Frame = new CGRect(0, 315, frameRect.Location.X + frameRect.Size.Width, 20);
}
}
else
{
isTextEnabled = false;
textField.Hidden = true;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
button.Frame = new CGRect(0, 270, frameRect.Location.X + frameRect.Size.Width, 20);
}
else
{
button.Frame = new CGRect(0, 275, frameRect.Location.X + frameRect.Size.Width, 20);
}
}
this.BecomeFirstResponder();
}
#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<Models>();
Models model = new Models(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 Models(brandObj.ModelName));
continue;
}
else
{
vehicle = new VehicleTypes(brandObj.VahicleType);
vehicle.Models = new List<Models>();
vehicle.Models.Add(new Models(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<Models>();
vehicle.Models.Add(new Models(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; }
}
public class Brands
{
private string m_brandName;
[DisplayNameAttribute("Brand")]
public string BrandName
{
get { return m_brandName; }
set { m_brandName = value; }
}
private IList<VehicleTypes> m_vehicleTypes;
public IList<VehicleTypes> VehicleTypes
{
get { return m_vehicleTypes; }
set { m_vehicleTypes = value; }
}
public Brands(string brandName)
{
m_brandName = brandName;
}
}
public class VehicleTypes
{
private string m_vehicleName;
[DisplayNameAttribute("Vehicle Type")]
public string VehicleName
{
get { return m_vehicleName; }
set { m_vehicleName = value; }
}
private IList<Models> m_models;
public IList<Models> Models
{
get { return m_models; }
set { m_models = value; }
}
public VehicleTypes(string vehicle)
{
m_vehicleName = vehicle;
}
}
public class Models
{
private string m_modelName;
[DisplayNameAttribute("Model")]
public string ModelName
{
get { return m_modelName; }
set { m_modelName = value; }
}
public Models(string name)
{
m_modelName = name;
}
}
#endregion
}