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
|
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Aspose.Cells;
using System.Data.OleDb;
namespace Aspose.Cells.Demos
{
/// <summary>
/// Summary description for ImportingData.
/// </summary>
public class ImportingData : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList ImportingDataType;
protected System.Web.UI.WebControls.Button btnCreateReport;
protected System.Web.UI.WebControls.DropDownList ddlFileVersion;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnCreateReport.Click += new System.EventHandler(this.btnCreateReport_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnCreateReport_Click(object sender, System.EventArgs e)
{
//Instantiate a new workbook
Workbook workbook = new Workbook();
//Get the first worksheet in the workbook
Worksheet sheet = workbook.Worksheets[0];
//switch case for dropdown's selected value
switch (ImportingDataType.SelectedItem.Text)
{
//if selected text is "Array"
case "Array":
ImportArray(sheet);
break;
//if selected text is "ArrayList"
case "ArrayList":
ImportArrayList(sheet);
break;
//if selected text is "DataColumn"
case "DataColumn":
ImportDataColumn(sheet);
break;
//if selected text is "DataGrid"
case "DataGrid":
ImportDataGrid(sheet);
break;
//if selected text is "DataTable"
case "DataTable":
ImportDataTable(sheet);
break;
//if selected text is "DataView"
case "DataView":
ImportDataView(sheet);
break;
//if selected text is "FormulaArray"
case "FormulaArray":
ImportFormulaArray(sheet);
break;
//if selected text is "FromDataReader"
case "FromDataReader":
ImportFromDataReader(sheet);
break;
//if selected text is "ObjectArray"
case "ObjectArray":
ImportObjectArray(sheet);
break;
//if selected text is "TwoDimensionArray"
case "TwoDimensionArray":
ImportTwoDimensionArray(sheet);
break;
default:
return;
}
if (ddlFileVersion.SelectedItem.Value == "XLS")
{
////Save file and send to client browser using selected format
workbook.Save(HttpContext.Current.Response, "ImportingData.xls", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));
}
else
{
workbook.Save(HttpContext.Current.Response, "ImportingData.xlsx", ContentDisposition.Attachment, new OoxmlSaveOptions(SaveFormat.Xlsx));
}
//end response to avoid unneeded html
HttpContext.Current.Response.End();
}
private void ImportArray(Worksheet sheet)
{
//Get the cells collection in the worksheet
Cells cells = sheet.Cells;
//Put a string value into a cell
cells["A1"].PutValue("Import an Array");
//Get Style Object
Aspose.Cells.Style style = cells["A1"].GetStyle();
//the font text is set to bold
style.Font.IsBold = true;
//Apply style to the cell
cells["A1"].SetStyle(style);
//Create a string array of values
string[] names = new string[] { "Tom", "John", "Kelly" };
//Import the array to the sheet cells
sheet.Cells.ImportArray(names, 1, 0, true);
}
private void ImportArrayList(Worksheet sheet)
{
//Get the cells collection in the worksheet
Cells cells = sheet.Cells;
//Put a string value into a cell
cells["A1"].PutValue("Import an ArrayList");
//Get Style Object
Aspose.Cells.Style style = cells["A1"].GetStyle();
//the font text is set to bold
style.Font.IsBold = true;
//Apply style to the cell
cells["A1"].SetStyle(style);
//Create an arraylist and fill some values to it
ArrayList list = new ArrayList();
list.Add("Tom");
list.Add("John");
list.Add("Kelly");
//Import the arraylist to the sheet cells
sheet.Cells.ImportArrayList(list, 1, 0, true);
}
private void ImportDataColumn(Worksheet sheet)
{
//Get the cells collection in the worksheet
Cells cells = sheet.Cells;
//Put a string value to a cell
cells["A1"].PutValue("Import a DataColumn");
//Get Style Object
Aspose.Cells.Style style = cells["A1"].GetStyle();
//the font text is set to bold
style.Font.IsBold = true;
//Apply style to the cell
cells["A1"].SetStyle(style);
//Create a datatable and add three columns to it
DataTable dataTable = new DataTable("Products");
dataTable.Columns.Add("Product ID", typeof(Int32));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(Int32));
//Add the first record to it
DataRow dr = dataTable.NewRow();
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;
dataTable.Rows.Add(dr);
//Add a second record to it
dr = dataTable.NewRow();
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;
dataTable.Rows.Add(dr);
//Import the datacolumn in the datatable to the sheet cells
sheet.Cells.ImportDataColumn(dataTable, true, 1, 0, 1, false);
}
private void ImportDataGrid(Worksheet sheet)
{
//Get the cells collection in the worksheet
Cells cells = sheet.Cells;
//Put a string value into a cell
sheet.Cells["A1"].PutValue("Import a DataGrid");
//Get Style Object
Aspose.Cells.Style style = cells["A1"].GetStyle();
//the font text is set to bold
style.Font.IsBold = true;
//Apply style to the cell
cells["A1"].SetStyle(style);
//Create a datatable and add three columns to it
DataTable dataTable = new DataTable("Products");
dataTable.Columns.Add("Product ID", typeof(Int32));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(Int32));
//Add the first record to it
DataRow dr = dataTable.NewRow();
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;
dataTable.Rows.Add(dr);
//Add the second record to it
dr = dataTable.NewRow();
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;
dataTable.Rows.Add(dr);
//Create a datagrid
DataGrid dataGrid = new DataGrid();
//set its datasource
dataGrid.DataSource = dataTable;
//bind data
dataGrid.DataBind();
//Import the datagrid to sheet cells
sheet.Cells.ImportDataGrid(dataGrid, 1, 0, false);
//Autofit all the columns in the sheet
sheet.AutoFitColumns();
}
private void ImportDataTable(Worksheet sheet)
{
//Get the cells collection in the worksheet
Cells cells = sheet.Cells;
//Put a string value into a cell
sheet.Cells["A1"].PutValue("Import a DataTable");
//Get Style Object
Aspose.Cells.Style style = cells["A1"].GetStyle();
//the font text is set to bold
style.Font.IsBold = true;
//Apply style to the cell
cells["A1"].SetStyle(style);
//Create a datatable and add three columns to it
DataTable dataTable = new DataTable("Products");
dataTable.Columns.Add("Product ID", typeof(Int32));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(Int32));
//Add the first record to it
DataRow dr = dataTable.NewRow();
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;
dataTable.Rows.Add(dr);
//Add the second record to it
dr = dataTable.NewRow();
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;
dataTable.Rows.Add(dr);
//Import the datatable to sheet cells
sheet.Cells.ImportDataTable(dataTable, true, "A2");
//Autofit all the columns in the sheet
sheet.AutoFitColumns();
}
private void ImportDataView(Worksheet sheet)
{
//Get the cells collection in the worksheet
Cells cells = sheet.Cells;
//Put a string value into a cell
sheet.Cells["A1"].PutValue("Import a DataView");
//Get Style Object
Aspose.Cells.Style style = cells["A1"].GetStyle();
//the font text is set to bold
style.Font.IsBold = true;
//Apply style to the cell
cells["A1"].SetStyle(style);
//Create a datatable and add three columns to it
DataTable dataTable = new DataTable("Products");
dataTable.Columns.Add("Product ID", typeof(Int32));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(Int32));
//Add the first record to it
DataRow dr = dataTable.NewRow();
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;
dataTable.Rows.Add(dr);
//Add the second record to it
dr = dataTable.NewRow();
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;
dataTable.Rows.Add(dr);
//Import the dataview to the sheet cells
sheet.Cells.ImportDataView(dataTable.DefaultView, true, 1, 0, false);
//Autofit all the columns in the sheet
sheet.AutoFitColumns();
}
private void ImportFormulaArray(Worksheet sheet)
{
//Get the cells collection in the worksheet
Cells cells = sheet.Cells;
//Put a string value into a cell
sheet.Cells["A1"].PutValue("Import a formula Array");
//Get Style Object
Aspose.Cells.Style style = cells["A1"].GetStyle();
//the font text is set to bold
style.Font.IsBold = true;
//Apply style to the cell
cells["A1"].SetStyle(style);
//Create a string array and fill it with some formula values
string[] stringArray = { "=LEN(A1)", "=A2*2", "=SUM(A2:A3)" };
//Import the array to the sheet cells
sheet.Cells.ImportFormulaArray(stringArray, 1, 0, true);
}
private void ImportFromDataReader(Worksheet sheet)
{
//Get the cells collection in the worksheet
Cells cells = sheet.Cells;
//Put the string value into a cell
sheet.Cells["A1"].PutValue("Import from DataReader");
//Get Style Object
Aspose.Cells.Style style = cells["A1"].GetStyle();
//the font text is set to bold
style.Font.IsBold = true;
//Apply style to the cell
cells["A1"].SetStyle(style);
string path = Server.MapPath("~");
path = path.Substring(0, path.LastIndexOf("\\")) + "\\Database\\Northwind.mdb";
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path;
string sql = "SELECT Country,EmployeeID,FirstName,LastName FROM Employees ORDER BY Country,EmployeeID";
//Define connection scope
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
//Create command object
OleDbCommand command = new OleDbCommand(sql, conn);
//Open connection
conn.Open();
//Create and fill data reader object
OleDbDataReader reader;
reader = command.ExecuteReader();
//Import the datareader object to the sheet cells
sheet.Cells.ImportDataReader(reader, true, 1, 0, false);
//sheet.Cells.ImportFromDataReader(reader, true, 1, 0, false);
// Always call Close when done reading.
reader.Close();
}
//Autofit all the columns in the sheet
sheet.AutoFitColumns();
}
private void ImportObjectArray(Worksheet sheet)
{
//Get the cells collection in the worksheet
Cells cells = sheet.Cells;
//Put a string value into a cell
sheet.Cells["A1"].PutValue("Import an object Array");
//Get Style Object
Aspose.Cells.Style style = cells["A1"].GetStyle();
//the font text is set to bold
style.Font.IsBold = true;
//Apply style to the cell
cells["A1"].SetStyle(style);
//Create an object array and fill it with some values
object[] obj = { "Tom", "John", "kelly", 1, 2, 2.8, 5.16, true, false };
//Import the object array to the sheet cells
sheet.Cells.ImportObjectArray(obj, 1, 0, false);
//Autofit all the columns in the sheet
sheet.AutoFitColumns();
}
private void ImportTwoDimensionArray(Worksheet sheet)
{
//Get the cells collection in the worksheet
Cells cells = sheet.Cells;
//Put a string value into a cell
sheet.Cells["A1"].PutValue("Import a two-dimension object Array");
//Get Style Object
Aspose.Cells.Style style = cells["A1"].GetStyle();
//the font text is set to bold
style.Font.IsBold = true;
//Apply style to the cell
cells["A1"].SetStyle(style);
//Create a multi-dimensional array and fill some values
object[,] objs = new object[2, 3];
objs[0, 0] = "Product ID";
objs[0, 1] = 1;
objs[0, 2] = 2;
objs[1, 0] = "Product Name";
objs[1, 1] = "Aniseed Syrup";
objs[1, 2] = "Boston Crab Meat";
//Import the multi-dimensional array to the sheet cells
sheet.Cells.ImportTwoDimensionArray(objs, 1, 0);
//Autofit the sheet cells
sheet.AutoFitColumns();
}
}
}
|