33 private MemoryStream stream;
56 public void Init(MemoryStream stream,
Workbook workbook, IOptions readerOptions)
59 this.Workbook = workbook;
73 XmlDocument xr =
new XmlDocument() { XmlResolver =
null };
74 using (XmlReader reader = XmlReader.Create(stream,
new XmlReaderSettings() { XmlResolver =
null }))
77 foreach (XmlNode node
in xr.DocumentElement.ChildNodes)
79 if (node.LocalName.Equals(
"numfmts", StringComparison.OrdinalIgnoreCase))
81 GetNumberFormats(node);
83 else if (node.LocalName.Equals(
"borders", StringComparison.OrdinalIgnoreCase))
87 else if (node.LocalName.Equals(
"fills", StringComparison.OrdinalIgnoreCase))
91 else if (node.LocalName.Equals(
"fonts", StringComparison.OrdinalIgnoreCase))
95 else if (node.LocalName.Equals(
"colors", StringComparison.OrdinalIgnoreCase))
101 foreach (XmlNode node
in xr.DocumentElement.ChildNodes)
103 if (node.LocalName.Equals(
"cellxfs", StringComparison.OrdinalIgnoreCase))
109 RederPlugInHandler.HandleInlineQueuePlugins(ref stream,
Workbook, PlugInUUID.StyleInlineReader);
112 Workbook.AuxiliaryData.SetData(PlugInUUID.StyleReader, PlugInUUID.StyleEntity, styleReaderContainer);
116 throw new IOException(
"The XML entry could not be read from the input stream. Please see the inner exception:", ex);
123 private void HandleMruColors()
125 if (styleReaderContainer.GetMruColors().Count > 0)
127 foreach (
string color
in styleReaderContainer.GetMruColors())
138 private void GetNumberFormats(XmlNode node)
140 foreach (XmlNode childNode
in node.ChildNodes)
142 if (childNode.LocalName.Equals(
"numfmt", StringComparison.OrdinalIgnoreCase))
144 NumberFormat numberFormat =
new NumberFormat();
145 int id = ParserUtils.ParseInt(ReaderUtils.GetAttribute(childNode,
"numFmtId"));
146 string code = ReaderUtils.GetAttribute(childNode,
"formatCode",
string.Empty);
147 numberFormat.CustomFormatID = id;
148 numberFormat.Number = FormatNumber.Custom;
149 numberFormat.InternalID = id;
150 numberFormat.CustomFormatCode = code;
160 private void GetBorders(XmlNode node)
162 foreach (XmlNode border
in node.ChildNodes)
164 Border borderStyle =
new Border();
165 string diagonalDown = ReaderUtils.GetAttribute(border,
"diagonalDown");
166 string diagonalUp = ReaderUtils.GetAttribute(border,
"diagonalUp");
167 if (diagonalDown !=
null)
169 int value = ParserUtils.ParseBinaryBool(diagonalDown);
172 borderStyle.DiagonalDown =
true;
175 if (diagonalUp !=
null)
177 int value = ParserUtils.ParseBinaryBool(diagonalUp);
180 borderStyle.DiagonalUp =
true;
183 XmlNode innerNode = ReaderUtils.GetChildNode(border,
"diagonal");
184 if (innerNode !=
null)
186 borderStyle.DiagonalStyle = ParseBorderStyle(innerNode);
187 borderStyle.DiagonalColor = GetColor(innerNode, Border.DefaultBorderColor);
189 innerNode = ReaderUtils.GetChildNode(border,
"top");
190 if (innerNode !=
null)
192 borderStyle.TopStyle = ParseBorderStyle(innerNode);
193 borderStyle.TopColor = GetColor(innerNode, Border.DefaultBorderColor);
195 innerNode = ReaderUtils.GetChildNode(border,
"bottom");
196 if (innerNode !=
null)
198 borderStyle.BottomStyle = ParseBorderStyle(innerNode);
199 borderStyle.BottomColor = GetColor(innerNode, Border.DefaultBorderColor);
201 innerNode = ReaderUtils.GetChildNode(border,
"left");
202 if (innerNode !=
null)
204 borderStyle.LeftStyle = ParseBorderStyle(innerNode);
205 borderStyle.LeftColor = GetColor(innerNode, Border.DefaultBorderColor);
207 innerNode = ReaderUtils.GetChildNode(border,
"right");
208 if (innerNode !=
null)
210 borderStyle.RightStyle = ParseBorderStyle(innerNode);
211 borderStyle.RightColor = GetColor(innerNode, Border.DefaultBorderColor);
213 borderStyle.InternalID = this.styleReaderContainer.GetNextBorderId();
214 this.styleReaderContainer.AddStyleComponent(borderStyle);
223 private static StyleValue ParseBorderStyle(XmlNode innerNode)
225 string value = ReaderUtils.GetAttribute(innerNode,
"style");
228 return Border.GetStyleEnum(value);
230 return StyleValue.None;
237 private void GetFills(XmlNode node)
240 foreach (XmlNode fill
in node.ChildNodes)
242 Fill fillStyle =
new Fill();
243 XmlNode innerNode = ReaderUtils.GetChildNode(fill,
"patternFill");
244 if (innerNode !=
null)
246 string pattern = ReaderUtils.GetAttribute(innerNode,
"patternType",
string.Empty);
247 fillStyle.PatternFill = Fill.GetPatternEnum(pattern);
248 if (ReaderUtils.GetAttributeOfChild(innerNode,
"fgColor",
"rgb", out attribute))
250 if (!
string.IsNullOrEmpty(attribute))
252 fillStyle.ForegroundColor = attribute;
255 XmlNode backgroundNode = ReaderUtils.GetChildNode(innerNode,
"bgColor");
256 if (backgroundNode !=
null)
258 string backgroundArgb = ReaderUtils.GetAttribute(backgroundNode,
"rgb");
259 if (!
string.IsNullOrEmpty(backgroundArgb))
261 fillStyle.BackgroundColor = backgroundArgb;
263 string backgroundIndex = ReaderUtils.GetAttribute(backgroundNode,
"indexed");
264 if (!
string.IsNullOrEmpty(backgroundIndex))
266 fillStyle.IndexedColor = ParserUtils.ParseInt(backgroundIndex);
271 fillStyle.InternalID = this.styleReaderContainer.GetNextFillId();
272 this.styleReaderContainer.AddStyleComponent(fillStyle);
280 private void GetFonts(XmlNode node)
283 foreach (XmlNode font
in node.ChildNodes)
285 Font fontStyle =
new Font();
286 XmlNode boldNode = ReaderUtils.GetChildNode(font,
"b");
287 if (boldNode !=
null)
289 fontStyle.Bold =
true;
291 XmlNode italicdNode = ReaderUtils.GetChildNode(font,
"i");
292 if (italicdNode !=
null)
294 fontStyle.Italic =
true;
296 XmlNode strikeNode = ReaderUtils.GetChildNode(font,
"strike");
297 if (strikeNode !=
null)
299 fontStyle.Strike =
true;
301 if (ReaderUtils.GetAttributeOfChild(font,
"u",
"val", out attribute))
303 if (attribute ==
null)
305 fontStyle.Underline = Font.UnderlineValue.Single;
309 fontStyle.Underline = Font.GetUnderlineEnum(attribute);
312 if (ReaderUtils.GetAttributeOfChild(font,
"vertAlign",
"val", out attribute))
314 fontStyle.VerticalAlign = Font.GetVerticalTextAlignEnum(attribute);
316 if (ReaderUtils.GetAttributeOfChild(font,
"sz",
"val", out attribute))
318 fontStyle.Size = ParserUtils.ParseFloat(attribute);
320 XmlNode colorNode = ReaderUtils.GetChildNode(font,
"color");
321 if (colorNode !=
null)
324 attribute = ReaderUtils.GetAttribute(colorNode,
"theme");
325 if (attribute !=
null)
330 fontStyle.ColorTheme = ColorSchemeElement.Dark1;
333 fontStyle.ColorTheme = ColorSchemeElement.Light1;
336 fontStyle.ColorTheme = ColorSchemeElement.Dark2;
339 fontStyle.ColorTheme = ColorSchemeElement.Light2;
342 fontStyle.ColorTheme = ColorSchemeElement.Accent1;
345 fontStyle.ColorTheme = ColorSchemeElement.Accent2;
348 fontStyle.ColorTheme = ColorSchemeElement.Accent3;
351 fontStyle.ColorTheme = ColorSchemeElement.Accent4;
354 fontStyle.ColorTheme = ColorSchemeElement.Accent5;
357 fontStyle.ColorTheme = ColorSchemeElement.Accent6;
360 fontStyle.ColorTheme = ColorSchemeElement.Hyperlink;
363 fontStyle.ColorTheme = ColorSchemeElement.FollowedHyperlink;
367 attribute = ReaderUtils.GetAttribute(colorNode,
"rgb");
368 if (attribute !=
null)
370 fontStyle.ColorValue = attribute;
373 if (ReaderUtils.GetAttributeOfChild(font,
"name",
"val", out attribute))
375 fontStyle.Name = attribute;
377 if (ReaderUtils.GetAttributeOfChild(font,
"family",
"val", out attribute))
383 fontStyle.Family = FontFamilyValue.NotApplicable;
386 fontStyle.Family = FontFamilyValue.Roman;
389 fontStyle.Family = FontFamilyValue.Swiss;
392 fontStyle.Family = FontFamilyValue.Modern;
395 fontStyle.Family = FontFamilyValue.Script;
398 fontStyle.Family = FontFamilyValue.Decorative;
401 fontStyle.Family = FontFamilyValue.Reserved1;
404 fontStyle.Family = FontFamilyValue.Reserved2;
407 fontStyle.Family = FontFamilyValue.Reserved3;
410 fontStyle.Family = FontFamilyValue.Reserved4;
413 fontStyle.Family = FontFamilyValue.Reserved5;
416 fontStyle.Family = FontFamilyValue.Reserved6;
419 fontStyle.Family = FontFamilyValue.Reserved7;
422 fontStyle.Family = FontFamilyValue.Reserved8;
425 fontStyle.Family = FontFamilyValue.Reserved9;
429 if (ReaderUtils.GetAttributeOfChild(font,
"scheme",
"val", out attribute))
434 fontStyle.Scheme = SchemeValue.Major;
437 fontStyle.Scheme = SchemeValue.Minor;
441 if (ReaderUtils.GetAttributeOfChild(font,
"charset",
"val", out attribute))
446 fontStyle.Charset = CharsetValue.ANSI;
449 fontStyle.Charset = CharsetValue.Default;
452 fontStyle.Charset = CharsetValue.Symbols;
455 fontStyle.Charset = CharsetValue.Macintosh;
458 fontStyle.Charset = CharsetValue.JIS;
461 fontStyle.Charset = CharsetValue.Hangul;
464 fontStyle.Charset = CharsetValue.Johab;
467 fontStyle.Charset = CharsetValue.GBK;
470 fontStyle.Charset = CharsetValue.Big5;
473 fontStyle.Charset = CharsetValue.Greek;
476 fontStyle.Charset = CharsetValue.Turkish;
479 fontStyle.Charset = CharsetValue.Vietnamese;
482 fontStyle.Charset = CharsetValue.Hebrew;
485 fontStyle.Charset = CharsetValue.Arabic;
488 fontStyle.Charset = CharsetValue.Baltic;
491 fontStyle.Charset = CharsetValue.Russian;
494 fontStyle.Charset = CharsetValue.Thai;
497 fontStyle.Charset = CharsetValue.EasternEuropean;
500 fontStyle.Charset = CharsetValue.OEM;
503 fontStyle.Charset = CharsetValue.ApplicationDefined;
508 fontStyle.InternalID = this.styleReaderContainer.GetNextFontId();
509 this.styleReaderContainer.AddStyleComponent(fontStyle);
517 private void GetCellXfs(XmlNode node)
519 foreach (XmlNode childNode
in node.ChildNodes)
521 if (ReaderUtils.IsNode(childNode,
"xf"))
523 CellXf cellXfStyle =
new CellXf();
524 string attribute = ReaderUtils.GetAttribute(childNode,
"applyAlignment");
525 if (attribute !=
null)
527 int value = ParserUtils.ParseBinaryBool(attribute);
528 cellXfStyle.ForceApplyAlignment = value == 1;
530 XmlNode alignmentNode = ReaderUtils.GetChildNode(childNode,
"alignment");
531 if (alignmentNode !=
null)
533 attribute = ReaderUtils.GetAttribute(alignmentNode,
"shrinkToFit");
534 if (attribute !=
null)
536 int value = ParserUtils.ParseBinaryBool(attribute);
539 cellXfStyle.Alignment = TextBreakValue.ShrinkToFit;
542 attribute = ReaderUtils.GetAttribute(alignmentNode,
"wrapText");
543 if (attribute !=
null && attribute ==
"1")
545 cellXfStyle.Alignment = TextBreakValue.WrapText;
547 attribute = ReaderUtils.GetAttribute(alignmentNode,
"horizontal",
string.Empty);
548 cellXfStyle.HorizontalAlign = CellXf.GetHorizontalAlignEnum(attribute);
549 attribute = ReaderUtils.GetAttribute(alignmentNode,
"vertical",
string.Empty);
550 cellXfStyle.VerticalAlign = CellXf.GetVerticalAlignEnum(attribute);
551 attribute = ReaderUtils.GetAttribute(alignmentNode,
"indent");
552 if (attribute !=
null)
554 cellXfStyle.Indent = ParserUtils.ParseInt(attribute);
556 attribute = ReaderUtils.GetAttribute(alignmentNode,
"textRotation");
557 if (attribute !=
null)
559 int rotation = ParserUtils.ParseInt(attribute);
560 cellXfStyle.TextRotation = rotation > 90 ? 90 - rotation : rotation;
563 XmlNode protectionNode = ReaderUtils.GetChildNode(childNode,
"protection");
564 if (protectionNode !=
null)
566 attribute = ReaderUtils.GetAttribute(protectionNode,
"hidden");
567 if (attribute !=
null && attribute ==
"1")
569 cellXfStyle.Hidden =
true;
571 attribute = ReaderUtils.GetAttribute(protectionNode,
"locked");
572 if (attribute !=
null && attribute ==
"0")
574 cellXfStyle.Locked =
false;
579 cellXfStyle.InternalID = this.styleReaderContainer.GetNextCellXFId();
580 this.styleReaderContainer.AddStyleComponent(cellXfStyle);
582 Style style =
new Style();
586 hasId = ParserUtils.TryParseInt(ReaderUtils.GetAttribute(childNode,
"numFmtId"), out
id);
587 NumberFormat format = this.styleReaderContainer.GetNumberFormat(
id);
588 if (!hasId || format ==
null)
590 FormatNumber formatNumber;
591 NumberFormat.TryParseFormatNumber(
id, out formatNumber);
594 format =
new NumberFormat
596 Number = formatNumber,
599 this.styleReaderContainer.AddStyleComponent(format);
601 hasId = ParserUtils.TryParseInt(ReaderUtils.GetAttribute(childNode,
"borderId"), out
id);
602 Border border = this.styleReaderContainer.GetBorder(
id);
603 if (!hasId || border ==
null)
607 InternalID = this.styleReaderContainer.GetNextBorderId()
610 hasId = ParserUtils.TryParseInt(ReaderUtils.GetAttribute(childNode,
"fillId"), out
id);
611 Fill fill = this.styleReaderContainer.GetFill(
id);
612 if (!hasId || fill ==
null)
616 InternalID = this.styleReaderContainer.GetNextFillId()
619 hasId = ParserUtils.TryParseInt(ReaderUtils.GetAttribute(childNode,
"fontId"), out
id);
620 Font font = this.styleReaderContainer.GetFont(
id);
621 if (!hasId || font ==
null)
625 InternalID = this.styleReaderContainer.GetNextFontId()
630 style.CurrentNumberFormat = format;
631 style.CurrentBorder = border;
632 style.CurrentFill = fill;
633 style.CurrentFont = font;
634 style.CurrentCellXf = cellXfStyle;
635 style.InternalID = this.styleReaderContainer.GetNextStyleId();
637 this.styleReaderContainer.AddStyleComponent(style);
646 private void GetColors(XmlNode node)
648 foreach (XmlNode color
in node.ChildNodes)
650 XmlNode mruColor = ReaderUtils.GetChildNode(color,
"color");
651 if (color.Name.Equals(
"mruColors", StringComparison.Ordinal) && mruColor !=
null)
653 foreach (XmlNode value
in color.ChildNodes)
655 string attribute = ReaderUtils.GetAttribute(value,
"rgb");
656 if (attribute !=
null)
658 this.styleReaderContainer.AddMruColor(attribute);
671 private static string GetColor(XmlNode node,
string fallback)
673 XmlNode childNode = ReaderUtils.GetChildNode(node,
"color");
674 if (childNode !=
null)
676 string color = ReaderUtils.GetAttribute(childNode,
"rgb");