zhao
2021-07-02 081df17b8cc4a6e7e4f4e1e1887f24810e3ec2f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* ====================================================================
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for Additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
==================================================================== */
 
namespace HH.WMS.Utils.NPOI.SS.Format
{
    using System;
 
    using HH.WMS.Utils.NPOI.SS.UserModel;
    using System.Text.RegularExpressions;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Drawing;
 
 
 
 
 
 
 
 
    /**
     * Format a value according to the standard Excel behavior.  This "standard" is
     * not explicitly documented by Microsoft, so the behavior is determined by
     * experimentation; see the tests.
     * 
     * An Excel format has up to four parts, Separated by semicolons.  Each part
     * specifies what to do with particular kinds of values, depending on the number
     * of parts given: 
     * 
     * - One part (example: <c>[Green]#.##</c>) 
     * If the value is a number, display according to this one part (example: green text,
     * with up to two decimal points). If the value is text, display it as is.
     * 
     * - Two parts (example: <c>[Green]#.##;[Red]#.##</c>) 
     * If the value is a positive number or zero, display according to the first part (example: green
     * text, with up to two decimal points); if it is a negative number, display
     * according to the second part (example: red text, with up to two decimal
     * points). If the value is text, display it as is. 
     * 
     * - Three parts (example: <c>[Green]#.##;[Black]#.##;[Red]#.##</c>) 
     * If the value is a positive number, display according to the first part (example: green text, with up to
     * two decimal points); if it is zero, display according to the second part
     * (example: black text, with up to two decimal points); if it is a negative
     * number, display according to the third part (example: red text, with up to
     * two decimal points). If the value is text, display it as is.
     * 
     * - Four parts (example: <c>[Green]#.##;[Black]#.##;[Red]#.##;[@]</c>)
     * If the value is a positive number, display according to the first part (example: green text,
     * with up to two decimal points); if it is zero, display according to the
     * second part (example: black text, with up to two decimal points); if it is a
     * negative number, display according to the third part (example: red text, with
     * up to two decimal points). If the value is text, display according to the
     * fourth part (example: text in the cell's usual color, with the text value
     * surround by brackets).
     * 
     * In Addition to these, there is a general format that is used when no format
     * is specified.  This formatting is presented by the {@link #GENERAL_FORMAT}
     * object.
     *
     * @author Ken Arnold, Industrious Media LLC
     */
    public class CellFormat
    {
        private String format;
        private CellFormatPart posNumFmt;
        private CellFormatPart zeroNumFmt;
        private CellFormatPart negNumFmt;
        private CellFormatPart textFmt;
 
