forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataTable.cs
308 lines (260 loc) · 11.6 KB
/
DataTable.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
#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;
using System.Data;
#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 DataTable : SampleView
{
CGRect frameRect = new CGRect();
float frameMargin = 8.0f;
UILabel label;
UILabel label1;
UIButton button;
UIButton btnInput;
UIButton btnImport;
SfDataGrid sfGrid;
public DataTable()
{
label = new UILabel();
label1 = new UILabel();
button = new UIButton(UIButtonType.System);
button.TouchUpInside += OnExportButtonClicked;
btnInput = new UIButton(UIButtonType.System);
btnInput.TouchUpInside += OnButtonInputClicked;
btnImport = new UIButton(UIButtonType.System);
btnImport.TouchUpInside += OnButtonImportClicked;
sfGrid = new SfDataGrid();
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 = GetEmptyData();
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);
}
private object GetEmptyData()
{
List<CustomerObject> customerObjects = new List<CustomerObject>();
for (int i = 0; i < 20; i++)
customerObjects.Add(new CustomerObject());
return customerObjects;
}
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 DataTable.";
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.Location.X + frameRect.Size.Width, 50);
}
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, 65, 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, 95, 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, 125, frameRect.Size.Width, 10);
}
this.AddSubview(button);
this.sfGrid.Frame = new CGRect(0, 145, this.Frame.Width, this.Frame.Height - 145);
base.LayoutSubviews();
}
void OnButtonInputClicked(object sender, EventArgs e)
{
string resourcePath = "SampleBrowser.Samples.XlsIO.Template.ExportSales.xlsx";
Assembly assembly = Assembly.GetExecutingAssembly();
Stream fileStream = assembly.GetManifestResourceStream(resourcePath);
MemoryStream stream = new MemoryStream();
fileStream.CopyTo(stream);
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
//Export DataTable from Excel worksheet
System.Data.DataTable dataTable = sheet.ExportDataTable(sheet.UsedRange, ExcelExportDataTableOptions.ColumnNames);
//Set exported DataTable as datasource to DataGrid
sfGrid.ItemsSource = dataTable;
button.Enabled = true;
}
void OnExportButtonClicked(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];
//Get DataGrid's datasource as DataTable
System.Data.DataTable dataTable = (System.Data.DataTable)sfGrid.ItemsSource;
//Import DataTable to Excel worksheet.
sheet.ImportDataTable(dataTable, 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("DataTable.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();
}
}
}