-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcelDataLoader.cs
More file actions
542 lines (450 loc) · 21.8 KB
/
ExcelDataLoader.cs
File metadata and controls
542 lines (450 loc) · 21.8 KB
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#region using statements
using DataJuggler.UltimateHelper;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
#endregion
namespace DataJuggler.Excelerate
{
#region class ExcelDataLoader
/// <summary>
/// This class is used to load Excel Workbooks and Worksheets
/// </summary>
public class ExcelDataLoader
{
#region Methods
#region GetCellText(ExcelWorksheet sheet, int row, int col)
/// <summary>
/// This method returns the Cell Text (what you see, including formatting)
/// </summary>
public static string GetCellText(ExcelWorksheet sheet, int row, int col)
{
// initial value
string cellText = "";
// If the sheet object exists
if (NullHelper.Exists(sheet))
{
// if the value of this cell exists
if (NullHelper.Exists(sheet.Cells[row, col].Value))
{
// Setg retue
cellText = sheet.Cells[row, col].Text;
}
}
// return value
return cellText;
}
#endregion
#region GetCellValue()
/// <summary>
/// This method returns the Cell Value
/// </summary>
public static object GetCellValue(ExcelWorksheet sheet, int row, int col)
{
// initial value
object cellValue = "";
// If the sheet object exists
if (NullHelper.Exists(sheet))
{
// if the value of this cell exists
if (NullHelper.Exists(sheet.Cells[row, col].Value))
{
// Setg retue
cellValue = sheet.Cells[row, col].Value;
}
}
// return value
return cellValue;
}
#endregion
#region GetColumnIndex(ExcelWorksheet excelWorksheet, string columnName)
/// <summary>
/// This method returns the Column Index
/// </summary>
public static int GetColumnIndex(ExcelWorksheet excelWorksheet, string columnName)
{
// initial value
int columnIndex = -1;
// If the columnName string exists
if ((TextHelper.Exists(columnName)) && (NullHelper.Exists(excelWorksheet)))
{
// Set the return value
columnIndex = excelWorksheet.Cells["1:1"].First(c => c.Value.ToString() == columnName).Start.Column;
}
// return value
return columnIndex;
}
#endregion
#region GetSheetNames(string path)
/// <summary>
/// method returns the Sheet Names for the workbook given
/// </summary>
public static List<string> GetSheetNames(string path)
{
// initial value
List<string> sheetNames = new List<string>();
// Load the excelWorkbook
ExcelWorkbook excelWorkbook = LoadExcelWorkbook(path);
// If the excelWorkbook object exists
if (NullHelper.Exists(excelWorkbook))
{
for (int x = 0; x < excelWorkbook.Worksheets.Count; x++)
{
// Add this name
sheetNames.Add(excelWorkbook.Worksheets[x].Name);
}
}
// return value
return sheetNames;
}
#endregion
#region GetSheetNames(ExcelWorkbook excelWorkbook)
/// <summary>
/// method returns the Sheet Names for the workbook given
/// </summary>
public static List<string> GetSheetNames(ExcelWorkbook excelWorkbook)
{
// initial value
List<string> sheetNames = new List<string>();
// If the excelWorkbook object exists
if (NullHelper.Exists(excelWorkbook))
{
for (int x = 0; x < excelWorkbook.Worksheets.Count; x++)
{
// Add this name
sheetNames.Add(excelWorkbook.Worksheets[x].Name);
}
}
// return value
return sheetNames;
}
#endregion
#region LoadAllData(string path)
/// <summary>
/// method returns the All Data
/// </summary>
public static Workbook LoadAllData(string path)
{
// initial value
Workbook workbook = null;
// load the workbook
ExcelWorkbook excelWorkbook = LoadExcelWorkbook(path);
// If the excelWorkbook object exists
if (NullHelper.Exists(excelWorkbook))
{
// Create a new instance of a 'Workbook' object.
workbook = new Workbook();
// Get the sheetNames
List<string> sheetNames = GetSheetNames(excelWorkbook);
// If the sheetNames collection exists and has one or more items
if (ListHelper.HasOneOrMoreItems(sheetNames))
{
// Iterate the collection of string objects
foreach (string sheetName in sheetNames)
{
// Create a new instance of a 'LoadWorksheetInfo' object.
LoadWorksheetInfo loadWorksheetInfo = new LoadWorksheetInfo();
// Set the sheetName
loadWorksheetInfo.SheetName = sheetName;
// Set the LoadColumnOption
loadWorksheetInfo.LoadColumnOptions = LoadColumnOptionsEnum.LoadAllColumnsExceptExcluded;
// Load this worksheet
Worksheet worksheet = LoadWorksheet(excelWorkbook, loadWorksheetInfo);
// if the workbook exists
if (NullHelper.Exists(worksheet))
{
// Add this item
workbook.Worksheets.Add(worksheet);
}
}
}
}
// return value
return workbook;
}
#endregion
#region LoadExcelPackage(string path)
/// <summary>
/// returns the Excel Package
/// </summary>
public static ExcelPackage LoadExcelPackage(string path)
{
// initial value
ExcelPackage excelPackage = null;
// If the path string exists
if (TextHelper.Exists(path))
{
// Create a new instance of a 'FileInfo' object.
FileInfo fileInfo = new FileInfo(path);
// Create the package
excelPackage = new ExcelPackage(fileInfo);
}
// return value
return excelPackage;
}
#endregion
#region LoadExcelWorkbook(string path)
/// <summary>
/// This method returns the Excel Workbook
/// </summary>
public static ExcelWorkbook LoadExcelWorkbook(string path)
{
// initial value
ExcelWorkbook excelWorkbook = null;
// If the path string exists
if (TextHelper.Exists(path))
{
// Create a new instance of a 'FileInfo' object.
FileInfo fileInfo = new FileInfo(path);
// Create the package
var package = new ExcelPackage(fileInfo);
// get the workbook
excelWorkbook = package.Workbook;
}
// return value
return excelWorkbook;
}
#endregion
#region LoadWorkbook(string path, List<LoadWorksheetInfo> sheetsToLoad)
/// <summary>
/// This method loads a Workbook for the path given
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static Workbook LoadWorkbook(string path, List<LoadWorksheetInfo> sheetsToLoad)
{
// initial value
Workbook workbook = new Workbook();
// If the path string exists and there is one or more sheetsToLoad
if (TextHelper.Exists(path) && (ListHelper.HasOneOrMoreItems(sheetsToLoad)))
{
// Create a new instance of a 'FileInfo' object.
FileInfo fileInfo = new FileInfo(path);
// Create the package
var package = new ExcelPackage(fileInfo);
// get the workbook
ExcelWorkbook excelWorkbook = package.Workbook;
// If the excelWorkbook object exists
if (NullHelper.Exists(excelWorkbook))
{
// Iterate the collection of LoadWorksheetInfo objects
foreach (LoadWorksheetInfo loadWorksheetInfo in sheetsToLoad)
{
// Create a workSheet object
Worksheet worksheet = LoadWorksheet(excelWorkbook, loadWorksheetInfo);
// If the worksheet object exists
if (NullHelper.Exists(worksheet))
{
// Add this worksheet
workbook.Worksheets.Add(worksheet);
}
}
}
}
// return value
return workbook;
}
#endregion
#region LoadWorkbook(string path, LoadWorksheetInfo loadWorksheetInfo)
/// <summary>
/// This method loads a Workbook and only one shheet for the path given
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static Workbook LoadWorkbook(string path, LoadWorksheetInfo loadWorksheetInfo)
{
// initial value
Workbook workbook = new Workbook();
// If the path string exists and the sheetToLoad exists
if (TextHelper.Exists(path) && (NullHelper.Exists(loadWorksheetInfo)))
{
// Create a new instance of a 'FileInfo' object.
FileInfo fileInfo = new FileInfo(path);
// Create the package
var package = new ExcelPackage(fileInfo);
// get the workbook
ExcelWorkbook excelWorkbook = package.Workbook;
// If the excelWorkbook object exists
if (NullHelper.Exists(excelWorkbook))
{
// Create a workSheet object
Worksheet workSheet = LoadWorksheet(excelWorkbook, loadWorksheetInfo);
// If the workSheet object exists
if (NullHelper.Exists(workSheet))
{
// Add this worksheet
workbook.Worksheets.Add(workSheet);
}
}
}
// return value
return workbook;
}
#endregion
#region LoadWorksheet(ExcelWorkbook excelWorkbook, LoadWorksheetInfo loadWorksheetInfo)
/// <summary>
/// This method returns the Worksheet
/// </summary>
public static Worksheet LoadWorksheet(ExcelWorkbook excelWorkbook, LoadWorksheetInfo loadWorksheetInfo)
{
// initial value
Worksheet worksheet = null;
// locals
int rowNumber = 0;
int colNumber = 1;
Column column = null;
int columnIndex = -1;
// verify both objects exist
if (NullHelper.Exists(excelWorkbook, loadWorksheetInfo))
{
try
{
//reading Project Information
ExcelWorksheet excelWorksheet = excelWorkbook.Worksheets[loadWorksheetInfo.SheetName];
// If the excelWorksheet object exists
if (NullHelper.Exists(excelWorksheet))
{
// set the rowCount and colCount
int rowCount = excelWorksheet.Dimension.Rows;
int colCount = excelWorksheet.Dimension.Columns;
// if the MawRowsToLoad is set and the MaxRowsToLoad is less than RowCount
if ((loadWorksheetInfo.MaxRowsToLoad > 0) && (loadWorksheetInfo.MaxRowsToLoad < rowCount))
{
// Only load this many rows
rowCount = loadWorksheetInfo.MaxRowsToLoad;
}
// if only a specified number of columns should be loaded
if (loadWorksheetInfo.LoadColumnOptions == LoadColumnOptionsEnum.LoadFirstXColumns)
{
// if the ColumnsToLoad is set and the ColumsnToLoad is less than the number of columns
if ((loadWorksheetInfo.ColumnsToLoad > 0) && (loadWorksheetInfo.ColumnsToLoad < colCount))
{
// Set the colCount
colCount = loadWorksheetInfo.ColumnsToLoad;
}
}
// verify there are rows and columns
if ((rowCount > 0) && (colCount > 0))
{
// Create a new instance of a 'Worksheet' object.
worksheet = new Worksheet();
// Store the loadWorksheetInfo, so saving is easier
worksheet.WorksheetInfo = loadWorksheetInfo;
// Set the sheetName
worksheet.Name = loadWorksheetInfo.SheetName;
do
{
// Increment the value for rowNumber
rowNumber++;
// Create a new instance of a 'Row' object.
Row row = new Row();
// Set the rowNumber
row.Number = rowNumber;
// Set IsHeaderRow to true, since the header row has to be in the top row
row.IsHeaderRow = (rowNumber == 1);
// Set the rowId
row.Id = Guid.NewGuid();
// now load the columns for this row
// if load specified columns is true and there are one or more columns specified
if ((loadWorksheetInfo.LoadColumnOptions == LoadColumnOptionsEnum.LoadSpecifiedColumns) && (ListHelper.HasOneOrMoreItems(loadWorksheetInfo.SpecifiedColumnNames)))
{
// load the specified columns
foreach (SpecifiedColumnName columnName in loadWorksheetInfo.SpecifiedColumnNames)
{
// if the Index is greater than zero
if (columnName.HasIndex)
{
// Set the index that was already looked up
columnIndex = columnName.Index;
}
else if (!columnName.NotFound)
{
// find the columnIndex
columnIndex = GetColumnIndex(excelWorksheet, columnName.Name);
// if the columnIndex was not found
if (columnIndex < 1)
{
// not found
columnName.NotFound = true;
}
}
// if the index was found
if (columnIndex > 0)
{
// increment the value
colNumber++;
// Create a new instance of a 'Column' object.
column = new Column();
// Set the value
column.RowNumber = rowNumber;
column.ColumnNumber = columnIndex;
// Get the ColumnValue
column.ColumnValue = GetCellValue(excelWorksheet, rowNumber, colNumber);
// Get the CellText
column.ColumnText = GetCellText(excelWorksheet, rowNumber, colNumber);
// Add this column
row.Columns.Add(column);
}
}
}
else
{
// iterate the columns up to colCount
for (int x = 1; x <= colCount; x++)
{
// Create a new instance of a 'Column' object.
column = new Column();
// Set the value
column.RowNumber = rowNumber;
column.ColumnNumber = x;
// Get the ColumnValue
column.ColumnValue = GetCellValue(excelWorksheet, rowNumber, x);
// Get the CellText
column.ColumnText = GetCellText(excelWorksheet, rowNumber, x);
// Add this column
row.Columns.Add(column);
}
}
// Add this row
worksheet.Rows.Add(row);
} while (rowNumber < rowCount);
}
}
}
catch (Exception error)
{
// Use this to attach logging or other centralized error handling
DebugHelper.WriteDebugError("LoadWorksheet", "ExcelDataLoader", error);
}
}
// return value
return worksheet;
}
#endregion
#region LoadWorksheet(string path, LoadWorksheetInfo loadWorksheetInfo)
/// <summary>
/// This method returns a single Worksheet
/// </summary>
public static Worksheet LoadWorksheet(string path, LoadWorksheetInfo loadWorksheetInfo)
{
// initial value
Worksheet worksheet = null;
// load the workbook
Workbook workbook = LoadWorkbook(path, loadWorksheetInfo);
// if the workbook
if ((NullHelper.Exists(workbook)) && (ListHelper.HasOneOrMoreItems(workbook.Worksheets)))
{
// set the return value
worksheet = workbook.Worksheets[0];
}
// return value
return worksheet;
}
#endregion
#endregion
}
#endregion
}