NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
Style.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
8using System.Text;
10using NanoXLSX.Utils;
11
12namespace NanoXLSX.Styles
13{
17 public class Style : AbstractStyle
18 {
19 #region privateFields
20 private bool internalStyle;
21 #endregion
22
23 #region properties
27 [Append(NestedProperty = true)]
28 public Border CurrentBorder { get; set; }
32 [Append(NestedProperty = true)]
33 public CellXf CurrentCellXf { get; set; }
37 [Append(NestedProperty = true)]
38 public Fill CurrentFill { get; set; }
42 [Append(NestedProperty = true)]
43 public Font CurrentFont { get; set; }
47 [Append(NestedProperty = true)]
48 public NumberFormat CurrentNumberFormat { get; set; }
53 [Append(Ignore = true)]
54 public string Name { get; set; }
55
59 [Append(Ignore = true)]
60 public bool IsInternalStyle
61 {
62 get { return internalStyle; }
63 }
64
65 #endregion
66
67 #region constructors
71 public Style()
72 {
73 CurrentBorder = new Border();
74 CurrentCellXf = new CellXf();
75 CurrentFill = new Fill();
76 CurrentFont = new Font();
79 }
80
85 public Style(string name)
86 {
87 CurrentBorder = new Border();
88 CurrentCellXf = new CellXf();
89 CurrentFill = new Fill();
90 CurrentFont = new Font();
92 this.Name = name;
93 }
94
101 public Style(string name, int forcedOrder, bool internalStyle)
102 {
103 CurrentBorder = new Border();
104 CurrentCellXf = new CellXf();
105 CurrentFill = new Fill();
106 CurrentFont = new Font();
108 this.Name = name;
109 InternalID = forcedOrder;
110 this.internalStyle = internalStyle;
111 }
112 #endregion
113
114 #region methods
115
121 public Style Append(AbstractStyle styleToAppend)
122 {
123 if (styleToAppend == null)
124 {
125 return this;
126 }
127 if (styleToAppend.GetType() == typeof(Border))
128 {
129 CurrentBorder.CopyProperties<Border>((Border)styleToAppend, new Border());
130 }
131 else if (styleToAppend.GetType() == typeof(CellXf))
132 {
133 CurrentCellXf.CopyProperties<CellXf>((CellXf)styleToAppend, new CellXf());
134 }
135 else if (styleToAppend.GetType() == typeof(Fill))
136 {
137 CurrentFill.CopyProperties<Fill>((Fill)styleToAppend, new Fill());
138 }
139 else if (styleToAppend.GetType() == typeof(Font))
140 {
141 CurrentFont.CopyProperties<Font>((Font)styleToAppend, new Font());
142 }
143 else if (styleToAppend.GetType() == typeof(NumberFormat))
144 {
145 CurrentNumberFormat.CopyProperties<NumberFormat>((NumberFormat)styleToAppend, new NumberFormat());
146 }
147 else if (styleToAppend.GetType() == typeof(Style))
148 {
149 CurrentBorder.CopyProperties<Border>(((Style)styleToAppend).CurrentBorder, new Border());
150 CurrentCellXf.CopyProperties<CellXf>(((Style)styleToAppend).CurrentCellXf, new CellXf());
151 CurrentFill.CopyProperties<Fill>(((Style)styleToAppend).CurrentFill, new Fill());
152 CurrentFont.CopyProperties<Font>(((Style)styleToAppend).CurrentFont, new Font());
153 CurrentNumberFormat.CopyProperties<NumberFormat>(((Style)styleToAppend).CurrentNumberFormat, new NumberFormat());
154 }
155 return this;
156 }
157
162 public override string ToString()
163 {
164 StringBuilder sb = new StringBuilder();
165 sb.Append("{\n\"Style\": {\n");
166 AddPropertyAsJson(sb, "Name", Name);
167 AddPropertyAsJson(sb, "HashCode", this.GetHashCode());
168 sb.Append(CurrentBorder.ToString()).Append(",\n");
169 sb.Append(CurrentCellXf.ToString()).Append(",\n");
170 sb.Append(CurrentFill.ToString()).Append(",\n");
171 sb.Append(CurrentFont.ToString()).Append(",\n");
172 sb.Append(CurrentNumberFormat.ToString()).Append("\n}\n}");
173 return sb.ToString();
174 }
175
183 public override int GetHashCode()
184 {
185 if (CurrentBorder == null || CurrentCellXf == null || CurrentFill == null || CurrentFont == null || CurrentNumberFormat == null)
186 {
187 throw new StyleException("The hash of the style could not be created because one or more components are missing as references");
188 }
189 unchecked
190 {
191 int p = 241;
192 int r = 1;
193 r *= p + this.CurrentBorder.GetHashCode();
194 r *= p + this.CurrentCellXf.GetHashCode();
195 r *= p + this.CurrentFill.GetHashCode();
196 r *= p + this.CurrentFont.GetHashCode();
197 r *= p + this.CurrentNumberFormat.GetHashCode();
198 return r;
199 }
200 }
201
206 public override AbstractStyle Copy()
207 {
208 if (CurrentBorder == null || CurrentCellXf == null || CurrentFill == null || CurrentFont == null || CurrentNumberFormat == null)
209 {
210 throw new StyleException("The style could not be copied because one or more components are missing as references");
211 }
212 Style copy = new Style
213 {
214 CurrentBorder = CurrentBorder.CopyBorder(),
215 CurrentCellXf = CurrentCellXf.CopyCellXf(),
216 CurrentFill = CurrentFill.CopyFill(),
217 CurrentFont = CurrentFont.CopyFont(),
218 CurrentNumberFormat = CurrentNumberFormat.CopyNumberFormat()
219 };
220 return copy;
221 }
222
228 {
229 return (Style)Copy();
230 }
231 #endregion
232
233 }
234}
Class for exceptions regarding Style incidents.
Class represents an abstract style component.
int? InternalID
Gets or sets the internal ID for sorting purpose in the Excel style document (nullable).
Class representing a Border entry. The Border entry is used to define frames and cell borders.
Definition Border.cs:18
override int GetHashCode()
Returns a hash code for this instance.
Definition Border.cs:246
Class representing an XF entry. The XF entry is used to make reference to other style instances like ...
Definition CellXf.cs:19
override int GetHashCode()
Returns a hash code for this instance.
Definition CellXf.cs:270
Class representing a Fill (background) entry. The Fill entry is used to define background colors and ...
Definition Fill.cs:18
override int GetHashCode()
Returns a hash code for this instance.
Definition Fill.cs:214
Class representing a Font entry. The Font entry is used to define text formatting.
Definition Font.cs:20
override int GetHashCode()
Returns a hash code for this instance.
Definition Font.cs:512
Class representing a NumberFormat entry. The NumberFormat entry is used to define cell formats like c...
override int GetHashCode()
Returns a hash code for this instance.
override AbstractStyle Copy()
Method to copy the current object to a new one without casting.
Definition Style.cs:206
override string ToString()
Override toString method.
Definition Style.cs:162
override int GetHashCode()
Returns a hash code for this instance.
Definition Style.cs:183
string Name
Gets or sets the name of the informal style. If not defined, the automatically calculated hash will b...
Definition Style.cs:54
Style CopyStyle()
Method to copy the current object to a new one with casting.
Definition Style.cs:227
CellXf CurrentCellXf
Gets or sets the current CellXf object of the style.
Definition Style.cs:33
Style Append(AbstractStyle styleToAppend)
Appends the specified style parts to the current one. The parts can be instances of sub-classes like ...
Definition Style.cs:121
Style()
Default constructor.
Definition Style.cs:71
Border CurrentBorder
Gets or sets the current Border object of the style.
Definition Style.cs:28
Fill CurrentFill
Gets or sets the current Fill object of the style.
Definition Style.cs:38
Style(string name)
Constructor with parameters.
Definition Style.cs:85
bool IsInternalStyle
Gets whether the style is system internal. Such styles are not meant to be altered.
Definition Style.cs:61
Style(string name, int forcedOrder, bool internalStyle)
Constructor with parameters (internal use).
Definition Style.cs:101
Font CurrentFont
Gets or sets the current Font object of the style.
Definition Style.cs:43
NumberFormat CurrentNumberFormat
Gets or sets the current NumberFormat object of the style.
Definition Style.cs:48
Class providing static methods to parse string values to specific types or to print object as languag...
static string ToString(int input)
Transforms an integer to an invariant sting.