zhao
2021-06-04 c7ec496f9e41c2227103b3ef776e4a3f91bce6b2
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
/* ====================================================================
   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.Extractor
{
    using System;
    using System.Text;
    using System.IO;
 
    using HH.WMS.Utils.NPOI.HSSF.UserModel;
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.POIFS.FileSystem;
    using HH.WMS.Utils.NPOI;
    using HH.WMS.Utils.NPOI.SS.Formula.Eval;
    using HH.WMS.Utils.NPOI.SS.UserModel;
 
    /// <summary>
    /// A text extractor for Excel files.
    /// Returns the textual content of the file, suitable for
    /// indexing by something like Lucene, but not really
    /// intended for display to the user.
    /// </summary>
    public class ExcelExtractor : POIOLE2TextExtractor
    {
        private HSSFWorkbook wb;
        private HSSFDataFormatter _formatter;
        private bool includeSheetNames = true;
        private bool formulasNotResults = false;
        private bool includeCellComments = false;
        private bool includeBlankCells = false;
        private bool includeHeaderFooter = true;
        /// <summary>
        /// Initializes a new instance of the <see cref="ExcelExtractor"/> class.
        /// </summary>
        /// <param name="wb">The wb.</param>
        public ExcelExtractor(HSSFWorkbook wb)
            : base(wb)
        {
            this.wb = wb;
            _formatter = new HSSFDataFormatter();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ExcelExtractor"/> class.
        /// </summary>
        /// <param name="fs">The fs.</param>
        public ExcelExtractor(POIFSFileSystem fs)
            : this(new HSSFWorkbook(fs))
        {
        }
 
        /// <summary>
        ///  Should header and footer be included? Default is true
        /// </summary>
        public bool IncludeHeaderFooter
        {
            get {
                return this.includeHeaderFooter;
            }
            set {
                this.includeHeaderFooter = value;
            }
        }
        /// <summary>
        /// Should sheet names be included? Default is true
        /// </summary>
        /// <value>if set to <c>true</c> [include sheet names].</value>
        public bool IncludeSheetNames
        {
            get {
                return this.includeSheetNames;
            }
            set
            {
                this.includeSheetNames = value;
            }
        }
        /// <summary>
        /// Should we return the formula itself, and not
        /// the result it produces? Default is false
        /// </summary>
        /// <value>if set to <c>true</c> [formulas not results].</value>
        public bool FormulasNotResults
        {
            get 
            {
                return this.formulasNotResults;
            }
            set
            {
                this.formulasNotResults = value;
            }
        }
        /// <summary>
        /// Should cell comments be included? Default is false
        /// </summary>
        /// <value>if set to <c>true</c> [include cell comments].</value>
        public bool IncludeCellComments
        {
            get
            {
                return this.includeCellComments;
            }
            set
            {
                this.includeCellComments = value;
            }
        }
        /// <summary>
        /// Should blank cells be output? Default is to only
        /// output cells that are present in the file and are
        /// non-blank.
        /// </summary>
        /// <value>if set to <c>true</c> [include blank cells].</value>
        public bool IncludeBlankCells
        {
            get
            {
                return this.includeBlankCells;
            }
            set
            {
                this.includeBlankCells = value;
            }
        }
 
        /// <summary>
        /// Retreives the text contents of the file
        /// </summary>
        /// <value>All the text from the document.</value>
        public override String Text
        {
            get
            {
                StringBuilder text = new StringBuilder();
 
                // We don't care about the differnce between
                //  null (missing) and blank cells
                wb.MissingCellPolicy = MissingCellPolicy.RETURN_BLANK_AS_NULL;
 
                // Process each sheet in turn
                for (int i = 0; i < wb.NumberOfSheets; i++)
                {
                    HSSFSheet sheet = (HSSFSheet)wb.GetSheetAt(i);
                    if (sheet == null) { continue; }
 
                    if (includeSheetNames)
                    {
                        String name = wb.GetSheetName(i);
                        if (name != null)
                        {
                            text.Append(name);
                            text.Append("\n");
                        }
                    }
 
                    // Header text, if there is any
                    if (sheet.Header != null && includeHeaderFooter)
                    {
                        text.Append(
                                ExtractHeaderFooter(sheet.Header)
                        );
                    }
 
                    int firstRow = sheet.FirstRowNum;
                    int lastRow = sheet.LastRowNum;
                    for (int j = firstRow; j <= lastRow; j++)
                    {
                        IRow row = sheet.GetRow(j);
                        if (row == null) { continue; }
 
                        // Check each cell in turn
                        int firstCell = row.FirstCellNum;
                        int lastCell = row.LastCellNum;
                        if (includeBlankCells)
                        {
                            firstCell = 0;
                        }
 
                        for (int k = firstCell; k < lastCell; k++)
                        {
                            ICell cell = row.GetCell(k);
                            bool outputContents = true;
 
                            if (cell == null)
                            {
                                // Only output if requested
                                outputContents = includeBlankCells;
                            }
                            else
                            {
                                switch (cell.CellType)
                                {
                                    case CellType.STRING:
                                        text.Append(cell.RichStringCellValue.String);
                                        break;
                                    case CellType.NUMERIC:
                                        // Note - we don't apply any formatting!
                                        //text.Append(cell.NumericCellValue);
                                        text.Append(_formatter.FormatCellValue(cell));
                                        break;
                                    case CellType.BOOLEAN:
                                        text.Append(cell.BooleanCellValue);
                                        break;
                                    case CellType.ERROR:
                                        text.Append(ErrorEval.GetText(cell.ErrorCellValue));
                                        break;
                                    case CellType.FORMULA:
                                        if (formulasNotResults)
                                        {
                                            text.Append(cell.CellFormula);
                                        }
                                        else
                                        {
                                            switch (cell.CachedFormulaResultType)
                                            {
                                                case CellType.STRING:
                                                    IRichTextString str = cell.RichStringCellValue;
                                                    if (str != null && str.Length > 0)
                                                    {
                                                        text.Append(str.ToString());
                                                    }
                                                    break;
                                                case CellType.NUMERIC:
                                                    //text.Append(cell.NumericCellValue);
                                                    HSSFCellStyle style = (HSSFCellStyle)cell.CellStyle;
                                                    if (style == null)
                                                    {
                                                        text.Append(cell.NumericCellValue);
                                                    }
                                                    else
                                                    {
                                                        text.Append(
                                                              _formatter.FormatRawCellContents(
                                                                    cell.NumericCellValue,
                                                                    style.DataFormat,
                                                                    style.GetDataFormatString()
                                                              )
                                                        );
                                                    }
                                                    break;
                                                case CellType.BOOLEAN:
                                                    text.Append(cell.BooleanCellValue);
                                                    break;
                                                case CellType.ERROR:
                                                    text.Append(ErrorEval.GetText(cell.ErrorCellValue));
                                                    break;
 
                                            }
                                        }
                                        break;
                                    default:
                                        throw new Exception("Unexpected cell type (" + cell.CellType + ")");
                                }
 
                                // Output the comment, if requested and exists
                                HH.WMS.Utils.NPOI.SS.UserModel.IComment comment = cell.CellComment;
                                if (includeCellComments && comment != null)
                                {
                                    // Replace any newlines with spaces, otherwise it
                                    //  breaks the output
                                    String commentText = comment.String.String.Replace('\n', ' ');
                                    text.Append(" Comment by " + comment.Author + ": " + commentText);
                                }
                            }
 
                            // Output a tab if we're not on the last cell
                            if (outputContents && k < (lastCell - 1))
                            {
                                text.Append("\t");
                            }
                        }
 
                        // Finish off the row
                        text.Append("\n");
                    }
 
                    // Finally Feader text, if there is any
                    if (sheet.Footer != null && includeHeaderFooter)
                    {
                        text.Append(
                                ExtractHeaderFooter(sheet.Footer)
                        );
                    }
                }
 
                return text.ToString();
            }
        }
 
        /// <summary>
        /// Extracts the header footer.
        /// </summary>
        /// <param name="hf">The header or footer</param>
        /// <returns></returns>
        private String ExtractHeaderFooter(HH.WMS.Utils.NPOI.SS.UserModel.IHeaderFooter hf)
        {
            StringBuilder text = new StringBuilder();
 
            if (hf.Left != null)
            {
                text.Append(hf.Left);
            }
            if (hf.Center != null)
            {
                if (text.Length > 0)
                    text.Append("\t");
                text.Append(hf.Center);
            }
            if (hf.Right != null)
            {
                if (text.Length > 0)
                    text.Append("\t");
                text.Append(hf.Right);
            }
            if (text.Length > 0)
                text.Append("\n");
 
            return text.ToString();
        }
    }
}