zhao
2021-06-11 98186752629a7bd38965418af84db382d90b9c07
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
/* ====================================================================
   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.HSSF.EventUserModel
{
    using System;
    using System.Collections;
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.HSSF.UserModel;
    using System.Collections.Generic;
    using HH.WMS.Utils.NPOI.SS.UserModel;
    using System.Globalization;
 
    /**
     * A proxy HSSFListener that keeps track of the document
     *  formatting records, and provides an easy way to look
     *  up the format strings used by cells from their ids.
     */
    public class FormatTrackingHSSFListener : HSSFListener
    {
        private HSSFListener childListener;
        private Dictionary<int, FormatRecord> customFormatRecords = new Dictionary<int, FormatRecord>();
        private DataFormatter formatter = new DataFormatter();
        private List<ExtendedFormatRecord> xfRecords = new List<ExtendedFormatRecord>();
 
        public FormatTrackingHSSFListener(HSSFListener childListener)
        {
            this.childListener = childListener;
        }
 
        public int NumberOfCustomFormats
        {
            get
            {
                return customFormatRecords.Count;
            }
        }
        public int NumberOfExtendedFormats
        {
            get
            {
                return xfRecords.Count;
            }
        }
        /**
         * Process this record ourselves, and then
         *  pass it on to our child listener
         */
        public void ProcessRecord(Record record)
        {
            // Handle it ourselves
            ProcessRecordInternally(record);
 
            // Now pass on to our child
            childListener.ProcessRecord(record);
        }
 
        /**
         * Process the record ourselves, but do not
         *  pass it on to the child Listener.
         * @param record
         */
        public void ProcessRecordInternally(Record record)
        {
            if (record is FormatRecord)
            {
                FormatRecord fr = (FormatRecord)record;
                customFormatRecords[fr.IndexCode] = fr;
            }
            else if (record is ExtendedFormatRecord)
            {
                ExtendedFormatRecord xr = (ExtendedFormatRecord)record;
                xfRecords.Add(xr);
            }
        }
        /**
         * Formats the given numeric of date Cell's contents
         *  as a String, in as close as we can to the way 
         *  that Excel would do so.
         * Uses the various format records to manage this.
         * 
         * TODO - move this to a central class in such a
         *  way that hssf.usermodel can make use of it too
         */
        public String FormatNumberDateCell(CellValueRecordInterface cell)
        {
            double value;
            if (cell is NumberRecord)
            {
                value = ((NumberRecord)cell).Value;
            }
            else if (cell is FormulaRecord)
            {
                value = ((FormulaRecord)cell).Value;
            }
            else
            {
                throw new ArgumentException("Unsupported CellValue Record passed in " + cell);
            }
 
            // Get the built in format, if there is one
            int formatIndex = GetFormatIndex(cell);
            String formatString = GetFormatString(cell);
 
            if (formatString == null)
            {
                return value.ToString(CultureInfo.InvariantCulture);
            }
            else
            {
                // Format, using the nice new
                //  HSSFDataFormatter to do the work for us 
                return formatter.FormatRawCellContents(value, formatIndex, formatString);
            }
        }
        /**
         * Returns the format string, eg $##.##, for the
         *  given number format index.
         */
        public String GetFormatString(int formatIndex)
        {
            String format = null;
            if (formatIndex >= HSSFDataFormat.NumberOfBuiltinBuiltinFormats)
            {
                FormatRecord tfr = (FormatRecord)customFormatRecords[formatIndex];
                if (tfr == null)
                {
                    Console.Error.WriteLine("Requested format at index " + formatIndex + ", but it wasn't found");
                }
                else
                {
                    format = tfr.FormatString;
                }
            }
            else
            {
                format = HSSFDataFormat.GetBuiltinFormat((short)formatIndex);
            }
            return format;
        }
 
        /**
         * Returns the format string, eg $##.##, used
         *  by your cell 
         */
        public String GetFormatString(CellValueRecordInterface cell)
        {
            int formatIndex = GetFormatIndex(cell);
            if (formatIndex == -1)
            {
                // Not found
                return null;
            }
            return GetFormatString(formatIndex);
        }
 
        /**
         * Returns the index of the format string, used by your cell,
         *  or -1 if none found
         */
        public int GetFormatIndex(CellValueRecordInterface cell)
        {
            ExtendedFormatRecord xfr = (ExtendedFormatRecord)
                xfRecords[cell.XFIndex];
            if (xfr == null)
            {
                Console.Error.WriteLine("Cell " + cell.Row + "," + cell.Column + " uses XF with index " + cell.XFIndex + ", but we don't have that");
                return -1;
            }
            return xfr.FormatIndex;
        }
    }
}