-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcelHelper.cs
More file actions
217 lines (185 loc) · 8.4 KB
/
ExcelHelper.cs
File metadata and controls
217 lines (185 loc) · 8.4 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
#region using statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OfficeOpenXml;
using DataJuggler.UltimateHelper;
#endregion
namespace DataJuggler.Excelerate
{
#region class ExcelHelper
/// <summary>
/// This class is used to help with common Excel tasks, with the main one being Saving data to Excel.
/// </summary>
public class ExcelHelper
{
#region Methods
#region GetColumnLetter(int column)
/// <summary>
/// returns the Column Letter for the column index (1 = A, 2 = B, 27 = AA, 78 = "ZZZ" I think)
/// </summary>
public static string GetColumnLetter(int column)
{
// initial value
string columnLetter = ExcelCellAddress.GetColumnLetter(column);
// return value
return columnLetter;
}
#endregion
#region SaveBatch(string path, Batch batch)
/// <summary>
/// returns the Batch
/// </summary>
public static bool SaveBatch(string path, Batch batch)
{
// initial value
bool saved = false;
try
{
// if the batch exists and the batch has BatchItems (represents a Worksheet) and the path to the Excel file exists on disk
if ((NullHelper.Exists(batch)) && (batch.HasBatchItems) && (FileHelper.Exists(path)))
{
// load the package
ExcelPackage package = ExcelDataLoader.LoadExcelPackage(path);
// iterate the batchItems
foreach (BatchItem batchItem in batch.BatchItems)
{
// if the batchItem has Updates and a WorksheetInfo
if ((batchItem.HasUpdates) && (batchItem.HasWorksheetInfo))
{
// Get the sheet
ExcelWorksheet excelworksheet = package.Workbook.Worksheets.FirstOrDefault(x => x.Name == batchItem.WorksheetInfo.SheetName);
// If the excelworksheet object exists
if (NullHelper.Exists(excelworksheet))
{
// iterate the rows to update
foreach (Row row in batchItem.Updates)
{
// If the value for the property row.HasColumns is true
if (row.HasColumns)
{
// iterate the rows
foreach (Column column in row.Columns)
{
// Set the value
excelworksheet.SetValue(column.RowNumber, column.ColumnNumber, column.ColumnValue);
}
}
}
}
}
}
// Save the package
package.Save();
// all is good
saved = true;
}
}
catch (Exception error)
{
// for debugging only for now
DebugHelper.WriteDebugError("SaveBatch", "ExcelHelper.cs", error);
}
// return value
return saved;
}
#endregion
#region SaveBatchItem(string path, BatchItem batchItem)
/// <summary>
/// Save and then returns the batchItem
/// </summary>
public static bool SaveBatchItem(string path, BatchItem batchItem)
{
// initial value
bool saved = false;
try
{
// if the batchItem exists and the batchItem has Updates (rows to update) and the path to the Excel file exists on disk
if ((NullHelper.Exists(batchItem)) && (batchItem.HasUpdates) && (batchItem.HasWorksheetInfo) && (FileHelper.Exists(path)))
{
// load the package
ExcelPackage package = ExcelDataLoader.LoadExcelPackage(path);
// Get the sheet
ExcelWorksheet excelworksheet = package.Workbook.Worksheets.FirstOrDefault(x => x.Name == batchItem.WorksheetInfo.SheetName);
// If the excelworksheet object exists
if (NullHelper.Exists(excelworksheet))
{
// iterate the rows to update
foreach (Row row in batchItem.Updates)
{
// If the value for the property row.HasColumns is true
if (row.HasColumns)
{
// iterate the rows
foreach (Column column in row.Columns)
{
// Set the value
excelworksheet.SetValue(column.RowNumber, column.ColumnNumber, column.ColumnValue);
}
}
}
}
// Save the package
package.Save();
// all is good
saved = true;
}
}
catch (Exception error)
{
// for debugging only for now
DebugHelper.WriteDebugError("SaveBatchItem", "ExcelHelper.cs", error);
}
// return value
return saved;
}
#endregion
#region SaveRow(string path, Row row, Worksheet worksheet)
/// <summary>
/// returns the Row
/// </summary>
public static bool SaveRow(string path, Row row, Worksheet worksheet)
{
// initial value
bool saved = false;
try
{
// if the worksheet exists and the worksheet.WorksheetInfo exists and the path to the worksheet exists
if ((NullHelper.Exists(worksheet, row)) && (worksheet.HasWorksheetInfo) && (FileHelper.Exists(path)) && (row.HasColumns))
{
// load the package
ExcelPackage package = ExcelDataLoader.LoadExcelPackage(path);
// Get the sheet
ExcelWorksheet excelworksheet = package.Workbook.Worksheets.FirstOrDefault(x => x.Name == worksheet.Name);
// If the excelworksheet object exists
if (NullHelper.Exists(excelworksheet))
{
// iterate the rows
foreach (Column column in row.Columns)
{
// Set the value
excelworksheet.SetValue(column.RowNumber, column.ColumnNumber, column.ColumnValue);
}
}
// Save the package
package.Save();
// all is good
saved = true;
}
}
catch (Exception error)
{
// for debugging only for now
DebugHelper.WriteDebugError("SaveRow", "ExcelHelper.cs", error);
}
// return value
return saved;
}
#endregion
#endregion
}
#endregion
}