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