Exemplo n.º 1
0
        /// <summary>
        /// The overload apply method
        /// Creates the Styles xml document
        /// </summary>
        /// <param name="sd">StyleData Object</param>
        public void Apply(StyleData sd)
        {
            _writer.WriteStartDocument();
            _writer.WriteStartElement("styleSheet", OpenXmlNamespaces.SpreadsheetML);


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



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

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

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

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

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

            _writer.WriteEndElement();


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

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


                string borderStyle = "";

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

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

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

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

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

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

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

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

                _writer.WriteEndElement();
            }

            _writer.WriteEndElement();



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

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

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

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

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

                _writer.WriteEndElement();
            }

            _writer.WriteEndElement();



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

                if (style.rgch != null)
                {
                    _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)
                {
                    _writer.WriteAttributeString("xfId", (style.ixfe - 1).ToString());
                }
                else
                {
                    _writer.WriteAttributeString("xfId", (style.ixfe).ToString());
                }
                _writer.WriteAttributeString("builtinId", style.istyBuiltIn.ToString());

                _writer.WriteEndElement();
            }

            _writer.WriteEndElement();

            // close tags


            // write color table !!

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

                _writer.WriteStartElement("indexedColors");

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

                    _writer.WriteEndElement();
                }


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

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

            // close writer
            _writer.Flush();
        }
Exemplo n.º 2
0
        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();
        }