/
Excel and Random.cs
372 lines (308 loc) · 12.7 KB
/
Excel and Random.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
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
// Excel
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
using ClosedXML.Excel; // https://github.com/ClosedXML/ClosedXML/wiki
namespace PlayingAround
{
class Program
{
private readonly static string _root = @"C:\temp\C# Training";
private readonly static string _excel = Path.Join(_root, "Excel");
private readonly static string _firstWorkbook = Path.Join(_excel, "FirstWorkbook.xlsx");
private readonly static string _secondWorkbook = Path.Join(_excel, "SecondWorkbook.xlsx");
private readonly static Random _random = new Random();
static void Main(string[] args)
{
var program = new Program();
program.Setup();
program.WriteFirstWorkBook();
program.WriteSecondWorkBook();
program.ReadSecondWorkBook();
program.CleanUp();
}
public void WriteFirstWorkBook()
{
using (var workbook = new XLWorkbook())
{
var sheet = workbook.AddWorksheet("Foo");
var colA = sheet.Columns("A");
colA.Width = 50;
var cell = new CellPosition();
var headers = new string[] { "Identifier", "Firstname", "Surname", "Age", "Gender" };
foreach (var header in headers)
{
sheet.Cell(cell.Reference).Value = header;
cell.IncrementColumn();
}
cell.ResetColumn();
cell.IncrementRow();
var id = 0;
var rnd = new Random();
foreach (var _ in Enumerable.Range(1, 100))
{
sheet.Cell(cell.Reference).Value = ++id;
sheet.Cell(cell.IncrementColumn()).Value = id <= 50 ? "Foo" : "Bar";
sheet.Cell(cell.IncrementColumn()).Value = rnd.Next(0, 2) == 0 ? "Brown" : "Bloggs";
sheet.Cell(cell.IncrementColumn()).Value = rnd.Next(25, 61); ;
sheet.Cell(cell.IncrementColumn()).Value = "M";
cell.ResetColumn();
cell.IncrementRow();
}
sheet.RangeUsed().SetAutoFilter().Column(2).AddFilter("Foo"); // Filters "Firstname" to "Foo"
sheet.AutoFilter.Sort(4, XLSortOrder.Descending); // Sorts "Age" descending
workbook.SaveAs(_firstWorkbook); //using System.IO;
}
}
public void WriteSecondWorkBook()
{
using var workbook = new XLWorkbook();
var sheet = workbook.AddWorksheet("Values");
// Heading section
var cell = new CellPosition("B2");
sheet.Cell(cell.Reference).Value = "User ID:";
sheet.Cell(cell.IncrementColumn()).Value = "03459ABCIJ";
var idRange = sheet.Range(cell.SetCellPosition("B2") + ":" + cell.IncrementColumn());
idRange.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
cell.ResetColumn();
cell.IncrementRow(2);
// Data
var headers = new string[] { "Identifier", "Firstname", "Surname", "Date of Birth", "Nationality", "Foo", "Bar" };
foreach (var header in headers)
{
sheet.Cell(cell.Reference).Value = header;
cell.IncrementColumn();
}
cell.ResetColumn();
cell.IncrementRow();
var id = 0;
var nationalities = new string[] { "GB", "DE", "FR", "IR" };
var foos = new string[] { "f1", "f2", "f3" };
var dateStart = DateTime.Now.AddYears(-18);
var dateRange = new TimeSpan(365 * 10, 0, 0, 0, 0); // 10 years
foreach (var _ in Enumerable.Range(1, 100))
{
sheet.Cell(cell.Reference).Value = ++id;
sheet.Cell(cell.IncrementColumn()).Value = RandomHelper.GenerateName(RandomHelper.GenerateNumber(3, 7));
sheet.Cell(cell.IncrementColumn()).Value = RandomHelper.GenerateName(RandomHelper.GenerateNumber(4, 10));
sheet.Cell(cell.IncrementColumn()).Value = RandomHelper.GenerateDate(dateStart, dateRange, "B");
sheet.Cell(cell.Reference).Style.NumberFormat.Format = "dd/MM/yyyy";
sheet.Cell(cell.IncrementColumn()).Value = RandomHelper.GetString(nationalities);
sheet.Cell(cell.IncrementColumn()).Value = RandomHelper.GetString(foos);
sheet.Cell(cell.IncrementColumn()).Value = RandomHelper.GenerateNumber(length: 10);
sheet.Cell(cell.Reference).Style.NumberFormat.Format = new string('0', 10); // Prevents leading zeros being omitted
cell.ResetColumn();
cell.IncrementRow();
}
foreach (var header in headers)
{
sheet.Column(cell.Column).AdjustToContents();
cell.IncrementColumn();
}
workbook.SaveAs(_secondWorkbook); //using System.IO;
}
public void ReadSecondWorkBook()
{
var wb = new XLWorkbook(_secondWorkbook);
var ws = wb.Worksheet("Values");
var userId = string.Empty;
var row = ws.FirstRowUsed();
var cell = row.FirstCellUsed();
if (!cell.Value.ToString().Contains("user id", StringComparison.OrdinalIgnoreCase))
throw new FormatException("Could not find user id");
cell = cell.CellRight();
userId = cell.Value.ToString();
Console.WriteLine("Debug - User ID found at {0}, value {1}", cell.Address, userId);
if (userId.Trim().Length != 20)
throw new FormatException("User ID missing or incomplete");
// Find table
var searchCount = 0;
do
{
if (++searchCount >= 100)
throw new FormatException("Could not find table data");
row = row.RowBelow();
} while (row.IsEmpty());
if (!row.FirstCell().Value.ToString().Contains("identifier", StringComparison.OrdinalIgnoreCase))
throw new FormatException("Could not find table data");
var firstAddress = row.RowBelow().FirstCellUsed().Address;
var lastAddress = ws.LastCellUsed().Address;
Console.WriteLine("Debug - Data found at {0}:{1}", firstAddress, lastAddress);
var npiRange = ws.Range(row.FirstCell().Address, ws.LastCellUsed().Address);
foreach (var r in npiRange.Rows())
{
int i = 0;
Console.WriteLine("Identifier: {0}", r.Cell(++i).Value.ToString());
Console.WriteLine("Firstname: {0}", r.Cell(++i).Value.ToString());
Console.WriteLine("Surname: {0}", r.Cell(++i).Value.ToString());
Console.WriteLine("Date of Birth: {0}", r.Cell(++i).Value.ToString());
Console.WriteLine("Nationality: {0}", r.Cell(++i).Value.ToString());
Console.WriteLine("Identification Type: {0}", r.Cell(++i).Value.ToString());
Console.WriteLine("Identification Value: {0}", r.Cell(++i).Value.ToString());
Console.WriteLine();
}
}
public void Setup()
{
if (!Directory.Exists(_excel))
Directory.CreateDirectory(_excel);
}
public void CleanUp()
{
if (File.Exists(_firstWorkbook))
File.Delete(_firstWorkbook);
if (File.Exists(_secondWorkbook))
File.Delete(_secondWorkbook);
if (Directory.GetFiles(_excel).Length == 0)
Directory.Delete(_excel);
}
}
public class RandomHelper
{
private static readonly RandomHelper _instance = new RandomHelper();
private static readonly Random _random = new Random();
private static readonly string _allowedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public static RandomHelper GetInstance()
{
return _instance;
}
public static string GetString(string[] items)
{
return items[GenerateNumber(0, items.Length - 1)];
}
public static int GenerateNumber(int start, int end)
{
return _random.Next(start, end + 1);
}
public static string GenerateNumber(int length, bool canStartWithZero = true)
{
string output = GetNumber(canStartWithZero);
for (var i = 0; i < length - 1; i++)
output += GetNumber();
return output;
}
public static string GenerateName(int length)
{
var output = GetUpper();
for (var i = 0; i < length - 1; i++)
output += GetLower();
return output;
}
public static DateTime GenerateDate(DateTime start, TimeSpan range, string direction = "F") // Todo; move to enum
{
var days = (int)range.TotalDays;
return start.AddDays(_random.Next(direction.Equals("F") ? 0 : -days, direction.Equals("B") ? 0 : days));
}
private static string GetNumber(bool canBeZero = true)
{
if (canBeZero)
return (_allowedChars[0..10])[_random.Next(0, 10)].ToString();
return (_allowedChars[1..10])[_random.Next(0, 9)].ToString();
}
private static string GetUpper()
{
return (_allowedChars[10..36])[_random.Next(0, 26)].ToString();
}
private static string GetLower()
{
return (_allowedChars[36..62])[_random.Next(0, 26)].ToString();
}
}
public class CellPosition
{
public string Column { get; private set; }
public int Row { get; private set; }
public string Reference { get { return Column + Row; } }
private static readonly Regex _regex = new Regex("^([a-zA-Z]{1,3})([1-9][0-9]*)$", RegexOptions.IgnoreCase);
private static readonly string _maxColumn = "XFD";
public CellPosition(string position = "A1")
{
SetCellPosition(position);
}
public string SetCellPosition(string position = "A1")
{
var match = _regex.Match(position);
if (!match.Success)
throw new ArgumentOutOfRangeException();
// Todo; validate range
Column = match.Groups[1].Value.ToUpper();
Row = int.Parse(match.Groups[2].Value);
return Reference;
}
public string Increment(int columnTimes, int rowTimes)
{
IncrementColumn(columnTimes);
IncrementRow(rowTimes);
return Reference;
}
public string IncrementColumn(int times)
{
foreach (var _ in Enumerable.Range(1, times))
{
IncrementColumn();
}
return Reference;
}
public string IncrementColumn()
{
if (Column.Equals(_maxColumn))
throw new InvalidOperationException("Max column range reached: " + _maxColumn);
var chars = Column.ToCharArray();
if (!Column.EndsWith('Z'))
chars[^1] = ++chars[^1];
else
{
if (Column.Distinct().Count() == 1)
{
chars = new char[Column.Length + 1];
Array.Fill(chars, 'A');
}
else
{
var current = Column.Length - 1;
while (current > 0)
{
if (chars[current] != 'Z')
{
chars[current] = chars[current]--;
break;
}
chars[current] = 'A';
current--;
}
}
}
Column = new string(chars);
return Reference;
}
public string IncrementRow(int times)
{
if (times < 1)
throw new ArgumentException("Must be greated than zero");
Row += times;
return Reference;
}
public string IncrementRow()
{
Row++;
return Reference;
}
public string ResetColumn()
{
Column = "A";
return Reference;
}
public string ResetRow()
{
Row = 1;
return Reference;
}
public string ResetCell()
{
ResetColumn();
ResetRow();
return Reference;
}
}
}