public static void addFontElement(XmlWriter _writer, FontData font, FontElementType type)
        {
            // begin font element
            if (type == FontElementType.NormalStyle)
            {
                _writer.WriteStartElement("font");
            }
            else if (type == FontElementType.String)
            {
                _writer.WriteStartElement("rPr");
            }

            // font size
            // NOTE: Excel 97, Excel 2000, Excel 2002, Office Excel 2003 and Office Excel 2007 can
            //   save out 0 for certain fonts. This is not valid in ECMA 376
            //
            if (font.size.ToPoints() != 0)
            {
                _writer.WriteStartElement("sz");
                _writer.WriteAttributeString("val", Convert.ToString(font.size.ToPoints(), CultureInfo.GetCultureInfo("en-US")));
                _writer.WriteEndElement();
            }

            // font name
            if (type == FontElementType.NormalStyle)
            {
                _writer.WriteStartElement("name");
            }
            else if (type == FontElementType.String)
            {
                _writer.WriteStartElement("rFont");
            }
            _writer.WriteAttributeString("val", font.fontName);
            _writer.WriteEndElement();
            // font family
            if (font.fontFamily != 0)
            {
                _writer.WriteStartElement("family");
                _writer.WriteAttributeString("val", ((int)font.fontFamily).ToString());
                _writer.WriteEndElement();
            }
            // font charset
            if (font.charSet != 0)
            {
                _writer.WriteStartElement("charset");
                _writer.WriteAttributeString("val", font.charSet.ToString());
                _writer.WriteEndElement();
            }

            // bool values
            if (font.isBold)
            {
                _writer.WriteElementString("b", "");
            }

            if (font.isItalic)
            {
                _writer.WriteElementString("i", "");
            }

            if (font.isOutline)
            {
                _writer.WriteElementString("outline", "");
            }

            if (font.isShadow)
            {
                _writer.WriteElementString("shadow", "");
            }

            if (font.isStrike)
            {
                _writer.WriteElementString("strike", "");
            }

            // underline style mapping
            if (font.uStyle != UnderlineStyle.none)
            {
                _writer.WriteStartElement("u");
                if (font.uStyle == UnderlineStyle.singleLine)
                {
                    _writer.WriteAttributeString("val", "single");
                }
                else if (font.uStyle == UnderlineStyle.doubleLine)
                {
                    _writer.WriteAttributeString("val", "double");
                }
                else
                {
                    _writer.WriteAttributeString("val", font.uStyle.ToString());
                }
                _writer.WriteEndElement();
            }

            if (font.vertAlign != SuperSubScriptStyle.none)
            {
                _writer.WriteStartElement("vertAlign");
                _writer.WriteAttributeString("val", font.vertAlign.ToString());
                _writer.WriteEndElement();
            }

            // colormapping
            StylesMapping.WriteRgbColor(_writer, StyleMappingHelper.convertColorIdToRGB(font.color));

            // end font element
            _writer.WriteEndElement();
        }
