-
Notifications
You must be signed in to change notification settings - Fork 204
/
DataTablePage.cs
245 lines (206 loc) · 9.34 KB
/
DataTablePage.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
#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;
using System.Data;
namespace SampleBrowser
{
public partial class DataTablePage : SamplePage
{
private Context m_context;
SfDataGrid sfGrid;
Button btnExport;
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 illustrates how to import/export DataTable from/to an Excel worksheet.";
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);
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 = GetEmptyRows();
sfGrid.VerticalOverScrollMode = VerticalOverScrollMode.None;
GridTextColumn salesPerson = new GridTextColumn();
salesPerson.MappingName = "SalesPerson";
salesPerson.HeaderText = "Name";
salesPerson.HeaderTextAlignment = GravityFlags.Center;
GridTextColumn salesJanJune = new GridTextColumn();
salesJanJune.MappingName = "SalesJanJune";
salesJanJune.HeaderText = "Jan-June";
salesJanJune.HeaderTextAlignment = GravityFlags.Center;
GridTextColumn salesJulyDec = new GridTextColumn();
salesJulyDec.MappingName = "SalesJulyDec";
salesJulyDec.HeaderText = "July-Dec";
salesJulyDec.HeaderTextAlignment = GravityFlags.Center;
GridTextColumn change = new GridTextColumn();
change.MappingName = "Change";
change.HeaderText = "Change";
change.HeaderTextAlignment = GravityFlags.Center;
sfGrid.Columns.Add(salesPerson);
sfGrid.Columns.Add(salesJanJune);
sfGrid.Columns.Add(salesJulyDec);
sfGrid.Columns.Add(change);
linear.AddView(sfGrid);
return linear;
}
private object GetEmptyRows()
{
List<CustomerObject> customerObjects = new List<CustomerObject>();
for (int i = 0; i < 20; i++)
customerObjects.Add(new CustomerObject());
return customerObjects;
}
void ButtonInputClicked(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)
{
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.ExportSales.xlsx";
Assembly assembly = Assembly.GetExecutingAssembly();
Stream fileStream = assembly.GetManifestResourceStream(resourcePath);
IWorkbook workbook = application.Workbooks.Open(fileStream);
IWorksheet sheet = workbook.Worksheets[0];
//Export DataTable from worksheet
System.Data.DataTable dataTable = sheet.ExportDataTable(sheet.UsedRange, ExcelExportDataTableOptions.ColumnNames);
//Set exported DataTable as source to DataGrid.
sfGrid.ItemsSource = dataTable;
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];
DataTable dataTable = (DataTable)sfGrid.ItemsSource;
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
#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("DataTable.xlsx", "application/msexcel", stream, m_context);
}
#endregion
}
}
}