NanoXLSX.Reader 3.1.0
Loading...
Searching...
No Matches
StyleReaderContainer.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 © 2026
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;
9using System.Collections.Generic;
10using NanoXLSX.Exceptions;
11using NanoXLSX.Styles;
12
13namespace NanoXLSX.Internal
14{
19 {
20
21 #region privateFields
22
23 private List<CellXf> cellXfs = new List<CellXf>();
24 private Dictionary<int, NumberFormat> numberFormats = new Dictionary<int, NumberFormat>();
25 private List<Style> styles = new List<Style>();
26 private List<Border> borders = new List<Border>();
27 private List<Fill> fills = new List<Fill>();
28 private List<Font> fonts = new List<Font>();
29 private List<string> mruColors = new List<string>();
30
31 #endregion
32
33 #region properties
34
38 public int StyleCount
39 {
40 get { return styles.Count; }
41 }
42
43 #endregion
44
45 #region functions
46
51
52 public void AddStyleComponent(AbstractStyle component)
53 {
54 Type t = component.GetType();
55 if (t == typeof(CellXf))
56 {
57 cellXfs.Add(component as CellXf);
58 }
59 else if (t == typeof(NumberFormat))
60 {
61 NumberFormat nf = component as NumberFormat;
62 if (nf.InternalID.HasValue && !numberFormats.ContainsKey(nf.InternalID.Value))
63 {
64 numberFormats.Add(nf.InternalID.Value, nf);
65 }
66 }
67 else if (t == typeof(Style))
68 {
69 styles.Add(component as Style);
70 }
71 else if (t == typeof(Border))
72 {
73 borders.Add(component as Border);
74 }
75 else if (t == typeof(Fill))
76 {
77 fills.Add(component as Fill);
78 }
79 else if (t == typeof(Font))
80 {
81 fonts.Add(component as Font);
82 }
83 }
84
90 public Style GetStyle(int index)
91 {
92 return GetComponent(typeof(Style), index) as Style;
93 }
94
102 public Style GetStyle(int index, out bool isDateStyle, out bool isTimeStyle)
103 {
104 Style style = GetComponent(typeof(Style), index) as Style;
105 isDateStyle = false;
106 isTimeStyle = false;
107 if (style != null)
108 {
109 isDateStyle = NumberFormat.IsDateFormat(style.CurrentNumberFormat.Number);
110 isTimeStyle = NumberFormat.IsTimeFormat(style.CurrentNumberFormat.Number);
111 }
112 return style;
113 }
114
120 public NumberFormat GetNumberFormat(int index)
121 {
122 return GetComponent(typeof(NumberFormat), index) as NumberFormat;
123 }
124
130 public Border GetBorder(int index)
131 {
132 return GetComponent(typeof(Border), index) as Border;
133 }
134
140 public Fill GetFill(int index)
141 {
142 return GetComponent(typeof(Fill), index) as Fill;
143 }
144
150 public Font GetFont(int index)
151 {
152 return GetComponent(typeof(Font), index) as Font;
153 }
154
159 public int GetNextStyleId()
160 {
161 return styles.Count;
162 }
163
168 public int GetNextCellXFId()
169 {
170 return cellXfs.Count;
171 }
172
177 public int GetNextBorderId()
178 {
179 return borders.Count;
180 }
181
186 public int GetNextFillId()
187 {
188 return fills.Count;
189 }
190
195 public int GetNextFontId()
196 {
197 return fonts.Count;
198 }
199
207 private AbstractStyle GetComponent(Type type, int index)
208 {
209 try
210 {
211 if (type == typeof(NumberFormat))
212 {
213 NumberFormat numberFormat;
214 if (numberFormats.TryGetValue(index, out numberFormat))
215 {
216 return numberFormat;
217 }
218 throw new StyleException("The number format with the numFmtId: " + index + " was not found");
219 }
220 else if (type == typeof(Style))
221 {
222 return styles[index];
223 }
224 else if (type == typeof(Border))
225 {
226 return borders[index];
227 }
228 else if (type == typeof(Fill))
229 {
230 return fills[index];
231 }
232 else // must be font (CellXF is not handled here)
233 {
234 return fonts[index];
235 }
236 }
237 catch (Exception)
238 {
239 // Ignore
240 }
241 return null;
242 }
243
248 internal void AddMruColor(string value)
249 {
250 this.mruColors.Add(value);
251 }
252
257 internal List<string> GetMruColors()
258 {
259 return this.mruColors;
260 }
261
262 #endregion
263
264 }
265}
Class representing a collection of pre-processed styles and their components. This class is internall...
Fill GetFill(int index)
Returns a fill component by its index.
Style GetStyle(int index)
Returns a whole style by its index.
int GetNextCellXFId()
Gets the next internal id of a cell XF component.
int GetNextBorderId()
Gets the next internal id of a border component.
int StyleCount
Gets the number of resolved styles.
Style GetStyle(int index, out bool isDateStyle, out bool isTimeStyle)
Returns a whole style by its index. It also returns information about the type of the style.
Border GetBorder(int index)
Returns a border component by its index.
int GetNextFillId()
Gets the next internal id of a fill component.
Font GetFont(int index)
Returns a font component by its index.
void AddStyleComponent(AbstractStyle component)
Adds a style component and determines the appropriate type of it automatically.
int GetNextStyleId()
Gets the next internal id of a style.
NumberFormat GetNumberFormat(int index)
Returns a number format component by its index.
int GetNextFontId()
Gets the next internal id of a font component.