-
Notifications
You must be signed in to change notification settings - Fork 204
/
ImportCollectionObjectsPage.xaml.cs
476 lines (404 loc) · 14.4 KB
/
ImportCollectionObjectsPage.xaml.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
#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 COLOR = Syncfusion.Drawing;
using System.Xml;
using Android.Content;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using SampleBrowser;
using Android.App;
using Syncfusion.SfDataGrid;
using System.ComponentModel;
namespace SampleBrowser
{
public partial class ImportCollectionObjectsPage : SamplePage
{
private Context m_context;
SfDataGrid sfGrid;
Button btnExport;
CLRObjectViewModel viewmodel;
public override View GetSampleContent(Context con)
{
LinearLayout linear = new LinearLayout(con);
linear.SetBackgroundColor(Color.White);
linear.Orientation = Orientation.Vertical;
linear.SetPadding(10, 10, 10, 10);
TextView text2 = new TextView(con);
text2.TextSize = 17;
text2.TextAlignment = TextAlignment.Center;
text2.Text = "This sample allows you to import/export data from/to CLR Objects.";
text2.SetTextColor(Color.ParseColor("#262626"));
text2.SetPadding(5, 5, 5, 5);
linear.AddView(text2);
m_context = con;
Button btnInput = new Button(con);
btnInput.Text = "Input Template";
btnInput.Click += ButtonInputClicked;
linear.AddView(btnInput);
Button btnImport = new Button(con);
btnImport.Text = "Import From Excel";
btnImport.Click += ButtonImportClicked;
linear.AddView(btnImport);
btnExport = new Button(con);
btnExport.Text = "Export To Excel";
btnExport.Click += ButtonExportClicked;
btnExport.Enabled = false;
linear.AddView(btnExport);
sfGrid = new SfDataGrid(con);
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;
sfGrid.VerticalOverScrollMode = VerticalOverScrollMode.None;
GridTextColumn brand = new GridTextColumn();
brand.MappingName = "BrandName";
brand.HeaderText = "Brand";
//salesPerson.ItemsSource = viewmodel.CustomersInfo;
brand.HeaderTextAlignment = GravityFlags.Center;
GridTextColumn vehicleType = new GridTextColumn();
vehicleType.MappingName = "VehicleType.VehicleName";
vehicleType.HeaderText = "Vehicle Type";
// salesJanJune.ItemsSource = viewmodel.CustomersInfo;
vehicleType.HeaderTextAlignment = GravityFlags.Center;
GridTextColumn model = new GridTextColumn();
model.MappingName = "VehicleType.Model.ModelName";
model.HeaderText = "Model";
//salesJulyDec.ItemsSource = viewmodel.CustomersInfo;
model.HeaderTextAlignment = GravityFlags.Center;
sfGrid.Columns.Add(brand);
sfGrid.Columns.Add(vehicleType);
sfGrid.Columns.Add(model);
linear.AddView(sfGrid);
return linear;
}
void ButtonInputClicked(object sender, EventArgs e)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
string resourcePath = "";
resourcePath = "SampleBrowser.Samples.XlsIO.Template.ExportData.xlsx";
Assembly assembly = Assembly.GetExecutingAssembly();
Stream fileStream = assembly.GetManifestResourceStream(resourcePath);
IWorkbook workbook = application.Workbooks.Open(fileStream);
IWorksheet sheet = workbook.Worksheets[0];
workbook.Version = ExcelVersion.Excel2013;
MemoryStream stream = new MemoryStream();
workbook.SaveAs(stream);
workbook.Close();
excelEngine.Dispose();
if (stream != null)
{
SaveAndroid androidSave = new SaveAndroid();
androidSave.Save("Input Template.xlsx", "application/msexcel", stream, m_context);
}
}
void ButtonImportClicked(object sender, EventArgs e)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
string resourcePath = "";
resourcePath = "SampleBrowser.Samples.XlsIO.Template.ExportData.xlsx";
Assembly assembly = Assembly.GetExecutingAssembly();
Stream fileStream = assembly.GetManifestResourceStream(resourcePath);
IWorkbook workbook = application.Workbooks.Open(fileStream);
IWorksheet sheet = workbook.Worksheets[0];
workbook.Version = ExcelVersion.Excel2013;
Dictionary<string, string> mappingProperties = new Dictionary<string, string>();
mappingProperties.Add("Brand", "BrandName");
mappingProperties.Add("Vehicle Type", "VehicleType.VehicleName");
mappingProperties.Add("Model", "VehicleType.Model.ModelName");
List<Brand> CLRObjects = sheet.ExportData<Brand>(4, 1, 141, 3, mappingProperties);
sfGrid.ItemsSource = CLRObjects;
btnExport.Enabled = true;
}
void ButtonExportClicked(object sender, EventArgs e)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
#region Initializing Workbook
//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];
sheet.ImportData((List<Brand>)sfGrid.ItemsSource, 4, 1, true);
#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
sheet["A1:C2"].Merge();
sheet["A1"].Text = "Automobile Brands in the US";
sheet["A1"].CellStyle = pageHeader;
sheet["A4:C4"].CellStyle = tableHeader;
sheet["A1:C1"].CellStyle.Font.Bold = true;
sheet.UsedRange.AutofitColumns();
#endregion
#endregion
#region Saving workbook and disposing objects
workbook.Version = ExcelVersion.Excel2013;
MemoryStream stream = new MemoryStream();
workbook.SaveAs(stream);
workbook.Close();
excelEngine.Dispose();
if (stream != null)
{
SaveAndroid androidSave = new SaveAndroid();
androidSave.Save("CLRObjects.xlsx", "application/msexcel", stream, m_context);
}
#endregion
}
}
public class CLRObjectViewModel : INotifyPropertyChanged
{
private bool isDataGridExported;
public bool IsDataGridExported
{
get
{
return isDataGridExported;
}
set
{
isDataGridExported = value;
RaisePropertyChanged("IsDataGridExported");
}
}
public CLRObjectViewModel()
{
CustomersInfo = new List<Brand>();
foreach (int i in Enumerable.Range(1, 20))
{
CustomersInfo.Add(new Brand());
}
}
#region ItemsSource
private List<Brand> customersInfo;
public List<Brand> 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>
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
}
public class Brand
{
private string m_brandName;
[DisplayNameAttribute("Brand")]
public string BrandName
{
get { return m_brandName; }
set
{
m_brandName = value;
RaisePropertyChanged("BrandName");
}
}
private VehicleType m_vehicleType;
public VehicleType VehicleType
{
get { return m_vehicleType; }
set
{
m_vehicleType = value;
RaisePropertyChanged("VehicleType");
}
}
public Brand(string brandName)
{
m_brandName = brandName;
}
public Brand()
{
}
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(String Name)
{
if (PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(Name));
}
#endregion
}
public class VehicleType
{
private string m_vehicleName;
[DisplayNameAttribute("Vehicle Type")]
public string VehicleName
{
get { return m_vehicleName; }
set
{
m_vehicleName = value;
RaisePropertyChanged("VehicleName");
}
}
private ModelObject m_model;
public ModelObject Model
{
get { return m_model; }
set
{
m_model = value;
RaisePropertyChanged("Model");
}
}
public VehicleType(string vehicle)
{
m_vehicleName = vehicle;
}
public VehicleType()
{
}
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(String Name)
{
if (PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(Name));
}
#endregion
}
public class ModelObject
{
private string m_modelName;
[DisplayNameAttribute("Model")]
public string ModelName
{
get { return m_modelName; }
set
{
m_modelName = value;
RaisePropertyChanged("ModelName");
}
}
public ModelObject(string name)
{
m_modelName = name;
}
public ModelObject()
{
}
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(String Name)
{
if (PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(Name));
}
#endregion
}
}