This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathProgram.cs
239 lines (201 loc) · 9.61 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Simplexcel.TestApp
{
class Program
{
static void Main(string[] args)
{
var wb = new Workbook();
wb.Title = "Workbook Title";
wb.Author = "The Author";
var sheet = new Worksheet("Test");
sheet.PageSetup.Orientation = Orientation.Landscape;
sheet.PageSetup.PrintRepeatRows = 2;
sheet.PageSetup.PrintRepeatColumns = 2;
sheet.ColumnWidths[0] = 24.6;
sheet.Cells["A1"] = "Test";
sheet.Cells["A1"].FontName = "Arial Black";
sheet.Cells[0, 1] = "Another Test";
sheet.Cells[0, 1].Border = CellBorder.Bottom | CellBorder.Right;
sheet.Cells[0, 2] = "Bold Red";
sheet.Cells[0, 2].Bold = true;
sheet.Cells[0, 2].TextColor = Color.Red;
Cell cell = "BIU & Big & Blue";
cell.Bold = true;
cell.Underline = true;
cell.Italic = true;
cell.TextColor = Color.Blue;
cell.FontSize = 18;
cell.Hyperlink = "https://github.com/mstum/Simplexcel";
sheet.Cells[0, 3] = cell;
sheet.ColumnWidths[3] = 40;
sheet.Cells[0, 4] = 13;
sheet.Cells[0, 5] = 13.58;
for (int i = 1; i < 55; i++)
{
sheet.Cells[1, i] = Guid.NewGuid().ToString("N");
sheet.Cells[i, 0] = Guid.NewGuid().ToString("N");
}
Cell cell2 = "Orange";
cell2.Bold = true;
cell2.Italic = true;
cell2.TextColor = Color.Orange;
cell2.FontSize = 18;
sheet.Cells[0, 2] = cell2;
// Note that Emoji only work in Excel 2013 or newer, not 2007 or 2010. This is an Excel limitation.
sheet.Cells[0, 6] = "👪";
sheet.Cells[0, 7] = "👨👩👧👦";
sheet.Cells["D4"] = DateTime.Now;
sheet.Cells["D5"] = new Cell(CellType.Date, DateTime.Now, BuiltInCellFormat.DateOnly);
sheet.Cells["D6"] = new Cell(CellType.Date, DateTime.Now, BuiltInCellFormat.TimeOnly);
sheet.Cells["D7"] = long.MaxValue;
sheet.Cells["D8"] = long.MinValue;
sheet.Cells["D9"] = decimal.MaxValue;
sheet.Cells["D10"] = decimal.MinValue;
sheet.Cells["D11"] = 9999999999L;
sheet.Cells["D12"] = 99999999999L;
sheet.Cells["D13"] = 100000000000L;
sheet.Cells["D14"] = 100000000001L;
sheet.Cells["D15"] = 1000000000000L;
sheet.Cells["D16"] = 1000000000001L;
sheet.Cells["D17"] = Cell.LargeNumberPositiveLimit;
sheet.Cells["D18"] = Cell.LargeNumberPositiveLimit + 1;
sheet.Cells["D19"] = Cell.LargeNumberPositiveLimit - 1;
sheet.Cells["D20"] = -9999999999L;
sheet.Cells["D21"] = -99999999999L;
sheet.Cells["D22"] = -100000000000L;
sheet.Cells["D23"] = -100000000001L;
sheet.Cells["D24"] = -1000000000000L;
sheet.Cells["D25"] = -1000000000001L;
sheet.Cells["D26"] = Cell.LargeNumberNegativeLimit;
sheet.Cells["D27"] = Cell.LargeNumberNegativeLimit + 1;
sheet.Cells["D28"] = Cell.LargeNumberNegativeLimit - 1;
sheet.LargeNumberHandlingMode = LargeNumberHandlingMode.StoreAsText;
sheet.Cells["C5"] = "ThinDiagonalCrosshatch";
sheet.Cells["C5"].Fill = new PatternFill { PatternType = PatternType.ThinDiagonalCrosshatch, PatternColor = Color.Yellow, BackgroundColor = Color.Violet };
sheet.Cells["C5"].TextColor = Color.Green;
sheet.Cells["C6"] = "ThickDiagonalCrosshatch";
sheet.Cells["C6"].Fill = new PatternFill { PatternType = PatternType.ThickDiagonalCrosshatch, PatternColor = Color.Violet };
sheet.Cells["C6"].TextColor = Color.Green;
sheet.Cells["C7"] = "DiagonalCrosshatch";
sheet.Cells["C7"].Fill = new PatternFill { PatternType = PatternType.DiagonalCrosshatch, BackgroundColor = Color.Violet };
sheet.Cells["C7"].TextColor = Color.Green;
sheet.Cells["C8"] = "FillSolid";
sheet.Cells["C8"].Fill = new PatternFill { PatternType = PatternType.Solid, PatternColor = Color.Yellow, BackgroundColor = Color.Violet };
sheet.Cells["C8"].TextColor = Color.Green;
sheet.Cells["C9"] = "SetFillBgColor";
sheet.Cells["C9"].Fill.BackgroundColor = Color.Navy;
sheet.Cells["C9"].TextColor = Color.Green;
sheet.Cells["F5"] = "Hello,";
sheet.Cells["G5"] = "World!";
sheet.Cells["F5"].Fill.BackgroundColor = Color.Blue;
sheet.Cells["G5"].Fill.BackgroundColor = Color.Red;
wb.Add(sheet);
// Prime the Cache...
var populatedSheet = new Worksheet("Populate");
populatedSheet.Populate(EnumeratePopulateTestData(), cacheTypeColumns: true);
// ...and use the cache
populatedSheet = new Worksheet("Populate");
populatedSheet.Populate(EnumeratePopulateTestData(), cacheTypeColumns: true);
wb.Add(populatedSheet);
var frozenTopRowSheet = new Worksheet("Frozen Top Row");
frozenTopRowSheet.Cells[0, 0] = "Header 1";
frozenTopRowSheet.Cells[0, 1] = "Header 2";
frozenTopRowSheet.Cells[0, 2] = "Header 3";
foreach (var i in Enumerable.Range(1, 100))
{
frozenTopRowSheet.Cells[i, 0] = "Value 1-" + i;
frozenTopRowSheet.Cells[i, 1] = "Value 2-" + i;
frozenTopRowSheet.Cells[i, 2] = "Value 3-" + i;
}
frozenTopRowSheet.FreezeTopRow();
wb.Add(frozenTopRowSheet);
var frozenLeftColumnSheet = new Worksheet("Frozen Left Column");
frozenLeftColumnSheet.Cells[0, 0] = "Header 1";
frozenLeftColumnSheet.Cells[1, 0] = "Header 2";
frozenLeftColumnSheet.Cells[2, 0] = "Header 3";
foreach (var i in Enumerable.Range(1, 100))
{
frozenLeftColumnSheet.Cells[0, i] = "Value 1-" + i;
frozenLeftColumnSheet.Cells[1, i] = "Value 2-" + i;
frozenLeftColumnSheet.Cells[2, i] = "Value 3-" + i;
}
frozenLeftColumnSheet.FreezeLeftColumn();
wb.Add(frozenLeftColumnSheet);
var pageBreakSheet = new Worksheet("Page Breaks");
for (var row = 0; row < 20; row++)
{
for (var col = 0; col < 20; col++)
{
pageBreakSheet.Cells[row, col] = $"Row {row} /Col {col}";
}
}
pageBreakSheet.InsertManualPageBreakAfterRow("B1");
pageBreakSheet.InsertManualPageBreakAfterRow(5);
pageBreakSheet.InsertManualPageBreakAfterColumn("B8");
pageBreakSheet.InsertManualPageBreakAfterColumn(16);
wb.Add(pageBreakSheet);
var formulaSheet = new Worksheet("Formula");
formulaSheet.Cells["A1"] = 5;
formulaSheet.Cells["A2"] = 10;
formulaSheet.Cells["A3"] = 15;
formulaSheet.Cells["B1"] = Cell.Formula("SUM(A:A)");
formulaSheet.Cells["B2"] = Cell.Formula("SUM(A1:A2)");
formulaSheet.Cells["B3"] = Cell.Formula("AVERAGE(A:A)");
formulaSheet.Cells["B4"] = Cell.Formula("MEDIAN(A:A)");
formulaSheet.Cells["C1"] = Cell.Formula("A1+B1");
formulaSheet.Cells["C2"] = Cell.Formula("=SUM(B:B)");
formulaSheet.Cells["C3"] = Cell.Formula("C5+A1");
formulaSheet.Cells["C5"] = Cell.Formula("SUM(B:B)+C1");
wb.Add(formulaSheet);
var freezeTopLeftSheet = new Worksheet("FreezeTopLeft");
for (var row = 0; row < 10; row++)
for (var col = 0; col < 10; col++)
{
freezeTopLeftSheet[row, col] = $"{row},{col}";
}
freezeTopLeftSheet.FreezeTopLeft(2, 2);
wb.Add(freezeTopLeftSheet);
wb.Save("compressed.xlsx", compress: true);
wb.Save("uncompressed.xlsx", compress: false);
}
private static IEnumerable<PopulateTestData> EnumeratePopulateTestData()
{
var r = new Random();
for (int i = 0; i < 500; i++)
{
yield return new PopulateTestData
{
Id = i,
CreatedUtc = new DateTime(2015, 1, 1, 1, 1, 1).AddDays(r.Next(1, 852)),
Name = "Item Number " + i,
Value = int.MaxValue - i,
Price = r.Next(800, 9400) * 0.52m,
Quantity = r.Next(4, 75) * 0.5m,
IgnoreMe = Guid.NewGuid().ToString()
};
}
}
private abstract class PopulateTestDataBase
{
public int Id { get; set; }
[XlsxColumn("Unit Price")]
public decimal Price { get; set; }
[XlsxColumn(null)]
public decimal Quantity { get; set; }
}
private class PopulateTestData : PopulateTestDataBase
{
public string Name { get; set; }
[XlsxColumn("")]
public long Value { get; set; }
[XlsxColumn("Total")]
public decimal TotalPrice { get { return Price * Quantity; } }
public DateTime CreatedUtc { get; set; }
[XlsxIgnoreColumn]
public string IgnoreMe { get; set; }
}
}
}