Example #1
0
        public int GetHyperlinkStyleIndex(SpreadsheetStyle style)
        {
            var styleIdentifier = style.GetIdentifier();

            if (_hyperlinkStyles.ContainsKey(styleIdentifier))
            {
                return(_hyperlinkStyles[styleIdentifier]);
            }
            var cellFormat = new CellFormat()
            {
                NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)1U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, ApplyNumberFormat = false, ApplyFill = false, ApplyBorder = false, ApplyAlignment = false, ApplyProtection = false
            };

            if (style.Alignment != null || style.Indent != 0 || style.VerticalAlignment != null)
            {
                var aligment = new Alignment();
                if (style.Alignment != null)
                {
                    aligment.Horizontal = style.Alignment.Value;
                }
                if (style.VerticalAlignment != null)
                {
                    aligment.Vertical = style.VerticalAlignment.Value;
                }
                if (style.Indent != 0)
                {
                    aligment.Indent = (UInt32)style.Indent;
                }
                cellFormat.AppendChild(aligment);
            }


            var styleIndex = _hyperlinkStyles[styleIdentifier] = (int)(UInt32)_stylesheet.CellFormats.Count;

            _stylesheet.CellFormats.AppendChild(cellFormat);
            _stylesheet.CellFormats.Count++;

            return(styleIndex);
        }
Example #2
0
        public int GetStyleIndex(SpreadsheetStyle style)
        {
            var styleIdentifier = style.GetIdentifier();

            if (_styles.ContainsKey(styleIdentifier))
            {
                return(_styles[styleIdentifier]);
            }

            var fontIndex = 0;

            if (style.Font != null)
            {
                var key = new FontKey(style.Font, style.ForegroundColor);
                if (_fonts.ContainsKey(key))
                {
                    fontIndex = _fonts[key];
                }
                else
                {
                    var newFont = new Font();
                    newFont.FontName = new FontName()
                    {
                        Val = style.Font.Name
                    };
                    newFont.FontSize = new FontSize()
                    {
                        Val = style.Font.Size
                    };
                    if (style.Font.Bold)
                    {
                        newFont.Bold = new Bold();
                    }
                    if (style.Font.Italic)
                    {
                        newFont.Italic = new Italic();
                    }
                    if (style.ForegroundColor != null)
                    {
                        newFont.Color = new Color()
                        {
                            Rgb = String.Format("{0:X2}{1:X2}{2:X2}{3:X2}", style.ForegroundColor.Value.A, style.ForegroundColor.Value.R, style.ForegroundColor.Value.G, style.ForegroundColor.Value.B)
                        };
                    }
                    _stylesheet.Fonts.AppendChild(newFont);
                    fontIndex = _fonts[key] = (int)((UInt32)_stylesheet.Fonts.Count);
                    _stylesheet.Fonts.Count++;
                }
            }
            var fillIndex = 0;

            if (style.BackgroundColor != null)
            {
                if (_fills.ContainsKey(style.BackgroundColor.Value))
                {
                    fillIndex = _fills[style.BackgroundColor.Value];
                }
                else
                {
                    var newFill = new PatternFill()
                    {
                        PatternType = PatternValues.Solid
                    };
                    newFill.ForegroundColor = new ForegroundColor()
                    {
                        Rgb = String.Format("{0:X2}{1:X2}{2:X2}{3:X2}", style.BackgroundColor.Value.A, style.BackgroundColor.Value.R, style.BackgroundColor.Value.G, style.BackgroundColor.Value.B)
                    };
                    newFill.BackgroundColor = new BackgroundColor()
                    {
                        Indexed = 64U
                    };
                    fillIndex = (int)(UInt32)_stylesheet.Fills.Count;
                    _fills[style.BackgroundColor.Value] = fillIndex;
                    _stylesheet.Fills.AppendChild(new Fill()
                    {
                        PatternFill = newFill
                    });
                    _stylesheet.Fills.Count++;
                }
            }
            var numberingId = 0;

            if (style.IsDate)
            {
                numberingId = 164;
            }

            var cellFormat = new CellFormat()
            {
                FormatId = 0, FontId = (UInt32)fontIndex, FillId = (UInt32)fillIndex, BorderId = 0, NumberFormatId = (UInt32)numberingId, ApplyFill = true
            };

            if (style.Alignment != null || style.VerticalAlignment != null || style.Indent != 0 || style.WrapText)
            {
                var aligment = new Alignment();
                if (style.Alignment != null)
                {
                    aligment.Horizontal = style.Alignment.Value;
                }
                if (style.VerticalAlignment != null)
                {
                    aligment.Vertical = style.VerticalAlignment.Value;
                }
                if (style.Indent != 0)
                {
                    aligment.Indent = (UInt32)style.Indent;
                }
                if (style.WrapText)
                {
                    aligment.WrapText = true;
                }
                cellFormat.AppendChild(aligment);
            }

            var styleIndex = _styles[styleIdentifier] = (int)(UInt32)_stylesheet.CellFormats.Count;

            _stylesheet.CellFormats.AppendChild(cellFormat);
            _stylesheet.CellFormats.Count++;

            return(styleIndex);
        }