-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathProgram.cs
More file actions
229 lines (188 loc) · 6.74 KB
/
Copy pathProgram.cs
File metadata and controls
229 lines (188 loc) · 6.74 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
using ArrayToExcel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.IO;
using System.Linq;
namespace ConsoleApp;
class Program
{
static void Main()
{
Example1();
Example2();
Example3();
Example4();
Example5();
Example6();
TestTypes();
TestDictionary();
TestExpandoObject();
TestHashtable();
TestDataTable();
TestObject();
}
// different types + stream
static void TestTypes()
{
var items = Enumerable.Range(1, 100).Select(x => new
{
String = "1) text text text; \n2) text text text !!!",
WrapText = new CellText("1) text text text; \n2) text text text", true),
Uri = new Uri($"https://www.google.com/search?q={x}"),
Hyperlink = new CellHyperlink($"https://www.google.com/search?q={x}", $"link_{x}"),
Formula = new CellFormula(row => $"L{row}+O{row}"),
Percent = new CellPercent(1d / x),
DateOnly = DateOnly.FromDateTime(DateTime.Today.AddDays(-x)),
DateTime = DateTime.Now.AddDays(-x),
DateTimeOffset = DateTimeOffset.Now.AddDays(-x),
Bool = x % 2 == 0,
NullableBool = x % 2 == 0 ? (bool?)true : null,
Int = -x * 100,
Uint = (uint)x * 100,
Long = (long)x * 100,
Double = 1.1d + x,
Float = 1.1f + x,
Decimal = 1.1m + x,
});
using var file = File.Create($@"..\{nameof(TestTypes)}.xlsx");
items.ToExcel(file);
}
static readonly IEnumerable<SomeItem> SomeItems = Enumerable.Range(1, 10).Select(x => new SomeItem
{
Prop1 = $"Text #{x}",
Prop2 = x * 1000,
Prop3 = DateOnly.FromDateTime(DateTime.Now.AddDays(-x)),
});
// default settings
static void Example1()
{
var excel = SomeItems.ToExcel();
File.WriteAllBytes($@"..\..\..\..\{nameof(Example1)}.xlsx".ToLower(), excel);
}
// rename sheet and columns
static void Example2()
{
var excel = SomeItems.ToExcel(schema => schema
.SheetName("Example name")
.ColumnName(m => m.Name.Replace("Prop", "Column #")));
File.WriteAllBytes($@"..\..\..\..\{nameof(Example2)}.xlsx".ToLower(), excel);
}
// sort columns
static void Example3()
{
var excel = SomeItems.ToExcel(schema => schema
.ColumnSort(m => m.Name, desc: true));
File.WriteAllBytes($@"..\..\..\..\{nameof(Example3)}.xlsx".ToLower(), excel);
}
// custom column's mapping
static void Example4()
{
var excel = SomeItems.ToExcel(schema => schema
.AddColumn("MyColumnName#1", x => new CellHyperlink($"https://www.google.com/search?q={x.Prop1}", x.Prop1))
.AddColumn("MyColumnName#2", x => $"test:{x.Prop2}")
.AddColumn("MyColumnName#3", x => x.Prop3));
File.WriteAllBytes($@"..\..\..\..\{nameof(Example4)}.xlsx".ToLower(), excel);
}
// additional sheets
static void Example5()
{
var extraItems = Enumerable.Range(1, 10).Select(x => new
{
ExtraListProp1 = x,
ExtraListProp2 = x * 10,
ExtraListProp3 = x * 100,
ExtraListProp4 = x * 1000,
});
var excel = SomeItems.ToExcel(schema => schema
.SheetName("Main")
.AddSheet(extraItems));
File.WriteAllBytes($@"..\..\..\..\{nameof(Example5)}.xlsx".ToLower(), excel);
}
// DataSet
static void Example6()
{
var dataSet = new DataSet();
for (var i = 1; i <= 3; i++)
{
var table = new DataTable($"Table{i}");
dataSet.Tables.Add(table);
table.Columns.Add($"Column {i}-1", typeof(string));
table.Columns.Add($"Column {i}-2", typeof(int));
table.Columns.Add($"Column {i}-3", typeof(DateTime));
for (var x = 1; x <= 10 * i; x++)
table.Rows.Add($"Text #{x}", x * 1000, DateTime.Now.AddDays(-x));
}
var excel = dataSet.ToExcel();
File.WriteAllBytes($@"..\..\..\..\{nameof(Example6)}.xlsx".ToLower(), excel);
}
// list of dictionaries
static void TestDictionary()
{
var items = Enumerable.Range(1, 100).Select(x => new Dictionary<object, object>
{
{ "Column #1", $"Text #{x}" },
{ "Column #2", x * 1000 },
{ "Column #3", DateTime.Now.AddDays(-x) },
});
var excel = items.ToExcel(s => s
.AddSheet(items.Skip(10))); // extra sheet
File.WriteAllBytes($@"..\{nameof(TestDictionary)}.xlsx".ToLower(), excel);
}
// list of expandos
static void TestExpandoObject()
{
var items = Enumerable.Range(1, 100).Select(x =>
{
var item = new ExpandoObject();
var itemDict = item as IDictionary<string, object>;
itemDict.Add("Column #1", $"Text #{x}");
itemDict.Add("Column #2", x * 1000);
itemDict.Add("Column #3", DateTime.Now.AddDays(-x));
return item;
});
var excel = items.ToExcel(s => s
.AddSheet(items.Skip(10))); // extra sheet
File.WriteAllBytes($@"..\{nameof(TestExpandoObject)}.xlsx", excel);
}
// list of hashtables
static void TestHashtable()
{
var items = Enumerable.Range(1, 100).Select(x =>
{
var item = new Hashtable
{
{ "Column #1", $"Text #{x}" },
{ "Column #2", x * 1000 },
{ "Column #3", DateTime.Now.AddDays(-x) }
};
return item;
});
var excel = items.ToExcel(s => s
.AddSheet(items.Skip(10))); // extra sheet
File.WriteAllBytes($@"..\{nameof(TestHashtable)}.xlsx", excel);
}
// DataTable
static void TestDataTable()
{
var table = new DataTable("Table1");
table.Columns.Add("Column #1", typeof(string));
table.Columns.Add("Column #2", typeof(int));
table.Columns.Add("Column #3", typeof(DateTime));
for (var x = 1; x <= 100; x++)
table.Rows.Add($"Text #{x}", x * 1000, DateTime.Now.AddDays(-x));
var excel = table.ToExcel(s => s
.AddSheet(table, ss => ss.SheetName("Table2"))
.AddSheet(SomeItems));
File.WriteAllBytes($@"..\{nameof(TestDataTable)}.xlsx", excel);
}
// list of objects
static void TestObject()
{
var excel = SomeItems.AsEnumerable<object>().ToExcel(s => s
.AddSheet(SomeItems.AsEnumerable<object>().Skip(3))); // extra sheet
File.WriteAllBytes($@"..\{nameof(TestObject)}.xlsx", excel);
}
}