        private static Regex ONE_PART = new Regex(CellFormatPart.FORMAT_PAT.ToString() + "(;|$)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
 
        private static CellFormatPart DEFAULT_TEXT_FORMAT =
                new CellFormatPart("@");
 
        private static CellFormat GENERAL_FORMAT = new GeneralCellFormat();
        /**
         * Format a value as it would be were no format specified.  This is also
         * used when the format specified is <tt>General</tt>.
         */
        public class GeneralCellFormat : CellFormat
        {
            public GeneralCellFormat()
                : base("General")
            {
            }
            public override CellFormatResult Apply(Object value)
            {
                String text;
                if (value == null)
                {
                    text = "";
                }
                else if (value.GetType().IsPrimitive/* is Number*/)
                {
                    throw new NotImplementedException();
                    //text = CellNumberFormatter.SIMPLE_NUMBER.Format(value);
                }
                else
                {
                    text = value.ToString();
                }
                return new CellFormatResult(true, text, Color.Empty);
            }
        }
 
        /** Maps a format string to its Parsed version for efficiencies sake. */
        private static Dictionary<String, CellFormat> formatCache =
                new Dictionary<String, CellFormat>();
 
        /**
         * Returns a {@link CellFormat} that applies the given format.  Two calls
         * with the same format may or may not return the same object.
         *
         * @param format The format.
         *
         * @return A {@link CellFormat} that applies the given format.
         */
        public static CellFormat GetInstance(String format)
        {
            CellFormat fmt = null;
            if (formatCache.ContainsKey(format))
                fmt = formatCache[format];
            if (fmt == null)
            {
                if (format.Equals("General"))
                    fmt = GENERAL_FORMAT;
                else
                    fmt = new CellFormat(format);
                formatCache.Add(format, fmt);
            }
            return fmt;
        }
 
        /**
         * Creates a new object.
         *
         * @param format The format.
         */
        private CellFormat(String format)
        {
            this.format = format;
            MatchCollection mc = ONE_PART.Matches(format);
            List<CellFormatPart> parts = new List<CellFormatPart>();
 
            //while (m.Success)
            foreach(Match m in mc)
            {
                try
                {
                    String valueDesc = m.Groups[0].Value;
 
                    // Strip out the semicolon if it's there
                    if (valueDesc.EndsWith(";"))
                        valueDesc = valueDesc.Substring(0, valueDesc.Length - 1);
 
                    parts.Add(new CellFormatPart(valueDesc));
                }
                catch (Exception)
                {
                    //CellFormatter.logger.Log(Level.WARNING,
                    //        "Invalid format: " + CellFormatter.Quote(m.Group()), e);
                    parts.Add(null);
                }
            }
 
            switch (parts.Count)
            {
                case 1:
                    posNumFmt = zeroNumFmt = negNumFmt = parts[(0)];
                    textFmt = DEFAULT_TEXT_FORMAT;
                    break;
                case 2:
                    posNumFmt = zeroNumFmt = parts[0];
                    negNumFmt = parts[1];
                    textFmt = DEFAULT_TEXT_FORMAT;
                    break;
                case 3:
                    posNumFmt = parts[0];
                    zeroNumFmt = parts[1];
                    negNumFmt = parts[2];
                    textFmt = DEFAULT_TEXT_FORMAT;
                    break;
                case 4:
                default:
                    posNumFmt = parts[0];
                    zeroNumFmt = parts[1];
                    negNumFmt = parts[2];
                    textFmt = parts[3];
                    break;
            }
        }
 
        /**
         * Returns the result of Applying the format to the given value.  If the
         * value is a number (a type of {@link Number} object), the correct number
         * format type is chosen; otherwise it is considered a text object.
         *
         * @param value The value
         *
         * @return The result, in a {@link CellFormatResult}.
         */
        public virtual CellFormatResult Apply(Object value)
        {
            if (value.GetType().IsPrimitive/* is Number*/)
            {
                double num = (double)value;
                double val = num;
                if (val > 0)
                    return posNumFmt.Apply(value);
                else if (val < 0)
                    return negNumFmt.Apply(-val);
                else
                    return zeroNumFmt.Apply(value);
            }
            else
            {
                return textFmt.Apply(value);
            }
        }
 
        /**
         * Fetches the appropriate value from the cell, and returns the result of
         * Applying it to the appropriate format.  For formula cells, the computed
         * value is what is used.
         *
         * @param c The cell.
         *
         * @return The result, in a {@link CellFormatResult}.
         */
        public CellFormatResult Apply(ICell c)
        {
            switch (UltimateType(c))
            {
                case CellType.BLANK:
                    return Apply("");
                case CellType.BOOLEAN:
                    return Apply(c.BooleanCellValue.ToString());
                case CellType.NUMERIC:
                    return Apply(c.NumericCellValue);
                case CellType.STRING:
                    return Apply(c.StringCellValue);
                default:
                    return Apply("?");
            }
        }
 
        /**
         * Uses the result of Applying this format to the value, Setting the text
         * and color of a label before returning the result.
         *
         * @param label The label to apply to.
         * @param value The value to Process.
         *
         * @return The result, in a {@link CellFormatResult}.
         */
        public CellFormatResult Apply(Label label, Object value)
        {
            CellFormatResult result = Apply(value);
            label.Text = (/*setter*/result.Text);
            if (result.TextColor != Color.Empty)
            {
                label.ForeColor = (/*setter*/result.TextColor);
            }
            return result;
        }
 
        /**
         * Fetches the appropriate value from the cell, and uses the result, Setting
         * the text and color of a label before returning the result.
         *
         * @param label The label to apply to.
         * @param c     The cell.
         *
         * @return The result, in a {@link CellFormatResult}.
         */
        public CellFormatResult Apply(Label label, ICell c)
        {
            switch (UltimateType(c))
            {
                case CellType.BLANK:
                    return Apply(label, "");
                case CellType.BOOLEAN:
                    return Apply(c.BooleanCellValue.ToString());
                case CellType.NUMERIC:
                    return Apply(label, c.NumericCellValue);
                case CellType.STRING:
                    return Apply(label, c.StringCellValue);
                default:
                    return Apply(label, "?");
            }
        }
 
        /**
         * Returns the ultimate cell type, following the results of formulas.  If
         * the cell is a {@link Cell#CELL_TYPE_FORMULA}, this returns the result of
         * {@link Cell#getCachedFormulaResultType()}.  Otherwise this returns the
         * result of {@link Cell#getCellType()}.
         *
         * @param cell The cell.
         *
         * @return The ultimate type of this cell.
         */
        public static CellType UltimateType(ICell cell)
        {
            CellType type = cell.CellType;
            if (type == CellType.FORMULA)
                return cell.CachedFormulaResultType;
            else
                return type;
        }
 
        /**
         * Returns <tt>true</tt> if the other object is a {@link CellFormat} object
         * with the same format.
         *
         * @param obj The other object.
         *
         * @return <tt>true</tt> if the two objects are Equal.
         */
 
        public override bool Equals(Object obj)
        {
            if (this == obj)
                return true;
            if (obj is CellFormat)
            {
                CellFormat that = (CellFormat)obj;
                return format.Equals(that.format);
            }
            return false;
        }
 
        /**
         * Returns a hash code for the format.
         *
         * @return A hash code for the format.
         */
 
        public override int GetHashCode()
        {
            return format.GetHashCode();
        }
 
        public override string ToString()
        {
            return format;
        }
    }
}