Example #1
0
        internal void Import(XmlReader reader)
        {
            foreach (XmlReaderAttributeItem xa in reader.GetAttributes())
            {
                switch (xa.LocalName)
                {
                case "Color":
                {
                    Color = XmlStyle.ExcelFormatToColor(xa.Value);
                    break;
                }

                case "PatternColor":
                {
                    PatternColor = XmlStyle.ExcelFormatToColor(xa.Value);
                    break;
                }

                case "Pattern":
                {
                    Pattern = ObjectExtensions.ParseEnum <Pattern>(xa.Value);
                    break;
                }
                }
            }
        }
Example #2
0
 internal void Export(XmlWriter writer)
 {
     writer.WriteStartElement("Font");
     writer.WriteAttributeString("ss", "FontName", null, Name);
     if (Size != 0)
     {
         writer.WriteAttributeString("ss", "Size", null, Size.ToString(CultureInfo.InvariantCulture));
     }
     writer.WriteAttributeString("ss", "Color", null, XmlStyle.ColorToExcelFormat(Color));
     if (Bold)
     {
         writer.WriteAttributeString("ss", "Bold", null, "1");
     }
     if (Italic)
     {
         writer.WriteAttributeString("ss", "Italic", null, "1");
     }
     if (Underline)
     {
         writer.WriteAttributeString("ss", "Underline", null, "Single");
     }
     if (Strikeout)
     {
         writer.WriteAttributeString("ss", "Strikeout", null, "1");
     }
     writer.WriteEndElement();
 }
Example #3
0
        internal object GetCellStyleProperty(StylePropertyAccessor getDelegate)
        {
            if (GetParentBook() == null)
            {
                return(getDelegate(Parent.FirstCell()));
            }
            XmlStyle style = GetParentBook().GetStyleByID(Parent.StyleID);

            return(getDelegate(style));
        }
Example #4
0
        private void ImportRow(XmlReader reader)
        {
            bool     isEmpty  = reader.IsEmptyElement;
            int      rowIndex = _Rows.Count;
            double   height   = -1;
            XmlStyle style    = null;
            bool     hidden   = false;

            foreach (XmlReaderAttributeItem xa in reader.GetAttributes())
            {
                if (xa.LocalName == "Height" && xa.HasValue)
                {
                    xa.Value.ParseToInt(out height);
                }
                if (xa.LocalName == "Index" && xa.HasValue)
                {
                    xa.Value.ParseToInt(out rowIndex);
                    rowIndex--;
                }
                if (xa.LocalName == "StyleID" && xa.HasValue)
                {
                    style = ParentBook.GetStyleByID(xa.Value);
                }
                if (xa.LocalName == "Hidden" && xa.HasValue)
                {
                    hidden = xa.Value == "1";
                }
            }
            Row row = GetRowByIndex(rowIndex);

            row.Hidden = hidden;
            if (height != -1)
            {
                row.Height = height;
            }
            if (style != null)
            {
                row.Style = style;
            }
            if (isEmpty)
            {
                return;
            }
            while (reader.Read() && !(reader.Name == "Row" && reader.NodeType == XmlNodeType.EndElement))
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "Cell")
                    {
                        ImportCell(reader, row);
                    }
                }
            }
        }
Example #5
0
        internal void Import(XmlReader reader)
        {
            foreach (XmlReaderAttributeItem xa in reader.GetAttributes())
            {
                switch (xa.LocalName)
                {
                case "FontName":
                {
                    Name = xa.Value;
                    break;
                }

                case "Size":
                {
                    int i;
                    if (xa.Value.ParseToInt(out i))
                    {
                        Size = i;
                    }
                    break;
                }

                case "Color":
                {
                    Color = XmlStyle.ExcelFormatToColor(xa.Value);
                    break;
                }

                case "Bold":
                {
                    Bold = xa.Value == "1" ? true : false;
                    break;
                }

                case "Italic":
                {
                    Italic = xa.Value == "1" ? true : false;
                    break;
                }

                case "Underline":
                {
                    Underline = xa.Value == "Single" ? true : false;
                    break;
                }

                case "Strikeout":
                {
                    Strikeout = xa.Value == "1" ? true : false;
                    break;
                }
                }
            }
        }
Example #6
0
 private void ImportStyles(XmlReader reader)
 {
     while (reader.Read() && !(reader.Name == "Styles" && reader.NodeType == XmlNodeType.EndElement))
     {
         XmlStyle style = XmlStyle.Import(reader);
         if (style != null)
         {
             Styles.Add(style);
         }
     }
 }
Example #7
0
 private void ExportBorder(XmlWriter writer, string border)
 {
     writer.WriteStartElement("Border");
     writer.WriteAttributeString("ss", "Position", null, border);
     writer.WriteAttributeString("ss", "LineStyle", null, LineStyle.ToString());
     writer.WriteAttributeString("ss", "Weight", null, Weight.ToString(CultureInfo.InvariantCulture));
     if (Color != Color.Black)
     {
         writer.WriteAttributeString("ss", "Color", null, XmlStyle.ColorToExcelFormat(Color));
     }
     writer.WriteEndElement();
 }
