35 private MemoryStream stream;
71 public void Init(MemoryStream stream,
Workbook workbook, IOptions readerOptions, Action<MemoryStream, Workbook, string, IOptions, int?> inlinePluginHandler)
74 this.Workbook = workbook;
75 this.Options = readerOptions;
76 this.InlinePluginHandler = inlinePluginHandler;
90 XmlDocument xr =
new XmlDocument() { XmlResolver =
null };
91 using (XmlReader reader = XmlReader.Create(stream,
new XmlReaderSettings() { XmlResolver =
null }))
94 foreach (XmlNode node
in xr.DocumentElement.ChildNodes)
96 if (node.LocalName.Equals(
"numfmts", StringComparison.OrdinalIgnoreCase))
98 GetNumberFormats(node);
100 else if (node.LocalName.Equals(
"borders", StringComparison.OrdinalIgnoreCase))
104 else if (node.LocalName.Equals(
"fills", StringComparison.OrdinalIgnoreCase))
108 else if (node.LocalName.Equals(
"fonts", StringComparison.OrdinalIgnoreCase))
112 else if (node.LocalName.Equals(
"colors", StringComparison.OrdinalIgnoreCase))
118 foreach (XmlNode node
in xr.DocumentElement.ChildNodes)
120 if (node.LocalName.Equals(
"cellxfs", StringComparison.OrdinalIgnoreCase))
129 Workbook.AuxiliaryData.SetData(PlugInUUID.StyleReader, PlugInUUID.StyleEntity, styleReaderContainer);
133 throw new IOException(
"The XML entry could not be read from the input stream. Please see the inner exception:", ex);
140 private void HandleMruColors()
142 if (styleReaderContainer.GetMruColors().Count > 0)
144 foreach (
string color
in styleReaderContainer.GetMruColors())
155 private void GetNumberFormats(XmlNode node)
157 foreach (XmlNode childNode
in node.ChildNodes)
159 if (childNode.LocalName.Equals(
"numfmt", StringComparison.OrdinalIgnoreCase))
161 NumberFormat numberFormat =
new NumberFormat();
162 int id = ParserUtils.ParseInt(ReaderUtils.GetAttribute(childNode,
"numFmtId"));
163 string code = ReaderUtils.GetAttribute(childNode,
"formatCode",
string.Empty);
164 numberFormat.CustomFormatID = id;
165 numberFormat.Number = FormatNumber.Custom;
166 numberFormat.InternalID = id;
167 numberFormat.CustomFormatCode = code;
177 private void GetBorders(XmlNode node)
179 foreach (XmlNode border
in node.ChildNodes)
181 Border borderStyle =
new Border();
182 string diagonalDown = ReaderUtils.GetAttribute(border,
"diagonalDown");
183 string diagonalUp = ReaderUtils.GetAttribute(border,
"diagonalUp");
184 if (diagonalDown !=
null)
186 int value = ParserUtils.ParseBinaryBool(diagonalDown);
189 borderStyle.DiagonalDown =
true;
192 if (diagonalUp !=
null)
194 int value = ParserUtils.ParseBinaryBool(diagonalUp);
197 borderStyle.DiagonalUp =
true;
200 XmlNode innerNode = ReaderUtils.GetChildNode(border,
"diagonal");
201 if (innerNode !=
null)
203 borderStyle.DiagonalStyle = ParseBorderStyle(innerNode);
204 borderStyle.DiagonalColor = GetColor(innerNode, Border.DefaultBorderColor);
206 innerNode = ReaderUtils.GetChildNode(border,
"top");
207 if (innerNode !=
null)
209 borderStyle.TopStyle = ParseBorderStyle(innerNode);
210 borderStyle.TopColor = GetColor(innerNode, Border.DefaultBorderColor);
212 innerNode = ReaderUtils.GetChildNode(border,
"bottom");
213 if (innerNode !=
null)
215 borderStyle.BottomStyle = ParseBorderStyle(innerNode);
216 borderStyle.BottomColor = GetColor(innerNode, Border.DefaultBorderColor);
218 innerNode = ReaderUtils.GetChildNode(border,
"left");
219 if (innerNode !=
null)
221 borderStyle.LeftStyle = ParseBorderStyle(innerNode);
222 borderStyle.LeftColor = GetColor(innerNode, Border.DefaultBorderColor);
224 innerNode = ReaderUtils.GetChildNode(border,
"right");
225 if (innerNode !=
null)
227 borderStyle.RightStyle = ParseBorderStyle(innerNode);
228 borderStyle.RightColor = GetColor(innerNode, Border.DefaultBorderColor);
230 borderStyle.InternalID = this.styleReaderContainer.GetNextBorderId();
231 this.styleReaderContainer.AddStyleComponent(borderStyle);
240 private static StyleValue ParseBorderStyle(XmlNode innerNode)
242 string value = ReaderUtils.GetAttribute(innerNode,
"style");
245 return Border.GetStyleEnum(value);
247 return StyleValue.None;
254 private void GetFills(XmlNode node)
256 foreach (XmlNode fill
in node.ChildNodes)
258 Fill fillStyle =
new Fill();
259 XmlNode innerNode = ReaderUtils.GetChildNode(fill,
"patternFill");
260 if (innerNode !=
null)
262 string pattern = ReaderUtils.GetAttribute(innerNode,
"patternType",
string.Empty);
263 fillStyle.PatternFill = Fill.GetPatternEnum(pattern);
266 XmlNode fgColorNode = ReaderUtils.GetChildNode(innerNode,
"fgColor");
267 if (fgColorNode !=
null)
269 fillStyle.ForegroundColor = ReadColorFromNode(fgColorNode);
273 XmlNode bgColorNode = ReaderUtils.GetChildNode(innerNode,
"bgColor");
274 if (bgColorNode !=
null)
276 fillStyle.BackgroundColor = ReadColorFromNode(bgColorNode);
279 fillStyle.InternalID = this.styleReaderContainer.GetNextFillId();
280 this.styleReaderContainer.AddStyleComponent(fillStyle);
289 private static Color ReadColorFromNode(XmlNode colorNode)
292 string autoAttr = ReaderUtils.GetAttribute(colorNode,
"auto");
293 if (!
string.IsNullOrEmpty(autoAttr) && ParserUtils.ParseBinaryBool(autoAttr) == 1)
295 return Color.CreateAuto();
299 string rgbAttr = ReaderUtils.GetAttribute(colorNode,
"rgb");
300 if (!
string.IsNullOrEmpty(rgbAttr))
302 return Color.CreateRgb(rgbAttr);
306 string indexedAttr = ReaderUtils.GetAttribute(colorNode,
"indexed");
307 if (!
string.IsNullOrEmpty(indexedAttr))
309 return Color.CreateIndexed(ParserUtils.ParseInt(indexedAttr));
313 string themeAttr = ReaderUtils.GetAttribute(colorNode,
"theme");
314 if (!
string.IsNullOrEmpty(themeAttr))
316 int themeIndex = ParserUtils.ParseInt(themeAttr);
318 string tintAttr = ReaderUtils.GetAttribute(colorNode,
"tint");
320 if (!
string.IsNullOrEmpty(tintAttr))
322 tint = ParserUtils.ParseDouble(tintAttr);
324 return Color.CreateTheme((Theme.ColorSchemeElement)themeIndex, tint);
328 string systemAttr = ReaderUtils.GetAttribute(colorNode,
"system");
329 if (!
string.IsNullOrEmpty(systemAttr))
331 SystemColor sysColor =
new SystemColor(SystemColor.MapStringToValue(systemAttr));
332 return Color.CreateSystem(sysColor);
336 return Color.CreateNone();
343 private void GetFonts(XmlNode node)
346 foreach (XmlNode font
in node.ChildNodes)
348 Font fontStyle =
new Font();
349 XmlNode boldNode = ReaderUtils.GetChildNode(font,
"b");
350 if (boldNode !=
null)
352 fontStyle.Bold =
true;
354 XmlNode italicdNode = ReaderUtils.GetChildNode(font,
"i");
355 if (italicdNode !=
null)
357 fontStyle.Italic =
true;
359 XmlNode strikeNode = ReaderUtils.GetChildNode(font,
"strike");
360 if (strikeNode !=
null)
362 fontStyle.Strike =
true;
364 XmlNode outlineNode = ReaderUtils.GetChildNode(font,
"outline");
365 if (outlineNode !=
null)
367 fontStyle.Outline =
true;
369 XmlNode shadowNode = ReaderUtils.GetChildNode(font,
"shadow");
370 if (shadowNode !=
null)
372 fontStyle.Shadow =
true;
374 XmlNode condenseNode = ReaderUtils.GetChildNode(font,
"condense");
375 if (condenseNode !=
null)
377 fontStyle.Condense =
true;
379 XmlNode extendNode = ReaderUtils.GetChildNode(font,
"extend");
380 if (extendNode !=
null)
382 fontStyle.Extend =
true;
384 if (ReaderUtils.GetAttributeOfChild(font,
"u",
"val", out attribute))
386 if (attribute ==
null)
388 fontStyle.Underline = Font.UnderlineValue.Single;
392 fontStyle.Underline = Font.GetUnderlineEnum(attribute);
395 if (ReaderUtils.GetAttributeOfChild(font,
"vertAlign",
"val", out attribute))
397 fontStyle.VerticalAlign = Font.GetVerticalTextAlignEnum(attribute);
399 if (ReaderUtils.GetAttributeOfChild(font,
"sz",
"val", out attribute))
401 fontStyle.Size = ParserUtils.ParseFloat(attribute);
403 XmlNode colorNode = ReaderUtils.GetChildNode(font,
"color");
404 if (colorNode !=
null)
407 attribute = ReaderUtils.GetAttribute(colorNode,
"theme");
408 if (attribute !=
null)
410 ColorSchemeElement element = ColorSchemeElement.Dark1;
414 element = ColorSchemeElement.Dark1;
417 element = ColorSchemeElement.Light1;
420 element = ColorSchemeElement.Dark2;
423 element = ColorSchemeElement.Light2;
426 element = ColorSchemeElement.Accent1;
429 element = ColorSchemeElement.Accent2;
432 element = ColorSchemeElement.Accent3;
435 element = ColorSchemeElement.Accent4;
438 element = ColorSchemeElement.Accent5;
441 element = ColorSchemeElement.Accent6;
444 element = ColorSchemeElement.Hyperlink;
447 element = ColorSchemeElement.FollowedHyperlink;
450 fontStyle.ColorValue = Color.CreateTheme(element);
453 attribute = ReaderUtils.GetAttribute(colorNode,
"rgb");
454 if (attribute !=
null)
456 fontStyle.ColorValue = Color.CreateRgb(attribute);
459 if (ReaderUtils.GetAttributeOfChild(font,
"name",
"val", out attribute))
461 fontStyle.Name = attribute;
463 if (ReaderUtils.GetAttributeOfChild(font,
"family",
"val", out attribute))
469 fontStyle.Family = FontFamilyValue.NotApplicable;
472 fontStyle.Family = FontFamilyValue.Roman;
475 fontStyle.Family = FontFamilyValue.Swiss;
478 fontStyle.Family = FontFamilyValue.Modern;
481 fontStyle.Family = FontFamilyValue.Script;
484 fontStyle.Family = FontFamilyValue.Decorative;
487 fontStyle.Family = FontFamilyValue.Reserved1;
490 fontStyle.Family = FontFamilyValue.Reserved2;
493 fontStyle.Family = FontFamilyValue.Reserved3;
496 fontStyle.Family = FontFamilyValue.Reserved4;
499 fontStyle.Family = FontFamilyValue.Reserved5;
502 fontStyle.Family = FontFamilyValue.Reserved6;
505 fontStyle.Family = FontFamilyValue.Reserved7;
508 fontStyle.Family = FontFamilyValue.Reserved8;
511 fontStyle.Family = FontFamilyValue.Reserved9;
515 if (ReaderUtils.GetAttributeOfChild(font,
"scheme",
"val", out attribute))
520 fontStyle.Scheme = SchemeValue.Major;
523 fontStyle.Scheme = SchemeValue.Minor;
527 if (ReaderUtils.GetAttributeOfChild(font,
"charset",
"val", out attribute))
532 fontStyle.Charset = CharsetValue.ANSI;
535 fontStyle.Charset = CharsetValue.Default;
538 fontStyle.Charset = CharsetValue.Symbols;
541 fontStyle.Charset = CharsetValue.Macintosh;
544 fontStyle.Charset = CharsetValue.JIS;
547 fontStyle.Charset = CharsetValue.Hangul;
550 fontStyle.Charset = CharsetValue.Johab;
553 fontStyle.Charset = CharsetValue.GBK;
556 fontStyle.Charset = CharsetValue.Big5;
559 fontStyle.Charset = CharsetValue.Greek;
562 fontStyle.Charset = CharsetValue.Turkish;
565 fontStyle.Charset = CharsetValue.Vietnamese;
568 fontStyle.Charset = CharsetValue.Hebrew;
571 fontStyle.Charset = CharsetValue.Arabic;
574 fontStyle.Charset = CharsetValue.Baltic;
577 fontStyle.Charset = CharsetValue.Russian;
580 fontStyle.Charset = CharsetValue.Thai;
583 fontStyle.Charset = CharsetValue.EasternEuropean;
586 fontStyle.Charset = CharsetValue.OEM;
589 fontStyle.Charset = CharsetValue.ApplicationDefined;
594 fontStyle.InternalID = this.styleReaderContainer.GetNextFontId();
595 this.styleReaderContainer.AddStyleComponent(fontStyle);
603 private void GetCellXfs(XmlNode node)
605 foreach (XmlNode childNode
in node.ChildNodes)
607 if (ReaderUtils.IsNode(childNode,
"xf"))
609 CellXf cellXfStyle =
new CellXf();
610 string attribute = ReaderUtils.GetAttribute(childNode,
"applyAlignment");
611 if (attribute !=
null)
613 int value = ParserUtils.ParseBinaryBool(attribute);
614 cellXfStyle.ForceApplyAlignment = value == 1;
616 XmlNode alignmentNode = ReaderUtils.GetChildNode(childNode,
"alignment");
617 if (alignmentNode !=
null)
619 attribute = ReaderUtils.GetAttribute(alignmentNode,
"shrinkToFit");
620 if (attribute !=
null)
622 int value = ParserUtils.ParseBinaryBool(attribute);
625 cellXfStyle.Alignment = TextBreakValue.ShrinkToFit;
628 attribute = ReaderUtils.GetAttribute(alignmentNode,
"wrapText");
629 if (attribute !=
null && attribute ==
"1")
631 cellXfStyle.Alignment = TextBreakValue.WrapText;
633 attribute = ReaderUtils.GetAttribute(alignmentNode,
"horizontal",
string.Empty);
634 cellXfStyle.HorizontalAlign = CellXf.GetHorizontalAlignEnum(attribute);
635 attribute = ReaderUtils.GetAttribute(alignmentNode,
"vertical",
string.Empty);
636 cellXfStyle.VerticalAlign = CellXf.GetVerticalAlignEnum(attribute);
637 attribute = ReaderUtils.GetAttribute(alignmentNode,
"indent");
638 if (attribute !=
null)
640 cellXfStyle.Indent = ParserUtils.ParseInt(attribute);
642 attribute = ReaderUtils.GetAttribute(alignmentNode,
"textRotation");
643 if (attribute !=
null)
645 int rotation = ParserUtils.ParseInt(attribute);
646 cellXfStyle.TextRotation = rotation > 90 ? 90 - rotation : rotation;
649 XmlNode protectionNode = ReaderUtils.GetChildNode(childNode,
"protection");
650 if (protectionNode !=
null)
652 attribute = ReaderUtils.GetAttribute(protectionNode,
"hidden");
653 if (attribute !=
null && attribute ==
"1")
655 cellXfStyle.Hidden =
true;
657 attribute = ReaderUtils.GetAttribute(protectionNode,
"locked");
658 if (attribute !=
null && attribute ==
"0")
660 cellXfStyle.Locked =
false;
665 cellXfStyle.InternalID = this.styleReaderContainer.GetNextCellXFId();
666 this.styleReaderContainer.AddStyleComponent(cellXfStyle);
668 Style style =
new Style();
672 hasId = ParserUtils.TryParseInt(ReaderUtils.GetAttribute(childNode,
"numFmtId"), out
id);
673 NumberFormat format = this.styleReaderContainer.GetNumberFormat(
id);
674 if (!hasId || format ==
null)
676 FormatNumber formatNumber;
677 NumberFormat.TryParseFormatNumber(
id, out formatNumber);
680 format =
new NumberFormat
682 Number = formatNumber,
685 this.styleReaderContainer.AddStyleComponent(format);
687 hasId = ParserUtils.TryParseInt(ReaderUtils.GetAttribute(childNode,
"borderId"), out
id);
688 Border border = this.styleReaderContainer.GetBorder(
id);
689 if (!hasId || border ==
null)
693 InternalID = this.styleReaderContainer.GetNextBorderId()
696 hasId = ParserUtils.TryParseInt(ReaderUtils.GetAttribute(childNode,
"fillId"), out
id);
697 Fill fill = this.styleReaderContainer.GetFill(
id);
698 if (!hasId || fill ==
null)
702 InternalID = this.styleReaderContainer.GetNextFillId()
705 hasId = ParserUtils.TryParseInt(ReaderUtils.GetAttribute(childNode,
"fontId"), out
id);
706 Font font = this.styleReaderContainer.GetFont(
id);
707 if (!hasId || font ==
null)
711 InternalID = this.styleReaderContainer.GetNextFontId()
716 style.CurrentNumberFormat = format;
717 style.CurrentBorder = border;
718 style.CurrentFill = fill;
719 style.CurrentFont = font;
720 style.CurrentCellXf = cellXfStyle;
721 style.InternalID = this.styleReaderContainer.GetNextStyleId();
723 this.styleReaderContainer.AddStyleComponent(style);
732 private void GetColors(XmlNode node)
734 foreach (XmlNode color
in node.ChildNodes)
736 XmlNode mruColor = ReaderUtils.GetChildNode(color,
"color");
737 if (color.Name.Equals(
"mruColors", StringComparison.Ordinal) && mruColor !=
null)
739 foreach (XmlNode value
in color.ChildNodes)
741 string attribute = ReaderUtils.GetAttribute(value,
"rgb");
742 if (attribute !=
null)
744 this.styleReaderContainer.AddMruColor(attribute);
757 private static string GetColor(XmlNode node,
string fallback)
759 XmlNode childNode = ReaderUtils.GetChildNode(node,
"color");
760 if (childNode !=
null)
762 string color = ReaderUtils.GetAttribute(childNode,
"rgb");