9using System.Collections.Generic;
17 internal class StyleManager
20 private List<AbstractStyle> borders;
21 private List<AbstractStyle> cellXfs;
22 private List<AbstractStyle> fills;
23 private List<AbstractStyle> fonts;
24 private List<AbstractStyle> numberFormats;
25 private List<AbstractStyle> styles;
34 borders =
new List<AbstractStyle>();
35 cellXfs =
new List<AbstractStyle>();
36 fills =
new List<AbstractStyle>();
37 fonts =
new List<AbstractStyle>();
38 numberFormats =
new List<AbstractStyle>();
39 styles =
new List<AbstractStyle>();
51 private static AbstractStyle GetComponentByHash(ref List<AbstractStyle> list,
int hash)
54 for (
int i = 0; i < len; i++)
56 if (list[i].GetHashCode() == hash)
68 public Border[] GetBorders()
70 return Array.ConvertAll(borders.ToArray(), x => (Border)x);
77 public int GetBorderStyleNumber()
90 public Fill[] GetFills()
92 return Array.ConvertAll(fills.ToArray(), x => (Fill)x);
99 public int GetFillStyleNumber()
110 public Font[] GetFonts()
112 return Array.ConvertAll(fonts.ToArray(), x => (Font)x);
119 public int GetFontStyleNumber()
130 public NumberFormat[] GetNumberFormats()
132 return Array.ConvertAll(numberFormats.ToArray(), x => (NumberFormat)x);
139 public int GetNumberFormatStyleNumber()
141 return numberFormats.Count;
150 public Style[] GetStyles()
152 return Array.ConvertAll(styles.ToArray(), x => (Style)x);
159 public int GetStyleNumber()
172 public Style AddStyle(Style style)
174 int hash = AddStyleComponent(style);
175 return (Style)GetComponentByHash(ref styles, hash);
184 private int AddStyleComponent(AbstractStyle style,
int?
id)
186 style.InternalID = id;
187 return AddStyleComponent(style);
195 private int AddStyleComponent(AbstractStyle style)
197 int hash = style.GetHashCode();
198 if (style.GetType() == typeof(Border))
200 if (GetComponentByHash(ref borders, hash) ==
null)
201 { borders.Add(style); }
202 Reorganize(ref borders);
204 else if (style.GetType() == typeof(CellXf))
206 if (GetComponentByHash(ref cellXfs, hash) ==
null)
207 { cellXfs.Add(style); }
208 Reorganize(ref cellXfs);
210 else if (style.GetType() == typeof(Fill))
212 if (GetComponentByHash(ref fills, hash) ==
null)
213 { fills.Add(style); }
214 Reorganize(ref fills);
216 else if (style.GetType() == typeof(Font))
218 if (GetComponentByHash(ref fonts, hash) ==
null)
219 { fonts.Add(style); }
220 Reorganize(ref fonts);
222 else if (style.GetType() == typeof(NumberFormat))
224 if (GetComponentByHash(ref numberFormats, hash) ==
null)
225 { numberFormats.Add(style); }
226 Reorganize(ref numberFormats);
228 else if (style.GetType() == typeof(Style))
230 Style s = (Style)style;
231 if (GetComponentByHash(ref styles, hash) ==
null)
234 if (!s.InternalID.HasValue)
241 id = s.InternalID.Value;
243 int temp = AddStyleComponent(s.CurrentBorder,
id);
244 s.CurrentBorder = (Border)GetComponentByHash(ref borders, temp);
245 temp = AddStyleComponent(s.CurrentCellXf,
id);
246 s.CurrentCellXf = (CellXf)GetComponentByHash(ref cellXfs, temp);
247 temp = AddStyleComponent(s.CurrentFill,
id);
248 s.CurrentFill = (Fill)GetComponentByHash(ref fills, temp);
249 temp = AddStyleComponent(s.CurrentFont,
id);
250 s.CurrentFont = (Font)GetComponentByHash(ref fonts, temp);
251 temp = AddStyleComponent(s.CurrentNumberFormat,
id);
252 s.CurrentNumberFormat = (NumberFormat)GetComponentByHash(ref numberFormats, temp);
255 Reorganize(ref styles);
256 hash = s.GetHashCode();
266 internal static StyleManager GetManagedStyles(Workbook workbook)
268 StyleManager styleManager =
new StyleManager();
269 styleManager.AddStyle(
new Style(
"default", 0,
true));
270 Style borderStyle =
new Style(
"default_border_style", 1,
true)
272 CurrentBorder = BasicStyles.DottedFill_0_125.CurrentBorder,
273 CurrentFill = BasicStyles.DottedFill_0_125.CurrentFill
275 styleManager.AddStyle(borderStyle);
277 for (
int i = 0; i < workbook.Worksheets.Count; i++)
279 foreach (KeyValuePair<string, Cell> cell
in workbook.Worksheets[i].Cells)
281 if (cell.Value.CellStyle !=
null)
283 Style resolvedStyle = styleManager.AddStyle(cell.Value.CellStyle);
284 workbook.Worksheets[i].Cells[cell.Key].SetStyle(resolvedStyle,
true);
287 foreach (KeyValuePair<int, Column> column
in workbook.Worksheets[i].Columns)
289 if (column.Value.DefaultColumnStyle !=
null)
291 Style resolvedStyle = styleManager.AddStyle(column.Value.DefaultColumnStyle);
292 workbook.Worksheets[i].Columns[column.Key].SetDefaultColumnStyle(resolvedStyle,
true);
303 private static void Reorganize(ref List<AbstractStyle> list)
305 int len = list.Count;
308 for (
int i = 0; i < len; i++)
310 list[i].InternalID = id;