Example #8
0
        private void Initialize()
        {
            Properties  = new DocumentProperties();
            Styles      = new List <XmlStyle>();
            _Worksheets = new List <Worksheet>();
            NamedRanges = new List <NamedRange>();
            XmlStyle style = new XmlStyle();

            style.ID = "Default";
            style.Alignment.Vertical = VerticalAlignment.Bottom;
            Styles.Add(style);
        }
Example #9
0
 internal bool CheckForMatch(XmlStyle style)
 {
     if (style == null)
     {
         return(false);
     }
     if (_Font.CheckForMatch(style._Font) && _Alignment.CheckForMatch(style._Alignment) && _Interior.CheckForMatch(style._Interior) && _Border.CheckForMatch(style._Border) && DisplayFormat == style.DisplayFormat)
     {
         return(true);
     }
     return(false);
 }
Example #10
0
 internal void SetCellStyleProperty(StylePropertyAccessor setDelegate)
 {
     if (GetParentBook() == null)
     {
         Parent.IterateAndApply(cell => setDelegate(cell));
     }
     else
     {
         XmlStyle style = new XmlStyle(GetParentBook().GetStyleByID(Parent.StyleID));
         setDelegate(style);
         Parent.StyleID = GetParentBook().AddStyle(style);
     }
 }
Example #11
0
 /// <summary>
 /// Creates a new instance from another instance of XmlStyle
 /// </summary>
 /// <param name="style">Instance to copy</param>
 public XmlStyle(XmlStyle style)
 {
     if (style == null)
     {
         Initialize();
         SetDefaults();
         return;
     }
     ID            = "";
     _Font         = new FontOptions(style._Font);
     _Interior     = new InteriorOptions(style._Interior);
     _Alignment    = new AlignmentOptions(style._Alignment);
     _Border       = new BorderOptions(style._Border);
     DisplayFormat = style.DisplayFormat;
 }
Example #12
0
 internal void Export(XmlWriter writer)
 {
     if (Color != Color.Empty || PatternColor != Color.Empty)
     {
         writer.WriteStartElement("Interior");
         if (Color != Color.Empty)
         {
             writer.WriteAttributeString("ss", "Color", null, XmlStyle.ColorToExcelFormat(Color));
         }
         if (PatternColor != Color.Empty)
         {
             writer.WriteAttributeString("ss", "PatternColor", null, XmlStyle.ColorToExcelFormat(PatternColor));
         }
         writer.WriteAttributeString("ss", "Pattern", null, Pattern.ToString());
         writer.WriteEndElement();
     }
 }
Example #13
0
        internal string AddStyle(XmlStyle style)
        {
            XmlStyle oldStyle = FindStyle(style);

            if (oldStyle == null)
            {
                int iterator = Styles.Count;
                style.ID = String.Format(CultureInfo.InvariantCulture, "S{0:00}", iterator++);
                while (HasStyleID(style.ID))
                {
                    style.ID = String.Format(CultureInfo.InvariantCulture, "S{0:00}", iterator++);
                }
                Styles.Add(style);
                return(style.ID);
            }
            return(oldStyle.ID);
        }
Example #14
0
        private void ImportTable(XmlReader reader)
        {
            if (reader.IsEmptyElement)
            {
                return;
            }
            int column = 0;

            while (reader.Read() && !(reader.Name == "Table" && reader.NodeType == XmlNodeType.EndElement))
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                    case "Column":
                    {
                        double   width  = 0;
                        bool     hidden = false;
                        int      span   = 1;
                        XmlStyle style  = null;
                        foreach (XmlReaderAttributeItem xa in reader.GetAttributes())
                        {
                            if (xa.LocalName == "Width" && xa.HasValue)
                            {
                                double d;
                                if (xa.Value.ParseToInt(out d))
                                {
                                    width = d;
                                }
                            }
                            if (xa.LocalName == "Hidden" && xa.HasValue)
                            {
                                hidden = xa.Value == "1";
                            }
                            if (xa.LocalName == "Index" && xa.HasValue)
                            {
                                xa.Value.ParseToInt(out column);
                            }
                            if (xa.LocalName == "Span" && xa.HasValue)
                            {
                                xa.Value.ParseToInt(out span);
                            }
                            if (xa.LocalName == "StyleID" && xa.HasValue)
                            {
                                style = ParentBook.GetStyleByID(xa.Value);
                            }
                        }
                        for (int i = 1; i <= span; i++)
                        {
                            Columns(column).Width  = width;
                            Columns(column).Hidden = hidden;
                            if (style != null)
                            {
                                Columns(column).Style = style;
                            }
                            column++;
                        }
                        break;
                    }

                    case "Row":
                    {
                        ImportRow(reader);
                        break;
                    }
                    }
                }
            }
        }
