Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Style/Dxf/ExcelDxfStyle.cs @ 13397

Last change on this file since 13397 was 12074, checked in by sraggl, 9 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 7.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5using System.Text;
6using System.Xml;
7using System.Drawing;
8
9namespace OfficeOpenXml.Style.Dxf
10{
11    public class ExcelDxfStyleConditionalFormatting : DxfStyleBase<ExcelDxfStyleConditionalFormatting>
12    {
13        XmlHelperInstance _helper;
14        internal ExcelDxfStyleConditionalFormatting(XmlNamespaceManager nameSpaceManager, XmlNode topNode, ExcelStyles styles) : base(styles)
15        {
16            NumberFormat = new ExcelDxfNumberFormat(_styles);
17            Font = new ExcelDxfFontBase(_styles);
18            Border = new ExcelDxfBorderBase(_styles);
19            Fill = new ExcelDxfFill(_styles);
20            if (topNode != null)
21            {
22                _helper = new XmlHelperInstance(nameSpaceManager, topNode);
23                NumberFormat.NumFmtID = _helper.GetXmlNodeInt("d:numFmt/@numFmtId");
24                NumberFormat.Format = _helper.GetXmlNodeString("d:numFmt/@formatCode");
25                if (NumberFormat.NumFmtID < 164 && string.IsNullOrEmpty(NumberFormat.Format))
26                {
27                    NumberFormat.Format = ExcelNumberFormat.GetFromBuildInFromID(NumberFormat.NumFmtID);
28                }
29
30                Font.Bold = _helper.GetXmlNodeBoolNullable("d:font/d:b/@val");
31                Font.Italic = _helper.GetXmlNodeBoolNullable("d:font/d:i/@val");
32                Font.Strike = _helper.GetXmlNodeBoolNullable("d:font/d:strike");
33                Font.Underline = GetUnderLineEnum(_helper.GetXmlNodeString("d:font/d:u/@val"));
34                Font.Color = GetColor(_helper, "d:font/d:color");
35
36                Border.Left = GetBorderItem(_helper, "d:border/d:left");
37                Border.Right = GetBorderItem(_helper, "d:border/d:right");
38                Border.Bottom = GetBorderItem(_helper, "d:border/d:bottom");
39                Border.Top = GetBorderItem(_helper, "d:border/d:top");
40
41                Fill.PatternType = GetPatternTypeEnum(_helper.GetXmlNodeString("d:fill/d:patternFill/@patternType"));
42                Fill.BackgroundColor = GetColor(_helper, "d:fill/d:patternFill/d:bgColor/");
43                Fill.PatternColor = GetColor(_helper, "d:fill/d:patternFill/d:fgColor/");
44            }
45            else
46            {
47                _helper = new XmlHelperInstance(nameSpaceManager);
48            }
49            _helper.SchemaNodeOrder = new string[] { "font", "numFmt", "fill", "border" };
50        }
51        private ExcelDxfBorderItem GetBorderItem(XmlHelperInstance helper, string path)
52        {
53            ExcelDxfBorderItem bi = new ExcelDxfBorderItem(_styles);
54            bi.Style = GetBorderStyleEnum(helper.GetXmlNodeString(path+"/@style"));
55            bi.Color = GetColor(helper, path+"/d:color");
56            return bi;
57        }
58        private ExcelBorderStyle GetBorderStyleEnum(string style)
59        {
60            if (style == "") return ExcelBorderStyle.None;
61            string sInStyle = style.Substring(0, 1).ToUpper(CultureInfo.InvariantCulture) + style.Substring(1, style.Length - 1);
62            try
63            {
64                return (ExcelBorderStyle)Enum.Parse(typeof(ExcelBorderStyle), sInStyle);
65            }
66            catch
67            {
68                return ExcelBorderStyle.None;
69            }
70
71        }
72        private ExcelFillStyle GetPatternTypeEnum(string patternType)
73        {
74            if (patternType == "") return ExcelFillStyle.None;
75            patternType = patternType.Substring(0, 1).ToUpper(CultureInfo.InvariantCulture) + patternType.Substring(1, patternType.Length - 1);
76            try
77            {
78                return (ExcelFillStyle)Enum.Parse(typeof(ExcelFillStyle), patternType);
79            }
80            catch
81            {
82                return ExcelFillStyle.None;
83            }
84        }
85        private ExcelDxfColor GetColor(XmlHelperInstance helper, string path)
86        {           
87            ExcelDxfColor ret = new ExcelDxfColor(_styles);
88            ret.Theme = helper.GetXmlNodeIntNull(path + "/@theme");
89            ret.Index = helper.GetXmlNodeIntNull(path + "/@indexed");
90            string rgb=helper.GetXmlNodeString(path + "/@rgb");
91            if(rgb!="")
92            {
93                ret.Color = Color.FromArgb( int.Parse(rgb.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier),
94                                            int.Parse(rgb.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier),
95                                            int.Parse(rgb.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier),
96                                            int.Parse(rgb.Substring(6, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
97            }
98            ret.Auto = helper.GetXmlNodeBoolNullable(path + "/@auto");
99            ret.Tint = helper.GetXmlNodeDoubleNull(path + "/@tint");
100            return ret;
101        }
102        private ExcelUnderLineType? GetUnderLineEnum(string value)
103        {
104            switch(value.ToLower(CultureInfo.InvariantCulture))
105            {
106                case "single":
107                    return ExcelUnderLineType.Single;
108                case "double":
109                    return ExcelUnderLineType.Double;
110                case "singleaccounting":
111                    return ExcelUnderLineType.SingleAccounting;
112                case "doubleaccounting":
113                    return ExcelUnderLineType.DoubleAccounting;
114                default:
115                    return null;
116            }
117        }
118        internal int DxfId { get; set; }
119        public ExcelDxfFontBase Font { get; set; }
120        public ExcelDxfNumberFormat NumberFormat { get; set; }
121        public ExcelDxfFill Fill { get; set; }
122        public ExcelDxfBorderBase Border { get; set; }
123        protected internal override string Id
124        {
125            get
126            {
127                return NumberFormat.Id + Font.Id + Border.Id + Fill.Id +
128                    (AllowChange ? "" : DxfId.ToString());//If allowchange is false we add the dxfID to ensure it's not used when conditional formatting is updated);
129            }
130        }
131        protected internal override ExcelDxfStyleConditionalFormatting Clone()
132        {
133         var s=new ExcelDxfStyleConditionalFormatting(_helper.NameSpaceManager, null, _styles);
134           s.Font = Font.Clone();
135           s.NumberFormat = NumberFormat.Clone();
136           s.Fill = Fill.Clone();
137           s.Border = Border.Clone();
138           return s;
139        }
140
141        protected internal override void CreateNodes(XmlHelper helper, string path)
142        {
143            if(Font.HasValue) Font.CreateNodes(helper, "d:font");
144            if (NumberFormat.HasValue) NumberFormat.CreateNodes(helper, "d:numFmt");           
145            if (Fill.HasValue) Fill.CreateNodes(helper, "d:fill");
146            if (Border.HasValue) Border.CreateNodes(helper, "d:border");
147        }
148        protected internal override bool HasValue
149        {
150            get { return Font.HasValue || NumberFormat.HasValue || Fill.HasValue || Border.HasValue; }
151        }
152    }
153}
Note: See TracBrowser for help on using the repository browser.