-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
156 lines (121 loc) · 6.23 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
// Copyright (c) Federico Seckel.
// Licensed under the BSD 3-Clause License. See LICENSE file in the project root for full license information.
// See https://aka.ms/new-console-template for more information
using BigExcelCreator;
using BigExcelCreator.Styles;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
int attempts = 0;
string fullPath;
do
{
string path = Path.GetTempPath() + @"excelTest\";
Directory.CreateDirectory(path);
string name = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xlsx";
fullPath = Path.Combine(path, name);
} while (attempts++ < 10 && File.Exists(fullPath));
Console.WriteLine(fullPath);
var columns = new List<Column>
{
new Column{ CustomWidth=true, Width=15, Hidden = true },
new Column{ Width=15, Hidden = false },
new Column{ CustomWidth=true, Width=19},
new Column{ CustomWidth=true, Width=5, Hidden = true },
new Column{ Hidden = true },
new Column{ Hidden = false },
};
StyleList styleList = new();
Font italic = new(new Italic());
Font bold = new(new Bold());
Font boldItalic = new(new Bold(), new Italic());
styleList.NewStyle(italic, null, null, null, "italic default");
styleList.NewStyle(bold, null, null, null, "bold default");
styleList.NewStyle(boldItalic, null, null, null, "bold italic default");
Alignment center = new() { Horizontal = HorizontalAlignmentValues.Center };
styleList.NewStyle(italic, null, null, null, center, "italic center");
styleList.NewStyle(bold, null, null, null, center, "bold center");
styleList.NewStyle(boldItalic, null, null, null, center, "bold italic center");
Fill yellowFill = new Fill(new[]{
new PatternFill(new[]{
new ForegroundColor { Rgb = new HexBinaryValue { Value = "FFFF00" } } }
)
{ PatternType = PatternValues.Solid } });
styleList.NewStyle(null, yellowFill, null, null, "YELLOW");
styleList.NewDifferentialStyle("RED", font: new Font(new[] { new Color { Rgb = new HexBinaryValue { Value = "FF0000" } } }));
Fill greenFill = new Fill(new[]{
new PatternFill(new[]{
new BackgroundColor { Rgb = new HexBinaryValue { Value = "00FF00" } } })
{ PatternType = PatternValues.Solid } });
styleList.NewDifferentialStyle("GREENBKG", fill: greenFill);
using BigExcelWriter excel = new(fullPath, SpreadsheetDocumentType.Workbook, styleList.GetStylesheet());
excel.CreateAndOpenSheet("S1", columns: columns, sheetState: SheetStateValues.Visible);
excel.WriteTextRow(new List<string> { "A1", "B1", "C1", "D1", "E1" });
excel.WriteTextRow(new List<string> { "A2", "B2", "C2", "D2", "E2" });
excel.WriteTextRow(new List<string> { "A3", "B3", "C3", "D3", "E3" });
excel.Comment("test A1", "A1", "Me");
excel.Comment("test A3", "A3", "you");
excel.Comment("test B2", "B2");
excel.Comment("test E2", "E2", "unknown");
excel.CloseSheet();
excel.CreateAndOpenSheet("S2");
excel.WriteTextRow(new List<string> { "A1", "B1", "C1", "D1", "E1" });
excel.WriteTextRow(new List<string> { "A2", "B2", "C2", "D2", "E2" }, hidden: true);
excel.WriteTextRow(new List<string> { "A3", "B3", "C3", "D3", "E3" });
excel.WriteNumberRow(new List<float> { 548, 1872, 14663, 1145, 1146 });
excel.Comment("test A1 another sheet", "A1", "Me");
excel.Comment("test E3 another sheet", "E3", "you too");
excel.Comment("test B2 another sheet", "B2");
excel.BeginRow();
excel.WriteTextCell("Formulas:");
excel.WriteFormulaCell("SUM(A4:E4)");
excel.Comment("comment while writing row", "C4", "me");
excel.AddAutofilter("a1:e1");
excel.EndRow();
excel.PrintGridLinesInCurrentSheet = true;
excel.PrintRowAndColumnHeadingsInCurrentSheet = true;
excel.ShowGridLinesInCurrentSheet = false;
excel.CloseSheet();
excel.CreateAndOpenSheet("ReadMe example");
excel.BeginRow();
excel.WriteTextCell("Cell content");
excel.WriteNumberCell(123); // write as number. This allows to use formulas.
excel.WriteNumberCell(456);
excel.WriteFormulaCell("SUM(B1:C1)");
excel.EndRow();
excel.BeginRow(true);
excel.WriteTextCell("This row id hidden");
excel.EndRow();
excel.CloseSheet();
excel.CreateAndOpenSheet("format");
excel.BeginRow();
excel.WriteTextCell("this is in italic", styleList.GetIndexByName("italic default"));
excel.WriteTextCell("this is bold", styleList.GetIndexByName("bold default"));
excel.WriteTextCell("this is bold and italic", styleList.GetIndexByName("bold italic default"));
excel.EndRow();
excel.BeginRow();
excel.WriteTextCell("this is in italic (centered)", styleList.GetIndexByName("italic center"));
excel.WriteTextCell("this is bold (centered)", styleList.GetIndexByName("bold center"));
excel.WriteTextCell("this is bold and italic (centered)", styleList.GetIndexByName("bold italic center"));
excel.EndRow();
excel.CloseSheet();
excel.CreateAndOpenSheet("conditional");
for (int i = 0; i < 10; i++)
{
excel.WriteNumberRow(new List<float> { i }, styleList.GetIndexByName("YELLOW"));
excel.WriteNumberRow(new List<float> { i }, styleList.GetIndexByName("YELLOW"));
}
const string conditionalFormattingRange = "A1:A20";
excel.AddConditionalFormattingFormula(conditionalFormattingRange, "A1<5", styleList.GetIndexDifferentialByName("RED"));
excel.AddConditionalFormattingFormula(conditionalFormattingRange, "A1>5", styleList.GetIndexDifferentialByName("GREENBKG"));
excel.AddConditionalFormattingDuplicatedValues(conditionalFormattingRange, styleList.GetIndexDifferentialByName("RED"));
excel.AddConditionalFormattingCellIs(conditionalFormattingRange, ConditionalFormattingOperatorValues.LessThan, "5", styleList.GetIndexDifferentialByName("RED"));
excel.AddConditionalFormattingCellIs(conditionalFormattingRange, ConditionalFormattingOperatorValues.Between, "3", styleList.GetIndexDifferentialByName("RED"), "7");
excel.AddIntegerValidator("B1:B10", 2, DataValidationOperatorValues.Between, secondOperand: 8);
excel.AddDecimalValidator("C1:C10", 0, DataValidationOperatorValues.Between, secondOperand: 1);
excel.CloseSheet();
excel.CreateAndOpenSheet("merged cells");
excel.MergeCells("1");
excel.MergeCells("a2:c2");
excel.MergeCells("a3:a5");
excel.MergeCells("c3:d5");
excel.CloseSheet();