Example #15
0
        private void ImportCell(XmlReader reader, Row row)
        {
            bool     isEmpty     = reader.IsEmptyElement;
            int      cellIndex   = row._Cells.Count;
            int      mergeDown   = 0;
            int      mergeAcross = 0;
            XmlStyle style       = null;
            string   formula     = "";
            string   reference   = "";

            foreach (XmlReaderAttributeItem xa in reader.GetAttributes())
            {
                if (xa.LocalName == "Index" && xa.HasValue)
                {
                    xa.Value.ParseToInt(out cellIndex);
                    cellIndex--;
                }
                if (xa.LocalName == "StyleID" && xa.HasValue)
                {
                    style = ParentBook.GetStyleByID(xa.Value);
                }
                if (xa.LocalName == "HRef" && xa.HasValue)
                {
                    reference = xa.Value;
                }
                if (xa.LocalName == "Formula" && xa.HasValue)
                {
                    formula = xa.Value;
                }
                if (xa.LocalName == "MergeAcross" && xa.HasValue)
                {
                    xa.Value.ParseToInt(out mergeAcross);
                }
                if (xa.LocalName == "MergeDown" && xa.HasValue)
                {
                    xa.Value.ParseToInt(out mergeDown);
                }
            }
            Cell cell = Cells(cellIndex, row.RowIndex);

            if (style != null)
            {
                cell.Style = style;
            }
            if (!reference.IsNullOrEmpty())
            {
                cell.HRef = reference;
            }
            if (!formula.IsNullOrEmpty())
            {
                FormulaParser.Parse(cell, formula);
                return;
            }
            if (isEmpty)
            {
                return;
            }
            if (mergeDown > 0 || mergeAcross > 0)
            {
                cell.MergeStart = true;
                Range range = new Range(cell, Cells(cellIndex + mergeAcross, row.RowIndex + mergeDown));
                _MergedCells.Add(range);
                cell.ColumnSpan = range.ColumnCount;
                cell.RowSpan    = range.RowCount;
            }
            while (reader.Read() && !(reader.Name == "Cell" && reader.NodeType == XmlNodeType.EndElement))
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "Data")
                    {
                        ImportCellData(reader, cell);
                    }
                    else if (reader.Name == "Comment")
                    {
                        ImportCellComment(reader, cell);
                    }
                }
            }
        }
Example #16
0
 internal XmlStyle FindStyle(XmlStyle style)
 {
     return(Styles.Find(xs => xs.CheckForMatch(style)));
 }
Example #17
0
        internal static XmlStyle Import(XmlReader reader)
        {
            XmlStyle style            = new XmlStyle();
            bool     isEmpty          = reader.IsEmptyElement;
            XmlReaderAttributeItem xa = reader.GetSingleAttribute("ID");

            if (xa != null)
            {
                style.ID = xa.Value;
            }
            if (isEmpty)
            {
                return(xa == null ? null : style);
            }
            while (reader.Read() && !(reader.Name == "Style" && reader.NodeType == XmlNodeType.EndElement))
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                    case "Font":
                    {
                        style._Font.Import(reader);
                        break;
                    }

                    case "Alignment":
                    {
                        style._Alignment.Import(reader);
                        break;
                    }

                    case "Interior":
                    {
                        style._Interior.Import(reader);
                        break;
                    }

                    case "Borders":
                    {
                        style._Border.Import(reader);
                        break;
                    }

                    case "NumberFormat":
                    {
                        XmlReaderAttributeItem nfAttr = reader.GetSingleAttribute("Format");
                        if (nfAttr != null)
                        {
                            string format = nfAttr.Value;
                            switch (format)
                            {
                            case "Short Date":
                            {
                                style.DisplayFormat = DisplayFormatType.ShortDate;
                                break;
                            }

                            case "General Date":
                            {
                                style.DisplayFormat = DisplayFormatType.GeneralDate;
                                break;
                            }

                            case "@":
                            {
                                style.DisplayFormat = DisplayFormatType.Text;
                                break;
                            }

                            default:
                            {
                                if (format == DateTimeFormatInfo.CurrentInfo.LongDatePattern)
                                {
                                    style.DisplayFormat = DisplayFormatType.LongDate;
                                }
                                string timeFormat = DateTimeFormatInfo.CurrentInfo.LongTimePattern;
                                if (timeFormat.Contains("t"))
                                {
                                    timeFormat = timeFormat.Replace("t", "AM/PM");
                                }
                                if (timeFormat.Contains("tt"))
                                {
                                    timeFormat = timeFormat.Replace("tt", "AM/PM");
                                }
                                if (format == timeFormat)
                                {
                                    style.DisplayFormat = DisplayFormatType.Time;
                                }
                                try
                                {
                                    style.DisplayFormat = ObjectExtensions.ParseEnum <DisplayFormatType>(format);
                                }
                                catch (ArgumentException)
                                {
                                    if (format.IsNullOrEmpty())
                                    {
                                        style.DisplayFormat = DisplayFormatType.None;
                                    }
                                    else
                                    {
                                        style.DisplayFormat      = DisplayFormatType.Custom;
                                        style.CustomFormatString = format;
                                    }
                                }
                                break;
                            }
                            }
                        }
                        break;
                    }
                    }
                }
            }
            return(style);
        }