NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
BasicStyles.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2025
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
10using static NanoXLSX.Styles.Border;
11using static NanoXLSX.Styles.Fill;
12using static NanoXLSX.Styles.Font;
13using static NanoXLSX.Styles.NumberFormat;
14
15namespace NanoXLSX.Styles
16{
20 public static class BasicStyles
21 {
22 #region enums
26 private enum StyleEnum
27 {
29 bold,
31 italic,
33 boldItalic,
35 underline,
37 doubleUnderline,
39 strike,
41 dateFormat,
43 timeFormat,
45 roundFormat,
47 borderFrame,
49 borderFrameHeader,
51 dottedFill_0_125,
53 mergeCellStyle
54 }
55 #endregion
56
57 #region staticFields
58 private static Style bold;
59 private static Style italic;
60 private static Style boldItalic;
61 private static Style underline;
62 private static Style doubleUnderline;
63 private static Style strike;
64 private static Style dateFormat;
65 private static Style timeFormat;
66 private static Style roundFormat;
67 private static Style borderFrame;
68 private static Style borderFrameHeader;
69 private static Style dottedFill_0_125;
70 private static Style mergeCellStyle;
71 #endregion
72
73 #region staticProperties
75 public static Style Bold
76 { get { return GetStyle(StyleEnum.bold); } }
77
78 public static Style BoldItalic
79 { get { return GetStyle(StyleEnum.boldItalic); } }
80
81 public static Style BorderFrame
82 { get { return GetStyle(StyleEnum.borderFrame); } }
83
85 { get { return GetStyle(StyleEnum.borderFrameHeader); } }
86
87 public static Style DateFormat
88 { get { return GetStyle(StyleEnum.dateFormat); } }
89
90 public static Style TimeFormat
91 { get { return GetStyle(StyleEnum.timeFormat); } }
92
93 public static Style DoubleUnderline
94 { get { return GetStyle(StyleEnum.doubleUnderline); } }
95
96#pragma warning disable CA1707 // Suppress: Identifiers should not contain underscores
97 public static Style DottedFill_0_125
98#pragma warning restore CA1707
99 { get { return GetStyle(StyleEnum.dottedFill_0_125); } }
100
101 public static Style Italic
102 { get { return GetStyle(StyleEnum.italic); } }
103
104 public static Style MergeCellStyle
105 { get { return GetStyle(StyleEnum.mergeCellStyle); } }
106
107 public static Style RoundFormat
108 { get { return GetStyle(StyleEnum.roundFormat); } }
109
110 public static Style Strike
111 { get { return GetStyle(StyleEnum.strike); } }
112
113 public static Style Underline
114 { get { return GetStyle(StyleEnum.underline); } }
115 #endregion
116
117 #region staticMethods
123 private static Style GetStyle(StyleEnum value)
124 {
125 Style s = null;
126 switch (value)
127 {
128 case StyleEnum.bold:
129 if (bold == null)
130 {
131 bold = new Style();
132 bold.CurrentFont.Bold = true;
133 }
134 s = bold;
135 break;
136 case StyleEnum.italic:
137 if (italic == null)
138 {
139 italic = new Style();
140 italic.CurrentFont.Italic = true;
141 }
142 s = italic;
143 break;
144 case StyleEnum.boldItalic:
145 if (boldItalic == null)
146 {
147 boldItalic = new Style();
148 boldItalic.CurrentFont.Italic = true;
149 boldItalic.CurrentFont.Bold = true;
150 }
151 s = boldItalic;
152 break;
153 case StyleEnum.underline:
154 if (underline == null)
155 {
156 underline = new Style();
157 underline.CurrentFont.Underline = UnderlineValue.Single;
158 }
159 s = underline;
160 break;
161 case StyleEnum.doubleUnderline:
162 if (doubleUnderline == null)
163 {
164 doubleUnderline = new Style();
165 doubleUnderline.CurrentFont.Underline = UnderlineValue.Double;
166 }
167 s = doubleUnderline;
168 break;
169 case StyleEnum.strike:
170 if (strike == null)
171 {
172 strike = new Style();
173 strike.CurrentFont.Strike = true;
174 }
175 s = strike;
176 break;
177 case StyleEnum.dateFormat:
178 if (dateFormat == null)
179 {
180 dateFormat = new Style();
181 dateFormat.CurrentNumberFormat.Number = FormatNumber.Format14;
182 }
183 s = dateFormat;
184 break;
185 case StyleEnum.timeFormat:
186 if (timeFormat == null)
187 {
188 timeFormat = new Style();
189 timeFormat.CurrentNumberFormat.Number = FormatNumber.Format21;
190 }
191 s = timeFormat;
192 break;
193 case StyleEnum.roundFormat:
194 if (roundFormat == null)
195 {
196 roundFormat = new Style();
197 roundFormat.CurrentNumberFormat.Number = FormatNumber.Format1;
198 }
199 s = roundFormat;
200 break;
201 case StyleEnum.borderFrame:
202 if (borderFrame == null)
203 {
204 borderFrame = new Style();
205 borderFrame.CurrentBorder.TopStyle = StyleValue.Thin;
206 borderFrame.CurrentBorder.BottomStyle = StyleValue.Thin;
207 borderFrame.CurrentBorder.LeftStyle = StyleValue.Thin;
208 borderFrame.CurrentBorder.RightStyle = StyleValue.Thin;
209 }
210 s = borderFrame;
211 break;
212 case StyleEnum.borderFrameHeader:
213 if (borderFrameHeader == null)
214 {
215 borderFrameHeader = new Style();
216 borderFrameHeader.CurrentBorder.TopStyle = StyleValue.Thin;
217 borderFrameHeader.CurrentBorder.BottomStyle = StyleValue.Medium;
218 borderFrameHeader.CurrentBorder.LeftStyle = StyleValue.Thin;
219 borderFrameHeader.CurrentBorder.RightStyle = StyleValue.Thin;
220 borderFrameHeader.CurrentFont.Bold = true;
221 }
222 s = borderFrameHeader;
223 break;
224 case StyleEnum.dottedFill_0_125:
225 if (dottedFill_0_125 == null)
226 {
227 dottedFill_0_125 = new Style();
228 dottedFill_0_125.CurrentFill.PatternFill = PatternValue.Gray125;
229 }
230 s = dottedFill_0_125;
231 break;
232 case StyleEnum.mergeCellStyle:
233 if (mergeCellStyle == null)
234 {
235 mergeCellStyle = new Style();
236 mergeCellStyle.CurrentCellXf.ForceApplyAlignment = true;
237 }
238 s = mergeCellStyle;
239 break;
240 }
241 return s.CopyStyle(); // Copy makes basic styles immutable
242 }
243
250 public static Style ColorizedText(string rgb)
251 {
252 Validators.ValidateColor(rgb, false);
253 Style s = new Style();
254 s.CurrentFont.ColorValue = ParserUtils.ToUpper("FF" + rgb);
255 return s;
256 }
257
264 public static Style ColorizedBackground(string rgb)
265 {
266 Validators.ValidateColor(rgb, false);
267 Style s = new Style();
268 s.CurrentFill.SetColor(ParserUtils.ToUpper("FF" + rgb), FillType.FillColor);
269
270 return s;
271 }
272
283 public static Style Font(string fontName, float fontSize = 11f, bool isBold = false, bool isItalic = false)
284 {
285 Style s = new Style();
286 s.CurrentFont.Name = fontName;
287 s.CurrentFont.Size = fontSize;
288 s.CurrentFont.Bold = isBold;
289 s.CurrentFont.Italic = isItalic;
290 return s;
291 }
292 #endregion
293 }
294}
Factory class with the most important predefined styles.
static Style ColorizedText(string rgb)
Gets a style to colorize the text of a cell.
static Style Strike
Gets the strike style.
static Style TimeFormat
Gets the time format style.
static Style DateFormat
Gets the date format style.
static Style RoundFormat
Gets the round format style.
static Style Italic
Gets the italic style.
static Style DoubleUnderline
Gets the double underline style.
static Style Underline
Gets the underline style.
static Style MergeCellStyle
Gets the style used when merging cells.
static Style DottedFill_0_125
Gets the special pattern fill style (for compatibility).
static Style Font(string fontName, float fontSize=11f, bool isBold=false, bool isItalic=false)
Gets a style with a user defined font.
static Style BorderFrame
Gets the border frame style.
static Style ColorizedBackground(string rgb)
Gets a style to colorize the background of a cell.
static Style Bold
Gets the bold style.
static Style BorderFrameHeader
Gets the border style for header cells.
static Style BoldItalic
Gets the bold and italic style.
Class representing a Border entry. The Border entry is used to define frames and cell borders.
Definition Border.cs:18
Class representing a Fill (background) entry. The Fill entry is used to define background colors and ...
Definition Fill.cs:18
void SetColor(string value, FillType fillType)
Sets the color and the depending on fill type.
Definition Fill.cs:255
Class representing a Font entry. The Font entry is used to define text formatting.
Definition Font.cs:20
Class representing a NumberFormat entry. The NumberFormat entry is used to define cell formats like c...
Class representing a Style with sub classes within a style sheet. An instance of this class is only a...
Definition Style.cs:18
Style CopyStyle()
Method to copy the current object to a new one with casting.
Definition Style.cs:227
Fill CurrentFill
Gets or sets the current Fill object of the style.
Definition Style.cs:38
Class providing static methods to parse string values to specific types or to print object as languag...
static string ToUpper(string input)
Transforms a string to upper case with null check and invariant culture.
Class providing general validator methods.
Definition Validators.cs:11
static void ValidateColor(string hexCode, bool useAlpha, bool allowEmpty=false)
Validates the passed string, whether it is a valid RGB or ARGB value that can be used for Fills or Fo...
Definition Validators.cs:25