Beispiel #2
0
        /// <summary>
        /// The overload apply method
        /// Creates the Styles xml document
        /// </summary>
        /// <param name="sd">StyleData Object</param>
        public void Apply(StyleData sd)
        {
            this._writer.WriteStartDocument();
            this._writer.WriteStartElement("styleSheet", OpenXmlNamespaces.SpreadsheetML);


            // Format mapping
            this._writer.WriteStartElement("numFmts");
            this._writer.WriteAttributeString("count", sd.FormatDataList.Count.ToString());
            foreach (var format in sd.FormatDataList)
            {
                this._writer.WriteStartElement("numFmt");
                this._writer.WriteAttributeString("numFmtId", format.ifmt.ToString());
                this._writer.WriteAttributeString("formatCode", format.formatString);
                this._writer.WriteEndElement();
            }
            this._writer.WriteEndElement();



            /// Font Mapping
            //<fonts count="1">
            //<font>
            //<sz val="10"/>
            //<name val="Arial"/>
            //</font>
            //</fonts>
            this._writer.WriteStartElement("fonts");
            this._writer.WriteAttributeString("count", sd.FontDataList.Count.ToString());
            foreach (var font in sd.FontDataList)
            {
                ///
                StyleMappingHelper.addFontElement(this._writer, font, FontElementType.NormalStyle);
            }
            // write fonts end element
            this._writer.WriteEndElement();

            /// Fill Mapping
            //<fills count="2">
            //<fill>
            //<patternFill patternType="none"/>
            //</fill>
            this._writer.WriteStartElement("fills");
            this._writer.WriteAttributeString("count", sd.FillDataList.Count.ToString());
            foreach (var fd in sd.FillDataList)
            {
                this._writer.WriteStartElement("fill");
                this._writer.WriteStartElement("patternFill");
                this._writer.WriteAttributeString("patternType", StyleMappingHelper.getStringFromFillPatern(fd.Fillpatern));

                // foreground color
                WriteRgbForegroundColor(this._writer, StyleMappingHelper.convertColorIdToRGB(fd.IcvFore));

                // background color
                WriteRgbBackgroundColor(this._writer, StyleMappingHelper.convertColorIdToRGB(fd.IcvBack));

                this._writer.WriteEndElement();
                this._writer.WriteEndElement();
            }

            this._writer.WriteEndElement();


            /// Border Mapping
            //<borders count="1">
            //  <border>
            //      <left/>
            //      <right/>
            //      <top/>
            //      <bottom/>
            //      <diagonal/>
            //  </border>
            //</borders>
            this._writer.WriteStartElement("borders");
            this._writer.WriteAttributeString("count", sd.BorderDataList.Count.ToString());
            foreach (var borderData in sd.BorderDataList)
            {
                this._writer.WriteStartElement("border");

                // write diagonal settings
                if (borderData.diagonalValue == 1)
                {
                    this._writer.WriteAttributeString("diagonalDown", "1");
                }
                else if (borderData.diagonalValue == 2)
                {
                    this._writer.WriteAttributeString("diagonalUp", "1");
                }
                else if (borderData.diagonalValue == 3)
                {
                    this._writer.WriteAttributeString("diagonalDown", "1");
                    this._writer.WriteAttributeString("diagonalUp", "1");
                }
                else
                {
                    // do nothing !
                }


                string borderStyle = "";

                // left border
                this._writer.WriteStartElement("left");
                borderStyle = StyleMappingHelper.convertBorderStyle(borderData.left.style);
                if (!borderStyle.Equals("none"))
                {
                    this._writer.WriteAttributeString("style", borderStyle);
                    WriteRgbColor(this._writer, StyleMappingHelper.convertColorIdToRGB(borderData.left.colorId));
                }
                this._writer.WriteEndElement();

                // right border
                this._writer.WriteStartElement("right");
                borderStyle = StyleMappingHelper.convertBorderStyle(borderData.right.style);
                if (!borderStyle.Equals("none"))
                {
                    this._writer.WriteAttributeString("style", borderStyle);
                    WriteRgbColor(this._writer, StyleMappingHelper.convertColorIdToRGB(borderData.right.colorId));
                }
                this._writer.WriteEndElement();

                // top border
                this._writer.WriteStartElement("top");
                borderStyle = StyleMappingHelper.convertBorderStyle(borderData.top.style);
                if (!borderStyle.Equals("none"))
                {
                    this._writer.WriteAttributeString("style", borderStyle);
                    WriteRgbColor(this._writer, StyleMappingHelper.convertColorIdToRGB(borderData.top.colorId));
                }
                this._writer.WriteEndElement();

                // bottom border
                this._writer.WriteStartElement("bottom");
                borderStyle = StyleMappingHelper.convertBorderStyle(borderData.bottom.style);
                if (!borderStyle.Equals("none"))
                {
                    this._writer.WriteAttributeString("style", borderStyle);
                    WriteRgbColor(this._writer, StyleMappingHelper.convertColorIdToRGB(borderData.bottom.colorId));
                }
                this._writer.WriteEndElement();

                // diagonal border
                this._writer.WriteStartElement("diagonal");
                borderStyle = StyleMappingHelper.convertBorderStyle(borderData.diagonal.style);
                if (!borderStyle.Equals("none"))
                {
                    this._writer.WriteAttributeString("style", borderStyle);
                    WriteRgbColor(this._writer, StyleMappingHelper.convertColorIdToRGB(borderData.diagonal.colorId));
                }
                this._writer.WriteEndElement();

                this._writer.WriteEndElement(); // end border
            }
            this._writer.WriteEndElement();     // end borders

            ///<cellStyleXfs count="1">
            ///<xf numFmtId="0" fontId="0" fillId="0" borderId="0"/>
            ///</cellStyleXfs>
            // xfcellstyle mapping
            this._writer.WriteStartElement("cellStyleXfs");
            this._writer.WriteAttributeString("count", sd.XFCellStyleDataList.Count.ToString());
            foreach (var xfcellstyle in sd.XFCellStyleDataList)
            {
                this._writer.WriteStartElement("xf");
                this._writer.WriteAttributeString("numFmtId", xfcellstyle.ifmt.ToString());
                this._writer.WriteAttributeString("fontId", xfcellstyle.fontId.ToString());
                this._writer.WriteAttributeString("fillId", xfcellstyle.fillId.ToString());
                this._writer.WriteAttributeString("borderId", xfcellstyle.borderId.ToString());

                if (xfcellstyle.hasAlignment)
                {
                    StylesMapping.WriteCellAlignment(this._writer, xfcellstyle);
                }

                this._writer.WriteEndElement();
            }

            this._writer.WriteEndElement();



            ///<cellXfs count="6">
            ///<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
            // xfcell mapping
            this._writer.WriteStartElement("cellXfs");
            this._writer.WriteAttributeString("count", sd.XFCellDataList.Count.ToString());
            foreach (var xfcell in sd.XFCellDataList)
            {
                this._writer.WriteStartElement("xf");
                this._writer.WriteAttributeString("numFmtId", xfcell.ifmt.ToString());
                this._writer.WriteAttributeString("fontId", xfcell.fontId.ToString());
                this._writer.WriteAttributeString("fillId", xfcell.fillId.ToString());
                this._writer.WriteAttributeString("borderId", xfcell.borderId.ToString());
                this._writer.WriteAttributeString("xfId", xfcell.ixfParent.ToString());

                // applyNumberFormat="1"
                if (xfcell.ifmt != 0)
                {
                    this._writer.WriteAttributeString("applyNumberFormat", "1");
                }

                // applyBorder="1"
                if (xfcell.borderId != 0)
                {
                    this._writer.WriteAttributeString("applyBorder", "1");
                }

                // applyFill="1"
                if (xfcell.fillId != 0)
                {
                    this._writer.WriteAttributeString("applyFill", "1");
                }

                // applyFont="1"
                if (xfcell.fontId != 0)
                {
                    this._writer.WriteAttributeString("applyFont", "1");
                }
                if (xfcell.hasAlignment)
                {
                    StylesMapping.WriteCellAlignment(this._writer, xfcell);
                }

                this._writer.WriteEndElement();
            }

            this._writer.WriteEndElement();



            /// write cell styles
            /// <cellStyles count="1">
            /// <cellStyle name="Normal" xfId="0" builtinId="0"/>
            /// </cellStyles>
            ///
            this._writer.WriteStartElement("cellStyles");
            //_writer.WriteAttributeString("count", sd.StyleList.Count.ToString());
            foreach (var style in sd.StyleList)
            {
                this._writer.WriteStartElement("cellStyle");

                if (style.rgch != null)
                {
                    this._writer.WriteAttributeString("name", style.rgch);
                }
                // theres a bug with the zero based reading from the referenz id
                // so the style.ixfe value is reduzed by one
                if (style.ixfe != 0)
                {
                    this._writer.WriteAttributeString("xfId", (style.ixfe - 1).ToString());
                }
                else
                {
                    this._writer.WriteAttributeString("xfId", (style.ixfe).ToString());
                }
                this._writer.WriteAttributeString("builtinId", style.istyBuiltIn.ToString());

                this._writer.WriteEndElement();
            }

            this._writer.WriteEndElement();

            // close tags


            // write color table !!

            if (sd.ColorDataList != null && sd.ColorDataList.Count > 0)
            {
                this._writer.WriteStartElement("colors");

                this._writer.WriteStartElement("indexedColors");

                // <rgbColor rgb="00000000"/>
                foreach (var item in sd.ColorDataList)
                {
                    this._writer.WriteStartElement("rgbColor");
                    this._writer.WriteAttributeString("rgb", string.Format("{0:x2}", item.Alpha).ToString() + item.SixDigitHexCode);

                    this._writer.WriteEndElement();
                }


                this._writer.WriteEndElement();
                this._writer.WriteEndElement();
            }
            // end color

            this._writer.WriteEndElement();      // close
            this._writer.WriteEndDocument();

            // close writer
            this._writer.Flush();
        }