forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLRObjects.cs
519 lines (433 loc) · 13.7 KB
/
CLRObjects.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
#region Copyright Syncfusion Inc. 2001-2016.
// Copyright Syncfusion Inc. 2001-2016. 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 Syncfusion.SfDataGrid;
using System.IO;
using COLOR = Syncfusion.Drawing;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.ComponentModel;
#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 CLRObjects : SampleView
{
CGRect frameRect = new CGRect();
float frameMargin = 8.0f;
UILabel label;
UILabel label1;
UIButton button;
UIButton btnInput;
UIButton btnImport;
SfDataGrid sfGrid;
CLRObjectViewModel viewmodel;
public CLRObjects()
{
label = new UILabel();
label1 = new UILabel();
button = new UIButton(UIButtonType.System);
button.TouchUpInside += OnButtonClicked;
btnInput = new UIButton(UIButtonType.System);
btnInput.TouchUpInside += OnButtonInputClicked;
btnImport = new UIButton(UIButtonType.System);
btnImport.TouchUpInside += OnButtonImportClicked;
sfGrid = new SfDataGrid();
viewmodel = new CLRObjectViewModel();
sfGrid.AutoGenerateColumns = false;
sfGrid.RowHeight = 50;
sfGrid.AllowEditing = true;
sfGrid.EditTapAction = TapAction.OnTap;
sfGrid.ColumnSizer = ColumnSizer.Star;
sfGrid.SelectionMode = SelectionMode.None;
sfGrid.HeaderRowHeight = 40;
sfGrid.ItemsSource = viewmodel.CustomersInfo;
GridTextColumn salesPerson = new GridTextColumn();
salesPerson.MappingName = "SalesPerson";
salesPerson.HeaderText = "Name";
salesPerson.HeaderTextAlignment = UIKit.UITextAlignment.Center;
GridTextColumn salesJanJune = new GridTextColumn();
salesJanJune.MappingName = "SalesJanJune";
salesJanJune.HeaderText = "Jan-June";
salesPerson.HeaderTextAlignment = UIKit.UITextAlignment.Center;
GridTextColumn salesJulyDec = new GridTextColumn();
salesJulyDec.MappingName = "SalesJulyDec";
salesJulyDec.HeaderText = "July-Dec";
salesPerson.HeaderTextAlignment = UIKit.UITextAlignment.Center;
GridTextColumn change = new GridTextColumn();
change.MappingName = "Change";
change.HeaderText = "Change";
salesPerson.HeaderTextAlignment = UIKit.UITextAlignment.Center;
sfGrid.Columns.Add(salesPerson);
sfGrid.Columns.Add(salesJanJune);
sfGrid.Columns.Add(salesJulyDec);
sfGrid.Columns.Add(change);
this.AddSubview(sfGrid);
}
void LoadAllowedTextsLabel()
{
label.Frame = frameRect;
label.TextColor = UIColor.FromRGB(38 / 255.0f, 38 / 255.0f, 38 / 255.0f);
label.Text = "This sample allows you to import/export data from/to CLR Objects.";
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, 50);
}
else
{
label.Frame = new CGRect(5, 5, frameRect.Size.Width, 70);
}
this.AddSubview(label);
btnInput.SetTitle("Input Template", UIControlState.Normal);
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
btnInput.Frame = new CGRect(0, 65, frameRect.Location.X + frameRect.Size.Width, 10);
}
else
{
btnInput.Frame = new CGRect(5, 75, frameRect.Size.Width, 10);
}
this.AddSubview(btnInput);
btnImport.SetTitle("Import From Excel", UIControlState.Normal);
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
btnImport.Frame = new CGRect(0, 95, frameRect.Location.X + frameRect.Size.Width, 10);
}
else
{
btnImport.Frame = new CGRect(5, 105, frameRect.Size.Width, 10);
}
this.AddSubview(btnImport);
button.SetTitle("Export To Excel", UIControlState.Normal);
button.Enabled = false;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
button.Frame = new CGRect(0, 125, frameRect.Location.X + frameRect.Size.Width, 10);
}
else
{
button.Frame = new CGRect(5, 135, frameRect.Size.Width, 10);
}
this.AddSubview(button);
this.sfGrid.Frame = new CGRect(0, 155, this.Frame.Width, this.Frame.Height-155);
base.LayoutSubviews();
}
void OnButtonInputClicked(object sender, EventArgs e)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
#region Initializing Workbook
string resourcePath = "SampleBrowser.Samples.XlsIO.Template.ExportSales.xlsx";
Assembly assembly = Assembly.GetExecutingAssembly();
Stream fileStream = assembly.GetManifestResourceStream(resourcePath);
IWorkbook workbook = application.Workbooks.Open(fileStream);
IWorksheet sheet = workbook.Worksheets[0];
#endregion
workbook.Version = ExcelVersion.Excel2013;
MemoryStream stream = new MemoryStream();
workbook.SaveAs(stream);
workbook.Close();
excelEngine.Dispose();
if (stream != null)
{
SaveiOS iOSSave = new SaveiOS();
iOSSave.Save("InputTemplate.xlsx", "application/msexcel", stream);
}
}
void OnButtonImportClicked(object sender, EventArgs e)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
#region Initializing Workbook
string resourcePath = "SampleBrowser.Samples.XlsIO.Template.ExportSales.xlsx";
Assembly assembly = Assembly.GetExecutingAssembly();
Stream fileStream = assembly.GetManifestResourceStream(resourcePath);
IWorkbook workbook = application.Workbooks.Open(fileStream);
IWorksheet sheet = workbook.Worksheets[0];
#endregion
workbook.Version = ExcelVersion.Excel2013;
List<CustomerObject> CLRObjects = sheet.ExportData<CustomerObject>(1, 1, 41, 4);
button.Enabled = true;
sfGrid.ItemsSource = CLRObjects;
}
void OnButtonClicked(object sender, EventArgs e)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
//A new workbook is created.[Equivalent to creating a new workbook in MS Excel]
//The new workbook will have 1 worksheets
IWorkbook workbook = application.Workbooks.Create(1);
//The first worksheet object in the worksheets collection is accessed.
IWorksheet sheet = workbook.Worksheets[0];
Assembly assembly = Assembly.GetExecutingAssembly();
Stream fileStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.XlsIO.Template.CLRObjects.xml");
//IEnumerable<CLRObject> customers = GetCLRObjectsData(fileStream);
sheet.ImportData((List<CustomerObject>)sfGrid.ItemsSource, 5, 1, false);
#region Define Styles
IStyle pageHeader = workbook.Styles.Add("PageHeaderStyle");
IStyle tableHeader = workbook.Styles.Add("TableHeaderStyle");
pageHeader.Font.RGBColor = COLOR.Color.FromArgb(255, 83, 141, 213);
pageHeader.Font.FontName = "Calibri";
pageHeader.Font.Size = 18;
pageHeader.Font.Bold = true;
pageHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;
pageHeader.VerticalAlignment = ExcelVAlign.VAlignCenter;
tableHeader.Font.Color = ExcelKnownColors.Black;
tableHeader.Font.Bold = true;
tableHeader.Font.Size = 11;
tableHeader.Font.FontName = "Calibri";
tableHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;
tableHeader.VerticalAlignment = ExcelVAlign.VAlignCenter;
tableHeader.Color = COLOR.Color.FromArgb(255, 118, 147, 60);
tableHeader.Borders[ExcelBordersIndex.EdgeLeft].LineStyle = ExcelLineStyle.Thin;
tableHeader.Borders[ExcelBordersIndex.EdgeRight].LineStyle = ExcelLineStyle.Thin;
tableHeader.Borders[ExcelBordersIndex.EdgeTop].LineStyle = ExcelLineStyle.Thin;
tableHeader.Borders[ExcelBordersIndex.EdgeBottom].LineStyle = ExcelLineStyle.Thin;
#endregion
#region Apply Styles
// Apply style for header
sheet["A1:D1"].Merge();
sheet["A1"].Text = "Yearly Sales Report";
sheet["A1"].CellStyle = pageHeader;
sheet["A1"].RowHeight = 20;
sheet["A2:D2"].Merge();
sheet["A2"].Text = "Namewise Sales Comparison Report";
sheet["A2"].CellStyle = pageHeader;
sheet["A2"].CellStyle.Font.Bold = false;
sheet["A2"].CellStyle.Font.Size = 16;
sheet["A2"].RowHeight = 20;
sheet["A3:A4"].Merge();
sheet["D3:D4"].Merge();
sheet["B3:C3"].Merge();
sheet["B3"].Text = "Sales";
sheet["A3:D4"].CellStyle = tableHeader;
sheet["A3"].Text = "Sales Person";
sheet["B4"].Text = "Jan - Jun";
sheet["C4"].Text = "Jul - Dec";
sheet["D3"].Text = "Change (%)";
sheet.Columns[0].ColumnWidth = 19;
sheet.Columns[1].ColumnWidth = 10;
sheet.Columns[2].ColumnWidth = 10;
sheet.Columns[3].ColumnWidth = 11;
#endregion
workbook.Version = ExcelVersion.Excel2013;
MemoryStream stream = new MemoryStream();
workbook.SaveAs(stream);
workbook.Close();
excelEngine.Dispose();
if (stream != null)
{
SaveiOS iOSSave = new SaveiOS();
iOSSave.Save("ImportCLRObjects.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));
LoadAllowedTextsLabel();
base.LayoutSubviews();
}
private IEnumerable<CLRObject> GetCLRObjectsData(Stream xml)
{
XmlReader reader = XmlReader.Create(xml);
List<CLRObject> collection = new List<CLRObject>();
string name = "";
int firstHalf = 0;
int secondHalf = 0;
int value = 0;
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "Customers":
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "SalesPerson":
reader.Read();
name = reader.Value;
break;
case "SalesJanJune":
reader.Read();
firstHalf = XmlConvert.ToInt32(reader.Value);
break;
case "SalesJulyDec":
reader.Read();
secondHalf = XmlConvert.ToInt32(reader.Value);
break;
case "Change":
reader.Read();
value = XmlConvert.ToInt32(reader.Value);
CLRObject temp = new CLRObject();
temp.SalesPerson = name;
temp.SalesJanJune = firstHalf;
temp.SalesJulyDec = secondHalf;
temp.Change = value;
collection.Add(temp);
break;
}
}
else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Customers")
break;
}
break;
}
}
}
return collection.AsEnumerable();
}
}
[Preserve(AllMembers = true)]
public class CLRObjectViewModel : INotifyPropertyChanged
{
private bool isDataGridExported;
public bool IsDataGridExported
{
get
{
return isDataGridExported;
}
set
{
isDataGridExported = value;
RaisePropertyChanged("IsDataGridExported");
}
}
public CLRObjectViewModel()
{
CustomersInfo = new List<CustomerObject>();
foreach (int i in Enumerable.Range(1, 20))
{
CustomersInfo.Add(new CustomerObject());
}
}
#region ItemsSource
private List<CustomerObject> customersInfo;
public List<CustomerObject> CustomersInfo
{
get { return customersInfo; }
set { this.customersInfo = value; RaisePropertyChanged("CustomersInfo"); }
}
#endregion
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(String name)
{
if (PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
#endregion
}
/// <summary>
/// Class used to store the customer details
/// </summary>
///
[Preserve(AllMembers = true)]
public class CustomerObject : INotifyPropertyChanged
{
public CustomerObject()
{
}
#region private variables
private string _salesPerson;
private int _salesJanJune;
private int _salesJulyDec;
private string _change;
#endregion
#region Public Properties
public string SalesPerson
{
get
{
return _salesPerson;
}
set
{
this._salesPerson = value;
RaisePropertyChanged("SalesPerson");
}
}
public int SalesJanJune
{
get
{
return _salesJanJune;
}
set
{
this._salesJanJune = value;
RaisePropertyChanged("SalesJanJune");
}
}
public int SalesJulyDec
{
get
{
return _salesJulyDec;
}
set
{
this._salesJulyDec = value;
RaisePropertyChanged("SalesJulyDec");
}
}
public string Change
{
get
{
return _change;
}
set
{
this._change = value;
RaisePropertyChanged("Change");
}
}
#endregion
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(String Name)
{
if (PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(Name));
}
#endregion
}
}