Esempio n. 1
0
        private bool WriteStyleNumberFormatXml(StreamWriter writer, NumberFormat format, bool writeEmpty, int id, int?styleId)
        {
            var convertedFormat = ConvertNumberFormat(format);

            if (convertedFormat.Item1 == -2)
            {
                writer.Write($"<numFmt numFmtId=\"{id}\" formatCode=\"{_XmlWriterHelper.EscapeAttribute(convertedFormat.Item2)}\"/>");
                if (styleId != null)
                {
                    _StyleIdNumberFormatIdMap[styleId.Value] = id;
                }
                return(true);
            }
            else if (convertedFormat.Item1 == -1)
            {
                if (writeEmpty)
                {
                    writer.Write($"<numFmt numFmtId=\"0\"/>");
                }

                if (styleId != null)
                {
                    _StyleIdNumberFormatIdMap[styleId.Value] = -1;
                }
                return(false);
            }
            else
            {
                if (styleId != null)
                {
                    _StyleIdNumberFormatIdMap[styleId.Value] = convertedFormat.Item1;
                }
                return(false);
            }
        }
Esempio n. 2
0
        private void WriteStyle(Style style, string id)
        {
            Write(string.Format(@"<ss:Style ss:ID=""{0}"">", id));

            if (style.Alignment != null)
            {
                var align = style.Alignment.Value;

                Write(@"<ss:Alignment"); // Opening tag

                string horz = null;
                switch (align.Horizontal)
                {
                default:
                case HorizontalAlignment.Automatic: break;

                case HorizontalAlignment.Left: horz = @"Left"; break;

                case HorizontalAlignment.Center: horz = @"Center"; break;

                case HorizontalAlignment.Right: horz = @"Right"; break;

                case HorizontalAlignment.Fill: horz = @"Fill"; break;

                case HorizontalAlignment.Justify: horz = @"Justify"; break;

                case HorizontalAlignment.CenterAcrossSelection: horz = @"CenterAcrossSelection"; break;

                case HorizontalAlignment.Distributed: horz = @"Distributed"; break;
                }

                if (horz != null)
                {
                    Write(string.Format(@" ss:Horizontal=""{0}""", horz));
                }

                if (align.Indent > 0) // 0 is default
                {
                    Write(string.Format(@" ss:Indent=""{0}""", align.Indent));
                }

                string readingOrder = null;
                switch (align.ReadingOrder)
                {
                default:
                case HorizontalReadingOrder.Context: break;

                case HorizontalReadingOrder.RightToLeft: readingOrder = @"RightToLeft"; break;

                case HorizontalReadingOrder.LeftToRight: readingOrder = @"LeftToRight"; break;
                }

                if (readingOrder != null)
                {
                    Write(string.Format(@" ss:ReadingOrder=""{0}""", readingOrder));
                }

                if (align.Rotate != 0.0) // 0 is default
                {
                    Write(string.Format(_Culture, @" ss:Rotate=""{0}""", align.Rotate));
                }

                if (align.ShrinkToFit) // FALSE is default
                {
                    Write(@" ss:ShrinkToFit=""1""");
                }

                string vert = null;
                switch (align.Vertical)
                {
                default:
                case VerticalAlignment.Automatic: break;

                case VerticalAlignment.Top: vert = @"Top"; break;

                case VerticalAlignment.Bottom: vert = @"Bottom"; break;

                case VerticalAlignment.Center: vert = @"Center"; break;

                case VerticalAlignment.Justify: vert = @"Justify"; break;

                case VerticalAlignment.Distributed: vert = @"Distributed"; break;

                case VerticalAlignment.JustifyDistributed: vert = @"JustifyDistributed"; break;
                }

                if (vert != null)
                {
                    Write(string.Format(@" ss:Vertical=""{0}""", vert));
                }

                if (align.VerticalText) // FALSE is default
                {
                    Write(@" ss:VerticalText=""1""");
                }

                if (align.WrapText) // FALSE is default
                {
                    Write(@" ss:WrapText=""1""");
                }

                Write("/>"); // Closing tag
            }

            if (style.NumberFormat.Type != NumberFormatType.None)
            {
                Write(string.Format(@"<ss:NumberFormat ss:Format=""{0}""/>", _XmlWriterHelper.EscapeAttribute(ConvertNumberFormat(style.NumberFormat))));
            }

            if (style.Borders != null && style.Borders.Count > 0)
            {
                Write(@"<ss:Borders>"); // Opening tag

                foreach (Border border in style.Borders)
                {
                    Write(@"<ss:Border"); // Opening tag

                    string Position = null;
                    switch (border.Position)
                    {
                    default:
                    case BorderPosition.Left:
                        Position = @"Left";
                        break;

                    case BorderPosition.Top:
                        Position = @"Top";
                        break;

                    case BorderPosition.Right:
                        Position = @"Right";
                        break;

                    case BorderPosition.Bottom:
                        Position = @"Bottom";
                        break;

                    case BorderPosition.DiagonalLeft:
                        Position = @"DiagonalLeft";
                        break;

                    case BorderPosition.DiagonalRight:
                        Position = @"DiagonalRight";
                        break;
                    }
                    Write($@" ss:Position=""{Position}"""); // Required

                    if (!ColorHelper.IsTransparentOrEmpty(border.Color))
                    {
                        Write($@" ss:Color=""#{ColorHelper.GetHexRgb(border.Color)}""");
                    }

                    string LineStyle = null;
                    switch (border.LineStyle)
                    {
                    default:
                    case BorderLineStyle.None:     // Default
                        break;

                    case BorderLineStyle.Continuous:
                        LineStyle = @"Continuous";
                        break;

                    case BorderLineStyle.Dash:
                        LineStyle = @"Dash";
                        break;

                    case BorderLineStyle.Dot:
                        LineStyle = @"Dot";
                        break;

                    case BorderLineStyle.DashDot:
                        LineStyle = @"DashDot";
                        break;

                    case BorderLineStyle.DashDotDot:
                        LineStyle = @"DashDotDot";
                        break;

                    case BorderLineStyle.SlantDashDot:
                        LineStyle = @"SlantDashDot";
                        break;

                    case BorderLineStyle.Double:
                        LineStyle = @"Double";
                        break;
                    }
                    if (LineStyle != null)
                    {
                        Write($@" ss:LineStyle=""{LineStyle}""");
                    }

                    if (border.Weight > 0.0) // 0 is default
                    {
                        Write(string.Format(_Culture, @" ss:Weight=""{0}""", border.Weight));
                    }

                    Write("/>"); // Closing tag
                }

                Write("</ss:Borders>"); // Closing tag
            }

            if (style.Fill != null)
            {
                var fill = style.Fill.Value;

                Write(@"<ss:Interior"); // Opening tag

                var bgColor = fill.Color;
                if (fill.Pattern == FillPattern.Solid && bgColor == Color.Empty)
                {
                    bgColor = fill.PatternColor;
                }

                if (!ColorHelper.IsTransparentOrEmpty(bgColor))
                {
                    Write($@" ss:Color=""#{ColorHelper.GetHexRgb(bgColor)}""");
                }

                string pattern = null;
                switch (fill.Pattern)
                {
                default:
                case FillPattern.None: break;

                case FillPattern.Solid: pattern = @"Solid"; break;

                case FillPattern.Gray75: pattern = @"Gray75"; break;

                case FillPattern.Gray50: pattern = @"Gray50"; break;

                case FillPattern.Gray25: pattern = @"Gray25"; break;

                case FillPattern.Gray125: pattern = @"Gray125"; break;

                case FillPattern.Gray0625: pattern = @"Gray0625"; break;

                case FillPattern.HorzStripe: pattern = @"HorzStripe"; break;

                case FillPattern.VertStripe: pattern = @"VertStripe"; break;

                case FillPattern.ReverseDiagStripe: pattern = @"ReverseDiagStripe"; break;

                case FillPattern.DiagCross: pattern = @"DiagCross"; break;

                case FillPattern.ThickDiagCross: pattern = @"ThickDiagCross"; break;

                case FillPattern.ThinHorzStripe: pattern = @"ThinHorzStripe"; break;

                case FillPattern.ThinVertStripe: pattern = @"ThinVertStripe"; break;

                case FillPattern.ThinReverseDiagStripe: pattern = @"ThinReverseDiagStripe"; break;

                case FillPattern.ThinDiagStripe: pattern = @"ThinDiagStripe"; break;

                case FillPattern.ThinHorzCross: pattern = @"ThinHorzCross"; break;

                case FillPattern.ThinDiagCross: pattern = @"ThinDiagCross"; break;
                }

                if (pattern != null)
                {
                    Write($@" ss:Pattern=""{pattern}""");
                }

                if (fill.Pattern != FillPattern.Solid)
                {
                    if (!ColorHelper.IsTransparentOrEmpty(fill.PatternColor))
                    {
                        Write($@" ss:PatternColor=""#{ColorHelper.GetHexRgb(fill.PatternColor)}""");
                    }
                }

                Write("/>"); // Closing tag
            }

            if (style.Font != null)
            {
                var font = style.Font.Value;

                Write("<ss:Font"); // Opening tag

                if (font.Bold)     // FALSE is default
                {
                    Write(@" ss:Bold=""1""");
                }

                if (!ColorHelper.IsTransparentOrEmpty(font.Color))
                {
                    Write($@" ss:Color=""#{ColorHelper.GetHexRgb(font.Color)}""");
                }

                if (font.Name != null && font.Name.Length > 0)
                {
                    Write($@" ss:FontName=""{_XmlWriterHelper.EscapeAttribute(font.Name)}""");
                }

                if (font.Italic) // FALSE is default
                {
                    Write(@" ss:Italic=""1""");
                }

                if (font.Outline) // FALSE is default
                {
                    Write(@" ss:Outline=""1""");
                }

                if (font.Shadow) // FALSE is default
                {
                    Write(@" ss:Shadow=""1""");
                }

                if (font.Size != 10.0) // 10 is default
                {
                    Write(string.Format(_Culture, @" ss:Size=""{0}""", font.Size));
                }

                if (font.StrikeThrough) // FALSE is default
                {
                    Write(@" ss:StrikeThrough=""1""");
                }

                string underline = null;
                switch (font.Underline)
                {
                default:
                case FontUnderline.None: break;

                case FontUnderline.Single: underline = @"Single"; break;

                case FontUnderline.Double: underline = @"Double"; break;

                case FontUnderline.SingleAccounting: underline = @"SingleAccounting"; break;

                case FontUnderline.DoubleAccounting: underline = @"DoubleAccounting"; break;
                }

                if (underline != null)
                {
                    Write($@" ss:Underline=""{underline}""");
                }

                string verticalAlign = null;
                switch (font.VerticalAlign)
                {
                default:
                case FontVerticalAlign.None: break;

                case FontVerticalAlign.Subscript: verticalAlign = @"Subscript"; break;

                case FontVerticalAlign.Superscript: verticalAlign = @"Superscript"; break;
                }

                if (verticalAlign != null)
                {
                    Write($@" ss:VerticalAlign=""{verticalAlign}""");
                }

                if (font.Charset != null && (int)font.Charset.Value > 0) // 0 is default
                {
                    Write(string.Format(_Culture, @" ss:CharSet=""{0}""", (int)font.Charset.Value));
                }

                string family = null;
                switch (font.Family)
                {
                default:
                case FontFamily.Automatic: break;

                case FontFamily.Decorative: family = @"Decorative"; break;

                case FontFamily.Modern: family = @"Modern"; break;

                case FontFamily.Roman: family = @"Roman"; break;

                case FontFamily.Script: family = @"Script"; break;

                case FontFamily.Swiss: family = @"Swiss"; break;
                }

                if (family != null)
                {
                    Write($@" ss:Family=""{family}""");
                }

                Write("/>"); // Closing tag
            }

            Write("</ss:Style>");
        }