NanoXLSX.Reader 3.0.0-rc.2
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 © 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;
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 List<NumberFormat> numberFormats = new List<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 numberFormats.Add(component as NumberFormat);
62 }
63 else if (t == typeof(Style))
64 {
65 styles.Add(component as Style);
66 }
67 else if (t == typeof(Border))
68 {
69 borders.Add(component as Border);
70 }
71 else if (t == typeof(Fill))
72 {
73 fills.Add(component as Fill);
74 }
75 else if (t == typeof(Font))
76 {
77 fonts.Add(component as Font);
78 }
79 }
80
86 public Style GetStyle(int index)
87 {
88 return GetComponent(typeof(Style), index) as Style;
89 }
90
98 public Style GetStyle(int index, out bool isDateStyle, out bool isTimeStyle)
99 {
100 Style style = GetComponent(typeof(Style), index) as Style;
101 isDateStyle = false;
102 isTimeStyle = false;
103 if (style != null)
104 {
105 isDateStyle = NumberFormat.IsDateFormat(style.CurrentNumberFormat.Number);
106 isTimeStyle = NumberFormat.IsTimeFormat(style.CurrentNumberFormat.Number);
107 }
108 return style;
109 }
110
116 public NumberFormat GetNumberFormat(int index)
117 {
118 return GetComponent(typeof(NumberFormat), index) as NumberFormat;
119 }
120
126 public Border GetBorder(int index)
127 {
128 return GetComponent(typeof(Border), index) as Border;
129 }
130
136 public Fill GetFill(int index)
137 {
138 return GetComponent(typeof(Fill), index) as Fill;
139 }
140
146 public Font GetFont(int index)
147 {
148 return GetComponent(typeof(Font), index) as Font;
149 }
150
155 public int GetNextStyleId()
156 {
157 return styles.Count;
158 }
159
164 public int GetNextCellXFId()
165 {
166 return cellXfs.Count;
167 }
168
173 public int GetNextBorderId()
174 {
175 return borders.Count;
176 }
177
182 public int GetNextFillId()
183 {
184 return fills.Count;
185 }
186
191 public int GetNextFontId()
192 {
193 return fonts.Count;
194 }
195
203 private AbstractStyle GetComponent(Type type, int index)
204 {
205 try
206 {
207 if (type == typeof(NumberFormat))
208 {
209 //Number format entries are handles differently, since identified by 'numFmtId'. Other components are identified by its entry index
210 NumberFormat numberFormat = numberFormats.Find(x => x.InternalID == index);
211 if (numberFormat == null)
212 {
213 throw new StyleException("The number format with the numFmtId: " + index + " was not found");
214 }
215 return numberFormat;
216 }
217 else if (type == typeof(Style))
218 {
219 return styles[index];
220 }
221 else if (type == typeof(Border))
222 {
223 return borders[index];
224 }
225 else if (type == typeof(Fill))
226 {
227 return fills[index];
228 }
229 else // must be font (CellXF is not handled here)
230 {
231 return fonts[index];
232 }
233 }
234 catch (Exception)
235 {
236 // Ignore
237 }
238 return null;
239 }
240
245 internal void AddMruColor(string value)
246 {
247 this.mruColors.Add(value);
248 }
249
254 internal List<string> GetMruColors()
255 {
256 return this.mruColors;
257 }
258
259 #endregion
260
261 }
262}
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.