36 private MemoryStream stream;
59 public void Init(MemoryStream stream,
Workbook workbook, IOptions readerOptions)
62 this.Workbook = workbook;
76 XmlDocument xr =
new XmlDocument() { XmlResolver =
null };
77 using (XmlReader reader = XmlReader.Create(stream,
new XmlReaderSettings() { XmlResolver =
null }))
80 foreach (XmlNode node
in xr.DocumentElement.ChildNodes)
82 if (node.LocalName.Equals(
"numfmts", StringComparison.OrdinalIgnoreCase))
84 GetNumberFormats(node);
86 else if (node.LocalName.Equals(
"borders", StringComparison.OrdinalIgnoreCase))
90 else if (node.LocalName.Equals(
"fills", StringComparison.OrdinalIgnoreCase))
94 else if (node.LocalName.Equals(
"fonts", StringComparison.OrdinalIgnoreCase))
98 else if (node.LocalName.Equals(
"colors", StringComparison.OrdinalIgnoreCase))
104 foreach (XmlNode node
in xr.DocumentElement.ChildNodes)
106 if (node.LocalName.Equals(
"cellxfs", StringComparison.OrdinalIgnoreCase))
112 RederPlugInHandler.HandleInlineQueuePlugins(ref stream,
Workbook, PlugInUUID.StyleInlineReader);
115 Workbook.AuxiliaryData.SetData(PlugInUUID.StyleReader, PlugInUUID.StyleEntity, styleReaderContainer);
119 throw new IOException(
"The XML entry could not be read from the input stream. Please see the inner exception:", ex);
126 private void HandleMruColors()
128 if (styleReaderContainer.GetMruColors().Count > 0)
130 foreach (
string color
in styleReaderContainer.GetMruColors())
141 private void GetNumberFormats(XmlNode node)
143 foreach (XmlNode childNode
in node.ChildNodes)
145 if (childNode.LocalName.Equals(
"numfmt", StringComparison.OrdinalIgnoreCase))
147 NumberFormat numberFormat =
new NumberFormat();
148 int id = ParserUtils.ParseInt(ReaderUtils.GetAttribute(childNode,
"numFmtId"));
149 string code = ReaderUtils.GetAttribute(childNode,
"formatCode",
string.Empty);
150 numberFormat.CustomFormatID = id;
151 numberFormat.Number = FormatNumber.Custom;
152 numberFormat.InternalID = id;
153 numberFormat.CustomFormatCode = code;
163 private void GetBorders(XmlNode node)
165 foreach (XmlNode border
in node.ChildNodes)
167 Border borderStyle =
new Border();
168 string diagonalDown = ReaderUtils.GetAttribute(border,
"diagonalDown");
169 string diagonalUp = ReaderUtils.GetAttribute(border,
"diagonalUp");
170 if (diagonalDown !=
null)
172 int value = ParserUtils.ParseBinaryBool(diagonalDown);
175 borderStyle.DiagonalDown =
true;
178 if (diagonalUp !=
null)
180 int value = ParserUtils.ParseBinaryBool(diagonalUp);
183 borderStyle.DiagonalUp =
true;
186 XmlNode innerNode = ReaderUtils.GetChildNode(border,
"diagonal");
187 if (innerNode !=
null)
189 borderStyle.DiagonalStyle = ParseBorderStyle(innerNode);
190 borderStyle.DiagonalColor = GetColor(innerNode, Border.DefaultBorderColor);
192 innerNode = ReaderUtils.GetChildNode(border,
"top");
193 if (innerNode !=
null)
195 borderStyle.TopStyle = ParseBorderStyle(innerNode);
196 borderStyle.TopColor = GetColor(innerNode, Border.DefaultBorderColor);
198 innerNode = ReaderUtils.GetChildNode(border,
"bottom");
199 if (innerNode !=
null)
201 borderStyle.BottomStyle = ParseBorderStyle(innerNode);
202 borderStyle.BottomColor = GetColor(innerNode, Border.DefaultBorderColor);
204 innerNode = ReaderUtils.GetChildNode(border,
"left");
205 if (innerNode !=
null)
207 borderStyle.LeftStyle = ParseBorderStyle(innerNode);
208 borderStyle.LeftColor = GetColor(innerNode, Border.DefaultBorderColor);
210 innerNode = ReaderUtils.GetChildNode(border,
"right");
211 if (innerNode !=
null)
213 borderStyle.RightStyle = ParseBorderStyle(innerNode);
214 borderStyle.RightColor = GetColor(innerNode, Border.DefaultBorderColor);
216 borderStyle.InternalID = this.styleReaderContainer.GetNextBorderId();
217 this.styleReaderContainer.AddStyleComponent(borderStyle);
226 private static StyleValue ParseBorderStyle(XmlNode innerNode)
228 string value = ReaderUtils.GetAttribute(innerNode,
"style");
231 return Border.GetStyleEnum(value);
233 return StyleValue.None;
240 private void GetFills(XmlNode node)
242 foreach (XmlNode fill
in node.ChildNodes)
244 Fill fillStyle =
new Fill();
245 XmlNode innerNode = ReaderUtils.GetChildNode(fill,
"patternFill");
246 if (innerNode !=
null)
248 string pattern = ReaderUtils.GetAttribute(innerNode,
"patternType",
string.Empty);
249 fillStyle.PatternFill = Fill.GetPatternEnum(pattern);
252 XmlNode fgColorNode = ReaderUtils.GetChildNode(innerNode,
"fgColor");
253 if (fgColorNode !=
null)
255 fillStyle.ForegroundColor = ReadColorFromNode(fgColorNode);
259 XmlNode bgColorNode = ReaderUtils.GetChildNode(innerNode,
"bgColor");
260 if (bgColorNode !=
null)
262 fillStyle.BackgroundColor = ReadColorFromNode(bgColorNode);
265 fillStyle.InternalID = this.styleReaderContainer.GetNextFillId();
266 this.styleReaderContainer.AddStyleComponent(fillStyle);
275 private Color ReadColorFromNode(XmlNode colorNode)
278 string autoAttr = ReaderUtils.GetAttribute(colorNode,
"auto");
279 if (!
string.IsNullOrEmpty(autoAttr) && ParserUtils.ParseBinaryBool(autoAttr) == 1)
281 return Color.CreateAuto();
285 string rgbAttr = ReaderUtils.GetAttribute(colorNode,
"rgb");
286 if (!
string.IsNullOrEmpty(rgbAttr))
288 return Color.CreateRgb(rgbAttr);
292 string indexedAttr = ReaderUtils.GetAttribute(colorNode,
"indexed");
293 if (!
string.IsNullOrEmpty(indexedAttr))
295 return Color.CreateIndexed(ParserUtils.ParseInt(indexedAttr));
299 string themeAttr = ReaderUtils.GetAttribute(colorNode,
"theme");
300 if (!
string.IsNullOrEmpty(themeAttr))
302 int themeIndex = ParserUtils.ParseInt(themeAttr);
304 string tintAttr = ReaderUtils.GetAttribute(colorNode,
"tint");
306 if (!
string.IsNullOrEmpty(tintAttr))
308 tint = ParserUtils.ParseDouble(tintAttr);
310 return Color.CreateTheme((Theme.ColorSchemeElement)themeIndex, tint);
314 string systemAttr = ReaderUtils.GetAttribute(colorNode,
"system");
315 if (!
string.IsNullOrEmpty(systemAttr))
317 SystemColor sysColor =
new SystemColor(SystemColor.MapStringToValue(systemAttr));
318 return Color.CreateSystem(sysColor);
322 return Color.CreateNone();
329 private void GetFonts(XmlNode node)
332 foreach (XmlNode font
in node.ChildNodes)
334 Font fontStyle =
new Font();
335 XmlNode boldNode = ReaderUtils.GetChildNode(font,
"b");
336 if (boldNode !=
null)
338 fontStyle.Bold =
true;
340 XmlNode italicdNode = ReaderUtils.GetChildNode(font,
"i");
341 if (italicdNode !=
null)
343 fontStyle.Italic =
true;
345 XmlNode strikeNode = ReaderUtils.GetChildNode(font,
"strike");
346 if (strikeNode !=
null)
348 fontStyle.Strike =
true;
350 XmlNode outlineNode = ReaderUtils.GetChildNode(font,
"outline");
351 if (outlineNode !=
null)
353 fontStyle.Outline =
true;
355 XmlNode shadowNode = ReaderUtils.GetChildNode(font,
"shadow");
356 if (shadowNode !=
null)
358 fontStyle.Shadow =
true;
360 XmlNode condenseNode = ReaderUtils.GetChildNode(font,
"condense");
361 if (condenseNode !=
null)
363 fontStyle.Condense =
true;
365 XmlNode extendNode = ReaderUtils.GetChildNode(font,
"extend");
366 if (extendNode !=
null)
368 fontStyle.Extend =
true;
370 if (ReaderUtils.GetAttributeOfChild(font,
"u",
"val", out attribute))
372 if (attribute ==
null)
374 fontStyle.Underline = Font.UnderlineValue.Single;
378 fontStyle.Underline = Font.GetUnderlineEnum(attribute);
381 if (ReaderUtils.GetAttributeOfChild(font,
"vertAlign",
"val", out attribute))
383 fontStyle.VerticalAlign = Font.GetVerticalTextAlignEnum(attribute);
385 if (ReaderUtils.GetAttributeOfChild(font,
"sz",
"val", out attribute))
387 fontStyle.Size = ParserUtils.ParseFloat(attribute);
389 XmlNode colorNode = ReaderUtils.GetChildNode(font,
"color");
390 if (colorNode !=
null)
393 attribute = ReaderUtils.GetAttribute(colorNode,
"theme");
394 if (attribute !=
null)
396 ColorSchemeElement element = ColorSchemeElement.Dark1;
400 element = ColorSchemeElement.Dark1;
403 element = ColorSchemeElement.Light1;
406 element = ColorSchemeElement.Dark2;
409 element = ColorSchemeElement.Light2;
412 element = ColorSchemeElement.Accent1;
415 element = ColorSchemeElement.Accent2;
418 element = ColorSchemeElement.Accent3;
421 element = ColorSchemeElement.Accent4;
424 element = ColorSchemeElement.Accent5;
427 element = ColorSchemeElement.Accent6;
430 element = ColorSchemeElement.Hyperlink;
433 element = ColorSchemeElement.FollowedHyperlink;
436 fontStyle.ColorValue = Color.CreateTheme(element);
439 attribute = ReaderUtils.GetAttribute(colorNode,
"rgb");
440 if (attribute !=
null)
442 fontStyle.ColorValue = Color.CreateRgb(attribute);
445 if (ReaderUtils.GetAttributeOfChild(font,
"name",
"val", out attribute))
447 fontStyle.Name = attribute;
449 if (ReaderUtils.GetAttributeOfChild(font,
"family",
"val", out attribute))
455 fontStyle.Family = FontFamilyValue.NotApplicable;
458 fontStyle.Family = FontFamilyValue.Roman;
461 fontStyle.Family = FontFamilyValue.Swiss;
464 fontStyle.Family = FontFamilyValue.Modern;
467 fontStyle.Family = FontFamilyValue.Script;
470 fontStyle.Family = FontFamilyValue.Decorative;
473 fontStyle.Family = FontFamilyValue.Reserved1;
476 fontStyle.Family = FontFamilyValue.Reserved2;
479 fontStyle.Family = FontFamilyValue.Reserved3;
482 fontStyle.Family = FontFamilyValue.Reserved4;
485 fontStyle.Family = FontFamilyValue.Reserved5;
488 fontStyle.Family = FontFamilyValue.Reserved6;
491 fontStyle.Family = FontFamilyValue.Reserved7;
494 fontStyle.Family = FontFamilyValue.Reserved8;
497 fontStyle.Family = FontFamilyValue.Reserved9;
501 if (ReaderUtils.GetAttributeOfChild(font,
"scheme",
"val", out attribute))
506 fontStyle.Scheme = SchemeValue.Major;
509 fontStyle.Scheme = SchemeValue.Minor;
513 if (ReaderUtils.GetAttributeOfChild(font,
"charset",
"val", out attribute))
518 fontStyle.Charset = CharsetValue.ANSI;
521 fontStyle.Charset = CharsetValue.Default;
524 fontStyle.Charset = CharsetValue.Symbols;
527 fontStyle.Charset = CharsetValue.Macintosh;
530 fontStyle.Charset = CharsetValue.JIS;
533 fontStyle.Charset = CharsetValue.Hangul;
536 fontStyle.Charset = CharsetValue.Johab;
539 fontStyle.Charset = CharsetValue.GBK;
542 fontStyle.Charset = CharsetValue.Big5;
545 fontStyle.Charset = CharsetValue.Greek;
548 fontStyle.Charset = CharsetValue.Turkish;
551 fontStyle.Charset = CharsetValue.Vietnamese;
554 fontStyle.Charset = CharsetValue.Hebrew;
557 fontStyle.Charset = CharsetValue.Arabic;
560 fontStyle.Charset = CharsetValue.Baltic;
563 fontStyle.Charset = CharsetValue.Russian;
566 fontStyle.Charset = CharsetValue.Thai;
569 fontStyle.Charset = CharsetValue.EasternEuropean;
572 fontStyle.Charset = CharsetValue.OEM;
575 fontStyle.Charset = CharsetValue.ApplicationDefined;
580 fontStyle.InternalID = this.styleReaderContainer.GetNextFontId();
581 this.styleReaderContainer.AddStyleComponent(fontStyle);
589 private void GetCellXfs(XmlNode node)
591 foreach (XmlNode childNode
in node.ChildNodes)
593 if (ReaderUtils.IsNode(childNode,
"xf"))
595 CellXf cellXfStyle =
new CellXf();
596 string attribute = ReaderUtils.GetAttribute(childNode,
"applyAlignment");
597 if (attribute !=
null)
599 int value = ParserUtils.ParseBinaryBool(attribute);
600 cellXfStyle.ForceApplyAlignment = value == 1;
602 XmlNode alignmentNode = ReaderUtils.GetChildNode(childNode,
"alignment");
603 if (alignmentNode !=
null)
605 attribute = ReaderUtils.GetAttribute(alignmentNode,
"shrinkToFit");
606 if (attribute !=
null)
608 int value = ParserUtils.ParseBinaryBool(attribute);
611 cellXfStyle.Alignment = TextBreakValue.ShrinkToFit;
614 attribute = ReaderUtils.GetAttribute(alignmentNode,
"wrapText");
615 if (attribute !=
null && attribute ==
"1")
617 cellXfStyle.Alignment = TextBreakValue.WrapText;
619 attribute = ReaderUtils.GetAttribute(alignmentNode,
"horizontal",
string.Empty);
620 cellXfStyle.HorizontalAlign = CellXf.GetHorizontalAlignEnum(attribute);
621 attribute = ReaderUtils.GetAttribute(alignmentNode,
"vertical",
string.Empty);
622 cellXfStyle.VerticalAlign = CellXf.GetVerticalAlignEnum(attribute);
623 attribute = ReaderUtils.GetAttribute(alignmentNode,
"indent");
624 if (attribute !=
null)
626 cellXfStyle.Indent = ParserUtils.ParseInt(attribute);
628 attribute = ReaderUtils.GetAttribute(alignmentNode,
"textRotation");
629 if (attribute !=
null)
631 int rotation = ParserUtils.ParseInt(attribute);
632 cellXfStyle.TextRotation = rotation > 90 ? 90 - rotation : rotation;
635 XmlNode protectionNode = ReaderUtils.GetChildNode(childNode,
"protection");
636 if (protectionNode !=
null)
638 attribute = ReaderUtils.GetAttribute(protectionNode,
"hidden");
639 if (attribute !=
null && attribute ==
"1")
641 cellXfStyle.Hidden =
true;
643 attribute = ReaderUtils.GetAttribute(protectionNode,
"locked");
644 if (attribute !=
null && attribute ==
"0")
646 cellXfStyle.Locked =
false;
651 cellXfStyle.InternalID = this.styleReaderContainer.GetNextCellXFId();
652 this.styleReaderContainer.AddStyleComponent(cellXfStyle);
654 Style style =
new Style();
658 hasId = ParserUtils.TryParseInt(ReaderUtils.GetAttribute(childNode,
"numFmtId"), out
id);
659 NumberFormat format = this.styleReaderContainer.GetNumberFormat(
id);
660 if (!hasId || format ==
null)
662 FormatNumber formatNumber;
663 NumberFormat.TryParseFormatNumber(
id, out formatNumber);
666 format =
new NumberFormat
668 Number = formatNumber,
671 this.styleReaderContainer.AddStyleComponent(format);
673 hasId = ParserUtils.TryParseInt(ReaderUtils.GetAttribute(childNode,
"borderId"), out
id);
674 Border border = this.styleReaderContainer.GetBorder(
id);
675 if (!hasId || border ==
null)
679 InternalID = this.styleReaderContainer.GetNextBorderId()
682 hasId = ParserUtils.TryParseInt(ReaderUtils.GetAttribute(childNode,
"fillId"), out
id);
683 Fill fill = this.styleReaderContainer.GetFill(
id);
684 if (!hasId || fill ==
null)
688 InternalID = this.styleReaderContainer.GetNextFillId()
691 hasId = ParserUtils.TryParseInt(ReaderUtils.GetAttribute(childNode,
"fontId"), out
id);
692 Font font = this.styleReaderContainer.GetFont(
id);
693 if (!hasId || font ==
null)
697 InternalID = this.styleReaderContainer.GetNextFontId()
702 style.CurrentNumberFormat = format;
703 style.CurrentBorder = border;
704 style.CurrentFill = fill;
705 style.CurrentFont = font;
706 style.CurrentCellXf = cellXfStyle;
707 style.InternalID = this.styleReaderContainer.GetNextStyleId();
709 this.styleReaderContainer.AddStyleComponent(style);
718 private void GetColors(XmlNode node)
720 foreach (XmlNode color
in node.ChildNodes)
722 XmlNode mruColor = ReaderUtils.GetChildNode(color,
"color");
723 if (color.Name.Equals(
"mruColors", StringComparison.Ordinal) && mruColor !=
null)
725 foreach (XmlNode value
in color.ChildNodes)
727 string attribute = ReaderUtils.GetAttribute(value,
"rgb");
728 if (attribute !=
null)
730 this.styleReaderContainer.AddMruColor(attribute);
743 private static string GetColor(XmlNode node,
string fallback)
745 XmlNode childNode = ReaderUtils.GetChildNode(node,
"color");
746 if (childNode !=
null)
748 string color = ReaderUtils.GetAttribute(childNode,
"rgb");