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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
/* ====================================================================
   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.UserModel
{
 
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using HH.WMS.Utils.NPOI.DDF;
    using HH.WMS.Utils.NPOI.HSSF.Model;
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.POIFS.FileSystem;
    using HH.WMS.Utils.NPOI.SS.Formula;
    using HH.WMS.Utils.NPOI.SS.Formula.PTG;
    using HH.WMS.Utils.NPOI.SS.Formula.Udf;
    using HH.WMS.Utils.NPOI.SS.UserModel;
    using HH.WMS.Utils.NPOI.SS.Util;
    using HH.WMS.Utils.NPOI.Util;
    using System.Globalization;
    using System.Security.Cryptography;
 
 
    /// <summary>
    /// High level representation of a workbook.  This is the first object most users
    /// will construct whether they are reading or writing a workbook.  It is also the
    /// top level object for creating new sheets/etc.
    /// </summary>
    /// @author  Andrew C. Oliver (acoliver at apache dot org)
    /// @author  Glen Stampoultzis (glens at apache.org)
    /// @author  Shawn Laubach (slaubach at apache dot org)
    [Serializable]
    public class HSSFWorkbook : POIDocument, IWorkbook
    {
        //private static int DEBUG = POILogger.DEBUG;
        private const int MAX_ROW = 0xFFFF;
        private const short MAX_COLUMN = (short)0x00FF;
 
        /**
         * The maximum number of cell styles in a .xls workbook.
         * The 'official' limit is 4,000, but POI allows a slightly larger number.
         * This extra delta takes into account built-in styles that are automatically
         * created for new workbooks
         *
         * See http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP005199291.aspx
         */
        private const int MAX_STYLES = 4030;
 
        /**
         * used for compile-time performance/memory optimization.  This determines the
         * initial capacity for the sheet collection.  Its currently Set to 3.
         * Changing it in this release will decrease performance
         * since you're never allowed to have more or less than three sheets!
         */
 
        public const int INITIAL_CAPACITY = 3;
 
        /**
         * this Is the reference to the low level Workbook object
         */
 
        private InternalWorkbook workbook;
 
        /**
         * this holds the HSSFSheet objects attached to this workbook
         */
 
        protected List<HSSFSheet> _sheets;
 
        /**
         * this holds the HSSFName objects attached to this workbook
         */
 
        private List<HSSFName> names;
 
        /**
         * holds whether or not to preserve other nodes in the POIFS.  Used
         * for macros and embedded objects.
         */
        private bool preserveNodes;
 
        /**
         * Used to keep track of the data formatter so that all
         * CreateDataFormatter calls return the same one for a given
         * book.  This Ensures that updates from one places Is visible
         * someplace else.
         */
        private HSSFDataFormat formatter;
 
        /**
         * this holds the HSSFFont objects attached to this workbook.
         * We only create these from the low level records as required.
         */
        private Hashtable fonts;
 
 
 
        //private static POILogger log = POILogFactory.GetLogger(typeof(HSSFWorkbook));
 
        public HH.WMS.Utils.NPOI.SS.UserModel.ICreationHelper GetCreationHelper()
        {
            return new HSSFCreationHelper(this);
        }
 
        /// <summary>
        /// Totals the sizes of all sheet records and eventually serializes them
        /// </summary>
        private class SheetRecordCollector : HH.WMS.Utils.NPOI.HSSF.Record.Aggregates.RecordVisitor,IDisposable
        {
 
            private ArrayList _list;
            private int _totalSize;
 
            public SheetRecordCollector()
            {
                _totalSize = 0;
                _list = new ArrayList(128);
            }
            public int TotalSize
            {
                get
                {
                    return _totalSize;
                }
            }
            public void VisitRecord(Record r)
            {
                _list.Add(r);
                _totalSize += r.RecordSize;
            }
            public int Serialize(int offset, byte[] data)
            {
                int result = 0;
                int nRecs = _list.Count;
                for (int i = 0; i < nRecs; i++)
                {
                    Record rec = (Record)_list[i];
                    result += rec.Serialize(offset + result, data);
                }
                return result;
            }
            public void Dispose()
            {
                //this._list = null;
            }
 
        }
        public static HSSFWorkbook Create(InternalWorkbook book)
        {
            return new HSSFWorkbook(book);
        }
 
        /// <summary>
        /// Creates new HSSFWorkbook from scratch (start here!)
        /// </summary>
        public HSSFWorkbook()
            : this(InternalWorkbook.CreateWorkbook())
        {
 
        }
 
        public HSSFWorkbook(InternalWorkbook book)
            : base((DirectoryNode)null)
        {
 
            workbook = book;
            _sheets = new List<HSSFSheet>(INITIAL_CAPACITY);
            names = new List<HSSFName>(INITIAL_CAPACITY);
        }
 
        public HSSFWorkbook(POIFSFileSystem fs)
            : this(fs, true)
        {
 
        }
 
        /// <summary>
        /// given a POI POIFSFileSystem object, Read in its Workbook and populate the high and
        /// low level models.  If you're Reading in a workbook...start here.
        /// </summary>
        /// <param name="fs">the POI filesystem that Contains the Workbook stream.</param>
        /// <param name="preserveNodes">whether to preseve other nodes, such as
        /// macros.  This takes more memory, so only say yes if you
        /// need to. If Set, will store all of the POIFSFileSystem
        /// in memory</param>
        public HSSFWorkbook(POIFSFileSystem fs, bool preserveNodes)
            : this(fs.Root, fs, preserveNodes)
        {
 
        }
 
        /**
         * Normally, the Workbook will be in a POIFS Stream
         * called "Workbook". However, some weird XLS generators use "WORKBOOK"
         */
        private static String[] WORKBOOK_DIR_ENTRY_NAMES = {
            "Workbook", // as per BIFF8 spec
            "WORKBOOK",
        };
 
 
        private static String GetWorkbookDirEntryName(DirectoryNode directory)
        {
 
            String[] potentialNames = WORKBOOK_DIR_ENTRY_NAMES;
            for (int i = 0; i < potentialNames.Length; i++)
            {
                String wbName = potentialNames[i];
                try
                {
                    directory.GetEntry(wbName);
                    return wbName;
                }
                catch (FileNotFoundException)
                {
                    // continue - to try other options
                }
            }
 
            // Check for previous version of file format
            try
            {
                directory.GetEntry("Book");
                throw new OldExcelFormatException("The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. "
                        + "POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)");
            }
            catch (FileNotFoundException)
            {
                // fall through
            }
 
            throw new ArgumentException("The supplied POIFSFileSystem does not contain a BIFF8 'Workbook' entry. "
                + "Is it really an excel file?");
        }
 
        /// <summary>
        /// given a POI POIFSFileSystem object, and a specific directory
        /// within it, Read in its Workbook and populate the high and
        /// low level models.  If you're Reading in a workbook...start here.
        /// </summary>
        /// <param name="directory">the POI filesystem directory to Process from</param>
        /// <param name="fs">the POI filesystem that Contains the Workbook stream.</param>
        /// <param name="preserveNodes">whether to preseve other nodes, such as
        /// macros.  This takes more memory, so only say yes if you
        /// need to. If Set, will store all of the POIFSFileSystem
        /// in memory</param>
        public HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, bool preserveNodes)
            : this(directory, preserveNodes)
        {
        }
            /**
     * given a POI POIFSFileSystem object, and a specific directory
     *  within it, read in its Workbook and populate the high and
     *  low level models.  If you're reading in a workbook...start here.
     *
     * @param directory the POI filesystem directory to process from
     * @param preserveNodes whether to preseve other nodes, such as
     *        macros.  This takes more memory, so only say yes if you
     *        need to. If set, will store all of the POIFSFileSystem
     *        in memory
     * @see org.apache.poi.poifs.filesystem.POIFSFileSystem
     * @exception IOException if the stream cannot be read
     */
        public HSSFWorkbook(DirectoryNode directory, bool preserveNodes):base(directory)
        {
 
            String workbookName = GetWorkbookDirEntryName(directory);
 
            this.preserveNodes = preserveNodes;
 
            // If we're not preserving nodes, don't track the
            //  POIFS any more
            if (!preserveNodes)
            {
                this.directory = null;
            }
 
            _sheets = new List<HSSFSheet>(INITIAL_CAPACITY);
            names = new List<HSSFName>(INITIAL_CAPACITY);
 
            // Grab the data from the workbook stream, however
            //  it happens to be spelled.
            Stream stream = directory.CreatePOIFSDocumentReader(workbookName);
 
 
            List<Record> records = RecordFactory.CreateRecords(stream);
 
            workbook = InternalWorkbook.CreateWorkbook(records);
            SetPropertiesFromWorkbook(workbook);
            int recOffset = workbook.NumRecords;
 
            // Convert all LabelRecord records to LabelSSTRecord
            ConvertLabelRecords(records, recOffset);
            RecordStream rs = new RecordStream(records, recOffset);
            while (rs.HasNext())
            {
                InternalSheet sheet = InternalSheet.CreateSheet(rs);
                _sheets.Add(new HSSFSheet(this, sheet));
            }
 
            for (int i = 0; i < workbook.NumNames; ++i)
            {
                NameRecord nameRecord = workbook.GetNameRecord(i);
                HSSFName name = new HSSFName(this, workbook.GetNameRecord(i), workbook.GetNameCommentRecord(nameRecord));
                names.Add(name);
            }
        }
 
        public HSSFWorkbook(Stream s)
            : this(s, true)
        {
 
        }
 
        /**
         * Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around your
         * inputstream.
         *
         * @param s  the POI filesystem that Contains the Workbook stream.
         * @param preserveNodes whether to preseve other nodes, such as
         *        macros.  This takes more memory, so only say yes if you
         *        need to.
         * @see org.apache.poi.poifs.filesystem.POIFSFileSystem
         * @see #HSSFWorkbook(POIFSFileSystem)
         * @exception IOException if the stream cannot be Read
         */
 
        public HSSFWorkbook(Stream s, bool preserveNodes)
            : this(new POIFSFileSystem(s), preserveNodes)
        {
 
        }
 
        /**
         * used internally to Set the workbook properties.
         */
 
        private void SetPropertiesFromWorkbook(InternalWorkbook book)
        {
            this.workbook = book;
 
            // none currently
        }
 
        /// <summary>
        /// This is basically a kludge to deal with the now obsolete Label records.  If
        /// you have to read in a sheet that contains Label records, be aware that the rest
        /// of the API doesn't deal with them, the low level structure only provides Read-only
        /// semi-immutable structures (the Sets are there for interface conformance with NO
        /// impelmentation).  In short, you need to call this function passing it a reference
        /// to the Workbook object.  All labels will be converted to LabelSST records and their
        /// contained strings will be written to the Shared String tabel (SSTRecord) within
        /// the Workbook.
        /// </summary>
        /// <param name="records">The records.</param>
        /// <param name="offset">The offset.</param>
        private void ConvertLabelRecords(IList records, int offset)
        {
            //if (log.Check(POILogger.DEBUG))
            //    log.Log(POILogger.DEBUG, "ConvertLabelRecords called");
            for (int k = offset; k < records.Count; k++)
            {
                Record rec = (Record)records[k];
 
                if (rec.Sid == LabelRecord.sid)
                {
                    LabelRecord oldrec = (LabelRecord)rec;
 
                    records.RemoveAt(k);
                    LabelSSTRecord newrec = new LabelSSTRecord();
                    int stringid =
                        workbook.AddSSTString(new UnicodeString(oldrec.Value));
 
                    newrec.Row = (oldrec.Row);
                    newrec.Column = (oldrec.Column);
                    newrec.XFIndex = (oldrec.XFIndex);
                    newrec.SSTIndex = (stringid);
                    records.Insert(k, newrec);
                }
            }
            //if (log.Check(POILogger.DEBUG))
            //    log.Log(POILogger.DEBUG, "ConvertLabelRecords exit");
        }
        [NonSerialized]
        private HH.WMS.Utils.NPOI.SS.UserModel.MissingCellPolicy missingCellPolicy = HH.WMS.Utils.NPOI.SS.UserModel.MissingCellPolicy.RETURN_NULL_AND_BLANK;
        /// <summary>
        /// Retrieves the current policy on what to do when
        /// getting missing or blank cells from a row.
        /// The default is to return blank and null cells.
        /// </summary>
        /// <value>The missing cell policy.</value>
        public HH.WMS.Utils.NPOI.SS.UserModel.MissingCellPolicy MissingCellPolicy
        {
            get { return missingCellPolicy; }
            set { this.missingCellPolicy = value; }
        }
 
        /// <summary>
        /// Sets the order of appearance for a given sheet.
        /// </summary>
        /// <param name="sheetname">the name of the sheet to reorder</param>
        /// <param name="pos">the position that we want to Insert the sheet into (0 based)</param>
        public void SetSheetOrder(String sheetname, int pos)
        {
            int oldSheetIndex = GetSheetIndex(sheetname);
            HSSFSheet sheet = (HSSFSheet)this.GetSheet(sheetname);
            _sheets.RemoveAt(oldSheetIndex);
            _sheets.Insert(pos, sheet);
            workbook.SetSheetOrder(sheetname, pos);
 
            FormulaShifter shifter = FormulaShifter.CreateForSheetShift(oldSheetIndex, pos);
            foreach (HSSFSheet sheet1 in _sheets)
            {
                sheet1.Sheet.UpdateFormulasAfterCellShift(shifter, /* not used */ -1);
            }
 
            workbook.UpdateNamesAfterCellShift(shifter);
        }
 
        /// <summary>
        /// Validates the index of the sheet.
        /// </summary>
        /// <param name="index">The index.</param>
        private void ValidateSheetIndex(int index)
        {
            int lastSheetIx = _sheets.Count - 1;
            if (index < 0 || index > lastSheetIx)
            {
                throw new ArgumentException("Sheet index ("
                        + index + ") is out of range (0.." + lastSheetIx + ")");
            }
        }
        /** Test only. Do not use */
        public void InsertChartRecord()
        {
            int loc = workbook.FindFirstRecordLocBySid(SSTRecord.sid);
            byte[] data = {
               (byte)0x0F, (byte)0x00, (byte)0x00, (byte)0xF0, (byte)0x52,
               (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
               (byte)0x06, (byte)0xF0, (byte)0x18, (byte)0x00, (byte)0x00,
               (byte)0x00, (byte)0x01, (byte)0x08, (byte)0x00, (byte)0x00,
               (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02,
               (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00,
               (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00,
               (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00,
               (byte)0x33, (byte)0x00, (byte)0x0B, (byte)0xF0, (byte)0x12,
               (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xBF, (byte)0x00,
               (byte)0x08, (byte)0x00, (byte)0x08, (byte)0x00, (byte)0x81,
               (byte)0x01, (byte)0x09, (byte)0x00, (byte)0x00, (byte)0x08,
               (byte)0xC0, (byte)0x01, (byte)0x40, (byte)0x00, (byte)0x00,
               (byte)0x08, (byte)0x40, (byte)0x00, (byte)0x1E, (byte)0xF1,
               (byte)0x10, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0D,
               (byte)0x00, (byte)0x00, (byte)0x08, (byte)0x0C, (byte)0x00,
               (byte)0x00, (byte)0x08, (byte)0x17, (byte)0x00, (byte)0x00,
               (byte)0x08, (byte)0xF7, (byte)0x00, (byte)0x00, (byte)0x10,
            };
            UnknownRecord r = new UnknownRecord((short)0x00EB, data);
            workbook.Records.Insert(loc, r);
        }
        /// <summary>
        /// Selects a single sheet. This may be different to
        /// the 'active' sheet (which Is the sheet with focus).
        /// </summary>
        /// <param name="index">The index.</param>
        public void SetSelectedTab(int index)
        {
 
            ValidateSheetIndex(index);
            int nSheets = _sheets.Count;
            for (int i = 0; i < nSheets; i++)
            {
                GetSheetAt(i).IsSelected = (i == index);
            }
            workbook.WindowOne.NumSelectedTabs = ((short)1);
        }
        /// <summary>
        /// Sets the selected tabs.
        /// </summary>
        /// <param name="indexes">The indexes.</param>
        public void SetSelectedTabs(int[] indexes)
        {
 
            for (int i = 0; i < indexes.Length; i++)
            {
                ValidateSheetIndex(indexes[i]);
            }
            int nSheets = _sheets.Count;
            for (int i = 0; i < nSheets; i++)
            {
                bool bSelect = false;
                for (int j = 0; j < indexes.Length; j++)
                {
                    if (indexes[j] == i)
                    {
                        bSelect = true;
                        break;
                    }
 
                }
                GetSheetAt(i).IsSelected = (bSelect);
            }
            workbook.WindowOne.NumSelectedTabs = ((short)indexes.Length);
        }
 
        /// <summary>
        /// Gets or sets the tab whose data is actually seen when the sheet is opened.
        /// This may be different from the "selected sheet" since excel seems to
        /// allow you to show the data of one sheet when another Is seen "selected"
        /// in the tabs (at the bottom).
        /// </summary>
        public int ActiveSheetIndex
        {
            get
            {
                return workbook.WindowOne.ActiveSheetIndex;
            }
        }
        public void SetActiveSheet(int index)
        {
 
            ValidateSheetIndex(index);
            int nSheets = _sheets.Count;
            for (int i = 0; i < nSheets; i++)
            {
                GetSheetAt(i).SetActive(i == index);
            }
            workbook.WindowOne.ActiveSheetIndex = (index);
        }
 
        /// <summary>
        /// Gets or sets the first tab that is displayed in the list of tabs
        /// in excel.
        /// </summary>
        public int FirstVisibleTab
        {
            get { return workbook.WindowOne.FirstVisibleTab; }
            set { workbook.WindowOne.FirstVisibleTab = value;}
        }
        [Obsolete("Misleading name - use GetFirstVisibleTab() ")]
        public short DisplayedTab
        {
            get { return (short)FirstVisibleTab; }
        }
 
        /**
         * @deprecated POI will now properly handle Unicode strings without
         * forceing an encoding
         */
        public const byte ENCODING_COMPRESSED_UNICODE = 0;
        /**
         * @deprecated POI will now properly handle Unicode strings without
         * forceing an encoding
         */
        public const byte ENCODING_UTF_16 = 1;
 
 
        /// <summary>
        /// Set the sheet name.
        /// </summary>
        /// <param name="sheetIx">The sheet number(0 based).</param>
        /// <param name="name">The name.</param>
        public void SetSheetName(int sheetIx, String name)
        {
            if (name == null)
            {
                throw new ArgumentException("sheetName must not be null");
            }
 
            if (workbook.ContainsSheetName(name, sheetIx))
            {
                throw new ArgumentException("The workbook already contains a sheet with this name");
            }
            ValidateSheetIndex(sheetIx);
            workbook.SetSheetName(sheetIx, name);
        }
 
        /// <summary>
        /// Get the sheet name
        /// </summary>
        /// <param name="sheetIx">The sheet index.</param>
        /// <returns>Sheet name</returns>
        public String GetSheetName(int sheetIx)
        {
            ValidateSheetIndex(sheetIx);
            return workbook.GetSheetName(sheetIx);
        }
 
        /// <summary>
        /// Check whether a sheet is hidden
        /// </summary>
        /// <param name="sheetIx">The sheet index.</param>
        /// <returns>
        ///     <c>true</c> if sheet is hidden; otherwise, <c>false</c>.
        /// </returns>
        public bool IsSheetHidden(int sheetIx)
        {
            ValidateSheetIndex(sheetIx);
            return workbook.IsSheetHidden(sheetIx);
        }
        /// <summary>
        /// Check whether a sheet is very hidden.
        /// This is different from the normal
        /// hidden status
        /// </summary>
        /// <param name="sheetIx">The sheet index.</param>
        /// <returns>
        ///     <c>true</c> if sheet is very hidden; otherwise, <c>false</c>.
        /// </returns>
        public bool IsSheetVeryHidden(int sheetIx)
        {
            ValidateSheetIndex(sheetIx);
            return workbook.IsSheetVeryHidden(sheetIx);
        }
        /// <summary>
        /// Hide or Unhide a sheet
        /// </summary>
        /// <param name="sheetIx">The sheet index</param>
        /// <param name="hidden">True to mark the sheet as hidden, false otherwise</param>
        public void SetSheetHidden(int sheetIx, SheetState hidden)
        {
            ValidateSheetIndex(sheetIx);
            WorkbookUtil.ValidateSheetState(hidden);
            workbook.SetSheetHidden(sheetIx, (int)hidden);
        }
        /// <summary>
        /// Hide or unhide a sheet.
        /// </summary>
        /// <param name="sheetIx">The sheet number</param>
        /// <param name="hidden">0 for not hidden, 1 for hidden, 2 for very hidden</param>
        public void SetSheetHidden(int sheetIx, int hidden)
        {
            ValidateSheetIndex(sheetIx);
            workbook.SetSheetHidden(sheetIx, hidden);
        }
        public void SetSheetHidden(int sheetIx, bool hidden)
        {
            ValidateSheetIndex(sheetIx);
            workbook.SetSheetHidden(sheetIx, hidden);
        }
        /// <summary>
        /// Returns the index of the sheet by his name
        /// </summary>
        /// <param name="name">the sheet name</param>
        /// <returns>index of the sheet (0 based)</returns>
        public int GetSheetIndex(String name)
        {
            return workbook.GetSheetIndex(name);
        }
 
        /// <summary>
        /// Returns the index of the given sheet
        /// </summary>
        /// <param name="sheet">the sheet to look up</param>
        /// <returns>index of the sheet (0 based).-1
        ///  if not found </returns>
        public int GetSheetIndex(ISheet sheet)
        {
            for (int i = 0; i < _sheets.Count; i++)
            {
                if (_sheets[i] == sheet)
                {
                    return i;
                }
            }
            return -1;
        }
 
        /// <summary>
        /// Returns the external sheet index of the sheet
        /// with the given internal index, creating one
        /// if needed.
        /// Used by some of the more obscure formula and
        /// named range things.
        /// </summary>
        /// <param name="internalSheetIndex">Index of the internal sheet.</param>
        /// <returns></returns>
        public int GetExternalSheetIndex(int internalSheetIndex)
        {
            return workbook.CheckExternSheet(internalSheetIndex);
        }
 
        /// <summary>
        /// Create an HSSFSheet for this HSSFWorkbook, Adds it to the sheets and returns
        /// the high level representation.  Use this to Create new sheets.
        /// </summary>
        /// <returns>HSSFSheet representing the new sheet.</returns>
        public ISheet CreateSheet()
        {
            HSSFSheet sheet = new HSSFSheet(this);
 
            _sheets.Add(sheet);
            workbook.SetSheetName(_sheets.Count - 1, "Sheet" + (_sheets.Count - 1));
            bool IsOnlySheet = _sheets.Count == 1;
            sheet.IsSelected = (IsOnlySheet);
            sheet.IsActive = (IsOnlySheet);
            return sheet;
        }
 
        /// <summary>
        /// Create an HSSFSheet from an existing sheet in the HSSFWorkbook.
        /// </summary>
        /// <param name="sheetIndex">the sheet index</param>
        /// <returns>HSSFSheet representing the Cloned sheet.</returns>
        public ISheet CloneSheet(int sheetIndex)
        {
            ValidateSheetIndex(sheetIndex);
            HSSFSheet srcSheet = (HSSFSheet)_sheets[sheetIndex];
            String srcName = workbook.GetSheetName(sheetIndex);
            HSSFSheet clonedSheet = srcSheet.CloneSheet(this);
            clonedSheet.IsSelected = (false);
            clonedSheet.IsActive = (false);
 
            String name = GetUniqueSheetName(srcName);
            int newSheetIndex = _sheets.Count;
            _sheets.Add(clonedSheet);
            workbook.SetSheetName(newSheetIndex, name);
 
            // Check this sheet has an autofilter, (which has a built-in NameRecord at workbook level)
            int filterDbNameIndex = FindExistingBuiltinNameRecordIdx(sheetIndex, NameRecord.BUILTIN_FILTER_DB);
            if (filterDbNameIndex != -1)
            {
                NameRecord newNameRecord = workbook.CloneFilter(filterDbNameIndex, newSheetIndex);
                HSSFName newName = new HSSFName(this, newNameRecord);
                names.Add(newName);
            }
            // TODO - maybe same logic required for other/all built-in name records
            workbook.CloneDrawings(clonedSheet.Sheet);
            return clonedSheet;
        }
        /// <summary>
        /// Gets the name of the unique sheet.
        /// </summary>
        /// <param name="srcName">Name of the SRC.</param>
        /// <returns></returns>
        private String GetUniqueSheetName(String srcName)
        {
            int uniqueIndex = 2;
            String baseName = srcName;
            int bracketPos = srcName.LastIndexOf('(');
            if (bracketPos > 0 && srcName.EndsWith(")", StringComparison.Ordinal))
            {
                String suffix = srcName.Substring(bracketPos + 1, srcName.Length - bracketPos - 2);
                try
                {
                    uniqueIndex = Int32.Parse(suffix.Trim(), CultureInfo.InvariantCulture);
                    uniqueIndex++;
                    baseName = srcName.Substring(0, bracketPos).Trim();
                }
                catch (FormatException)
                {
                    // contents of brackets not numeric
                }
            }
            while (true)
            {
                // Try and find the next sheet name that is unique
                String index = (uniqueIndex++).ToString(CultureInfo.CurrentCulture);
                String name;
                if (baseName.Length + index.Length + 2 < 31)
                {
                    name = baseName + " (" + index + ")";
                }
                else
                {
                    name = baseName.Substring(0, 31 - index.Length - 2) + "(" + index + ")";
                }
 
                //If the sheet name is unique, then set it otherwise move on to the next number.
                if (workbook.GetSheetIndex(name) == -1)
                {
                    return name;
                }
            }
        }
        /// <summary>
        /// Create an HSSFSheet for this HSSFWorkbook, Adds it to the sheets and
        /// returns the high level representation. Use this to Create new sheets.
        /// </summary>
        /// <param name="sheetname">sheetname to set for the sheet.</param>
        /// <returns>HSSFSheet representing the new sheet.</returns>
        public ISheet CreateSheet(String sheetname)
        {
            if (sheetname == null)
            {
                throw new ArgumentException("sheetName must not be null");
            }
 
            if (workbook.ContainsSheetName(sheetname, _sheets.Count))
                throw new ArgumentException("The workbook already contains a sheet of this name");
 
            HSSFSheet sheet = new HSSFSheet(this);
 
            workbook.SetSheetName(_sheets.Count, sheetname);
            _sheets.Add(sheet);
 
            bool isOnlySheet = _sheets.Count == 1;
            sheet.IsSelected = isOnlySheet;
            sheet.IsActive = isOnlySheet;
            return sheet;
        }
 
        /// <summary>
        /// Get the number of spreadsheets in the workbook (this will be three after serialization)
        /// </summary>
        /// <value>The number of sheets.</value>
        public int NumberOfSheets
        {
            get { return _sheets.Count; }
        }
 
        /// <summary>
        /// Gets the sheets.
        /// </summary>
        /// <returns></returns>
        private List<HSSFSheet> GetSheets()
        {
            return _sheets;
        }
        ///<summary>
        /// Get the HSSFSheet object at the given index.
        ///</summary>
        /// <param name="index">index of the sheet number (0-based)</param>
        /// <returns>HSSFSheet at the provided index</returns>
        public HH.WMS.Utils.NPOI.SS.UserModel.ISheet GetSheetAt(int index)
        {
            return (HSSFSheet)_sheets[index];
        }
 
        /// <summary>
        /// Get sheet with the given name (case insensitive match)
        /// </summary>
        /// <param name="name">name of the sheet</param>
        /// <returns>HSSFSheet with the name provided or null if it does not exist</returns>
        public HH.WMS.Utils.NPOI.SS.UserModel.ISheet GetSheet(String name)
        {
            HSSFSheet retval = null;
 
            for (int k = 0; k < _sheets.Count; k++)
            {
                String sheetname = workbook.GetSheetName(k);
 
                if (sheetname.Equals(name, StringComparison.OrdinalIgnoreCase))
                {
                    retval = (HSSFSheet)_sheets[k];
                }
            }
            return retval;
        }
 
        /// <summary>
        /// Removes sheet at the given index.
        /// </summary>
        /// <param name="index">index of the sheet  (0-based)</param>
        ///<remarks>
        /// Care must be taken if the Removed sheet Is the currently active or only selected sheet in
        /// the workbook. There are a few situations when Excel must have a selection and/or active
        /// sheet. (For example when printing - see Bug 40414).
        /// This method makes sure that if the Removed sheet was active, another sheet will become
        /// active in its place.  Furthermore, if the Removed sheet was the only selected sheet, another
        /// sheet will become selected.  The newly active/selected sheet will have the same index, or
        /// one less if the Removed sheet was the last in the workbook.
        /// </remarks>
        public void RemoveSheetAt(int index)
        {
            ValidateSheetIndex(index);
            bool wasActive = GetSheetAt(index).IsActive;
            bool wasSelected = GetSheetAt(index).IsSelected;
 
            _sheets.RemoveAt(index);
            workbook.RemoveSheet(index);
 
            // Set the remaining active/selected sheet
            int nSheets = _sheets.Count;
            if (nSheets < 1)
            {
                // nothing more to do if there are no sheets left
                return;
            }
            // the index of the closest remaining sheet to the one just deleted
            int newSheetIndex = index;
            if (newSheetIndex >= nSheets)
            {
                newSheetIndex = nSheets - 1;
            }
            if (wasActive)
            {
                SetActiveSheet(newSheetIndex);
            }
 
            if (wasSelected)
            {
                bool someOtherSheetIsStillSelected = false;
                for (int i = 0; i < nSheets; i++)
                {
                    if (GetSheetAt(i).IsSelected)
                    {
                        someOtherSheetIsStillSelected = true;
                        break;
                    }
                }
                if (!someOtherSheetIsStillSelected)
                {
                    SetSelectedTab(newSheetIndex);
                }
            }
        }
 
        /// <summary>
        /// determine whether the Excel GUI will backup the workbook when saving.
        /// </summary>
        /// <value>the current Setting for backups.</value>
        public bool BackupFlag
        {
            get
            {
                BackupRecord backupRecord = workbook.BackupRecord;
 
                return (backupRecord.Backup == 0) ? false
                        : true;
            }
            set
            {
                BackupRecord backupRecord = workbook.BackupRecord;
 
                backupRecord.Backup = (value ? (short)1
                        : (short)0);
            }
        }
 
        /// <summary>
        /// Sets the repeating rows and columns for a sheet (as found in
        /// File-&gt;PageSetup-&gt;Sheet).  This Is function Is included in the workbook
        /// because it Creates/modifies name records which are stored at the
        /// workbook level.
        /// </summary>
        /// <param name="sheetIndex">0 based index to sheet.</param>
        /// <param name="startColumn">0 based start of repeating columns.</param>
        /// <param name="endColumn">0 based end of repeating columns.</param>
        /// <param name="startRow">0 based start of repeating rows.</param>
        /// <param name="endRow">0 based end of repeating rows.</param>
        /// <example>
        /// To set just repeating columns:
        /// workbook.SetRepeatingRowsAndColumns(0,0,1,-1-1);
        /// To set just repeating rows:
        /// workbook.SetRepeatingRowsAndColumns(0,-1,-1,0,4);
        /// To remove all repeating rows and columns for a sheet.
        /// workbook.SetRepeatingRowsAndColumns(0,-1,-1,-1,-1);
        /// </example>
        public void SetRepeatingRowsAndColumns(int sheetIndex,
                                               int startColumn, int endColumn,
                                               int startRow, int endRow)
        {
            // Check arguments
            if (startColumn == -1 && endColumn != -1) throw new ArgumentException("Invalid column range specification");
            if (startRow == -1 && endRow != -1) throw new ArgumentException("Invalid row range specification");
            if (startColumn < -1 || startColumn >= 0xFF) throw new ArgumentException("Invalid column range specification");
            if (endColumn < -1 || endColumn >= 0xFF) throw new ArgumentException("Invalid column range specification");
            if (startRow < -1 || startRow > 65535) throw new ArgumentException("Invalid row range specification");
            if (endRow < -1 || endRow > 65535) throw new ArgumentException("Invalid row range specification");
            if (startColumn > endColumn) throw new ArgumentException("Invalid column range specification");
            if (startRow > endRow) throw new ArgumentException("Invalid row range specification");
 
            HSSFSheet sheet = (HSSFSheet)GetSheetAt(sheetIndex);
            int externSheetIndex = Workbook.CheckExternSheet(sheetIndex);
 
            bool settingRowAndColumn =
                    startColumn != -1 && endColumn != -1 && startRow != -1 && endRow != -1;
            bool removingRange =
                    startColumn == -1 && endColumn == -1 && startRow == -1 && endRow == -1;
 
            bool IsNewRecord = false;
 
 
            int rowColHeaderNameIndex = FindExistingBuiltinNameRecordIdx(sheetIndex, NameRecord.BUILTIN_PRINT_TITLE);
            if (removingRange)
            {
                if (rowColHeaderNameIndex >= 0)
                {
                    workbook.RemoveName(rowColHeaderNameIndex);
                }
                return;
            }
            NameRecord nameRecord;
            if (rowColHeaderNameIndex < 0)
            {
                //does a lot of the house keeping for builtin records, like setting lengths to zero etc
                nameRecord = workbook.CreateBuiltInName(NameRecord.BUILTIN_PRINT_TITLE, sheetIndex + 1);
                IsNewRecord = true;
            }
            else
            {
                nameRecord = workbook.GetNameRecord(rowColHeaderNameIndex);
                IsNewRecord = false;
            }
 
 
            ArrayList temp = new ArrayList();
            if (settingRowAndColumn)
            {
                int exprsSize = 2 * 11 + 1; // 2 * Area3DPtg.SIZE + UnionPtg.SIZE
                temp.Add(new MemFuncPtg(exprsSize));
            }
            if (startColumn >= 0)
            {
                Area3DPtg colArea = new Area3DPtg(0, MAX_ROW, startColumn, endColumn,
                        false, false, false, false, externSheetIndex);
                temp.Add(colArea);
            }
            if (startRow >= 0)
            {
                Area3DPtg rowArea = new Area3DPtg(startRow, endRow, 0, MAX_COLUMN,
                        false, false, false, false, externSheetIndex);
                temp.Add(rowArea);
            }
            if (settingRowAndColumn)
            {
                temp.Add(UnionPtg.instance);
            }
            Ptg[] ptgs = (Ptg[])temp.ToArray(typeof(Ptg));
            nameRecord.NameDefinition = ptgs;
 
            if (IsNewRecord)
            {
                HSSFName newName = new HSSFName(this, nameRecord);
                names.Add(newName);
            }
 
            HH.WMS.Utils.NPOI.SS.UserModel.IPrintSetup printSetup = sheet.PrintSetup;
            printSetup.ValidSettings = (false);
 
            sheet.IsActive = (true);
        }
        private int FindExistingBuiltinNameRecordIdx(int sheetIndex, byte builtinCode)
        {
            for (int defNameIndex = 0; defNameIndex < names.Count; defNameIndex++)
            {
                NameRecord r = workbook.GetNameRecord(defNameIndex);
                if (r == null)
                {
                    throw new InvalidOperationException("Unable to find all defined names to iterate over");
                }
                if (!r.IsBuiltInName || r.BuiltInName != builtinCode)
                {
                    continue;
                }
                if (r.SheetNumber - 1 == sheetIndex)
                {
                    return defNameIndex;
                }
            }
            return -1;
        }
 
        private bool IsRowColHeaderRecord(NameRecord r)
        {
            return r.OptionFlag == 0x20 && ("" + ((char)7)).Equals(r.NameText);
        }
 
        /// <summary>
        /// Create a new Font and Add it to the workbook's font table
        /// </summary>
        /// <returns>new font object</returns>
        public HH.WMS.Utils.NPOI.SS.UserModel.IFont CreateFont()
        {
            FontRecord font = workbook.CreateNewFont();
            short fontindex = (short)(NumberOfFonts - 1);
 
            if (fontindex > 3)
            {
                fontindex++;   // THERE Is NO FOUR!!
            }
            if (fontindex == short.MaxValue)
            {
                throw new ArgumentException("Maximum number of fonts was exceeded");
            }
            // Ask getFontAt() to build it for us,
            //  so it gets properly cached
            return GetFontAt(fontindex);
        }
 
        //public HH.WMS.Utils.NPOI.SS.UserModel.Font FindFont(HH.WMS.Utils.NPOI.SS.UserModel.FontBoldWeight boldWeight, short color, short fontHeight,
        //                 String name, bool italic, bool strikeout,
        //                 HH.WMS.Utils.NPOI.SS.UserModel.FontSuperScript typeOffset, HH.WMS.Utils.NPOI.SS.UserModel.FontUnderlineType Underline)
        //{
        //    return this.FindFont(boldWeight, color, fontHeight, name, italic, strikeout, typeOffset, Underline);
        //}
 
        /// <summary>
        /// Finds a font that matches the one with the supplied attributes
        /// </summary>
        /// <param name="boldWeight">The bold weight.</param>
        /// <param name="color">The color.</param>
        /// <param name="fontHeight">Height of the font.</param>
        /// <param name="name">The name.</param>
        /// <param name="italic">if set to <c>true</c> [italic].</param>
        /// <param name="strikeout">if set to <c>true</c> [strikeout].</param>
        /// <param name="typeOffset">The type offset.</param>
        /// <param name="Underline">The underline.</param>
        /// <returns></returns>
        public HH.WMS.Utils.NPOI.SS.UserModel.IFont FindFont(short boldWeight, short color, short fontHeight,
                         String name, bool italic, bool strikeout,
                         short typeOffset, byte Underline)
        {
            //        Console.WriteLine( boldWeight + ", " + color + ", " + fontHeight + ", " + name + ", " + italic + ", " + strikeout + ", " + typeOffset + ", " + Underline );
            for (short i = 0; i <= this.NumberOfFonts; i++)
            {
                // Remember - there is no 4!
                if (i == 4)
                    continue;
 
                HH.WMS.Utils.NPOI.SS.UserModel.IFont hssfFont = GetFontAt(i);
                //            Console.WriteLine( hssfFont.GetBoldweight() + ", " + hssfFont.GetColor() + ", " + hssfFont.FontHeight + ", " + hssfFont.FontName + ", " + hssfFont.GetItalic() + ", " + hssfFont.GetStrikeout() + ", " + hssfFont.GetTypeOffset() + ", " + hssfFont.Underline );
                if (hssfFont.Boldweight == boldWeight
                        && hssfFont.Color == color
                        && hssfFont.FontHeight == fontHeight
                        && hssfFont.FontName.Equals(name)
                        && hssfFont.IsItalic == italic
                        && hssfFont.IsStrikeout == strikeout
                        && hssfFont.TypeOffset == typeOffset
                        && hssfFont.Underline == Underline)
                {
                    //                Console.WriteLine( "Found font" );
                    return hssfFont;
                }
            }
 
            //        Console.WriteLine( "No font found" );
            return null;
        }
 
        /// <summary>
        /// Get the number of fonts in the font table
        /// </summary>
        /// <value>The number of fonts.</value>
        public short NumberOfFonts
        {
            get
            {
                return (short)workbook.NumberOfFontRecords;
            }
        }
        public bool IsHidden
        {
            get
            {
                return workbook.WindowOne.Hidden;
            }
            set
            {
                workbook.WindowOne.Hidden = value;
            }
        }
 
 
        /// <summary>
        /// Get the font at the given index number
        /// </summary>
        /// <param name="idx">The index number</param>
        /// <returns>HSSFFont at the index</returns>
        public HH.WMS.Utils.NPOI.SS.UserModel.IFont GetFontAt(short idx)
        {
            if (fonts == null) fonts = new Hashtable();
 
            // So we don't confuse users, give them back
            //  the same object every time, but create
            //  them lazily
 
            if (fonts.ContainsKey(idx))
            {
                return (HSSFFont)fonts[idx];
            }
 
            FontRecord font = workbook.GetFontRecordAt(idx);
            HSSFFont retval = new HSSFFont(idx, font);
            fonts[idx] = retval;
 
            return retval;
        }
 
        /// <summary>
        /// Reset the fonts cache, causing all new calls
        /// to getFontAt() to create new objects.
        /// Should only be called after deleting fonts,
        /// and that's not something you should normally do
        /// </summary>
        public void ResetFontCache()
        {
            fonts = new Hashtable();
        }
        /// <summary>
        /// Create a new Cell style and Add it to the workbook's style table
        /// </summary>
        /// <returns>the new Cell Style object</returns>
        public HH.WMS.Utils.NPOI.SS.UserModel.ICellStyle CreateCellStyle()
        {
            if (workbook.NumExFormats == MAX_STYLES)
            {
                throw new InvalidOperationException("The maximum number of cell styles was exceeded. " +
                        "You can define up to 4000 styles in a .xls workbook");
            }
            ExtendedFormatRecord xfr = workbook.CreateCellXF();
            short index = (short)(NumCellStyles - 1);
            HSSFCellStyle style = new HSSFCellStyle(index, xfr, this);
 
            return style;
        }
 
        /// <summary>
        /// Get the number of styles the workbook Contains
        /// </summary>
        /// <value>count of cell styles</value>
        public short NumCellStyles
        {
            get
            {
                return (short)workbook.NumExFormats;
            }
 
        }
 
        /// <summary>
        /// Get the cell style object at the given index
        /// </summary>
        /// <param name="idx">index within the Set of styles</param>
        /// <returns>HSSFCellStyle object at the index</returns>
        public HH.WMS.Utils.NPOI.SS.UserModel.ICellStyle GetCellStyleAt(short idx)
        {
            ExtendedFormatRecord xfr = workbook.GetExFormatAt(idx);
            HSSFCellStyle style = new HSSFCellStyle(idx, xfr, this);
 
            return style;
        }
 
        /// <summary>
        /// Write out this workbook to an Outputstream.  Constructs
        /// a new POI POIFSFileSystem, passes in the workbook binary representation  and
        /// Writes it out.
        /// </summary>
        /// <param name="stream">the java OutputStream you wish to Write the XLS to</param>
        public override void Write(Stream stream)
        {
            byte[] bytes = GetBytes();
            POIFSFileSystem fs = new POIFSFileSystem();
            
            // For tracking what we've written out, used if we're
            //  going to be preserving nodes
            List<string> excepts = new List<string>(1);
 
            using (MemoryStream newMemoryStream = new MemoryStream(bytes))
            {
                // Write out the Workbook stream
                fs.CreateDocument(newMemoryStream, "Workbook");
 
                // Write out our HPFS properties, if we have them
                WriteProperties(fs, excepts);
 
                if (preserveNodes)
                {
                    // Don't Write out the old Workbook, we'll be doing our new one
                    excepts.Add("Workbook");
                    // If the file had WORKBOOK instead of Workbook, we'll Write it
                    //  out correctly shortly, so don't include the old one
                    excepts.Add("WORKBOOK");
 
                    // Copy over all the other nodes to our new poifs
                    POIUtils.CopyNodes(directory, fs.Root, excepts);
                    // YK: preserve StorageClsid, it is important for embedded workbooks,
                    // see Bugzilla 47920
                    fs.Root.StorageClsid = (this.directory.StorageClsid);
                }
                fs.WriteFileSystem(stream);
 
            }
            
            bytes = null;
        }
 
        /// <summary>
        /// Get the bytes of just the HSSF portions of the XLS file.
        /// Use this to construct a POI POIFSFileSystem yourself.
        /// </summary>
        /// <returns>byte[] array containing the binary representation of this workbook and all contained
        /// sheets, rows, cells, etc.</returns>
        public byte[] GetBytes()
        {
            //if (log.Check(POILogger.DEBUG))
            //{
            //    log.Log(DEBUG, "HSSFWorkbook.GetBytes()");
            //}
 
            List<HSSFSheet> sheets = GetSheets();
            int nSheets = sheets.Count;
 
            // before Getting the workbook size we must tell the sheets that
            // serialization Is about to occur.
            for (int i = 0; i < nSheets; i++)
            {
                sheets[i].Sheet.Preserialize();
            }
 
            int totalsize = workbook.Size;
 
            // pre-calculate all the sheet sizes and set BOF indexes
            SheetRecordCollector[] srCollectors = new SheetRecordCollector[nSheets];
            for (int k = 0; k < nSheets; k++)
            {
                workbook.SetSheetBof(k, totalsize);
                using (SheetRecordCollector src = new SheetRecordCollector())
                {
                    sheets[k].Sheet.VisitContainedRecords(src, totalsize);
 
                    totalsize += src.TotalSize;
                    srCollectors[k] = src;
                }
            }
 
 
            byte[] retval = new byte[totalsize];
            int pos = workbook.Serialize(0, retval);
 
            for (int k = 0; k < nSheets; k++)
            {
                SheetRecordCollector src = srCollectors[k];
                int serializedSize = src.Serialize(pos, retval);
                if (serializedSize != src.TotalSize)
                {
                    // Wrong offset values have been passed in the call to SetSheetBof() above.
                    // For books with more than one sheet, this discrepancy would cause excel 
                    // to report errors and loose data while Reading the workbook
                    throw new InvalidOperationException("Actual serialized sheet size (" + serializedSize
                            + ") differs from pre-calculated size (" + src.TotalSize
                            + ") for sheet (" + k + ")");
                    // TODO - Add similar sanity Check to Ensure that Sheet.SerializeIndexRecord() does not Write mis-aligned offsets either
                }
                pos += serializedSize;
                src.Dispose();
            }
            
            return retval;
        }
 
        [Obsolete("Do not call this method from your applications. Use the methods available in the HSSFRow to Add string HSSFCells")]
        public int AddSSTString(String str)
        {
            return workbook.AddSSTString(new UnicodeString(str));
        }
 
        [Obsolete("Do not call this method from your applications. Use the methods available in the HSSFRow to Get string HSSFCells")]
        public String GetSSTString(int index)
        {
            return workbook.GetSSTString(index).String;
        }
        /**
 * The locator of user-defined functions.
 * By default includes functions from the Excel Analysis Toolpack
 */
        [NonSerialized]
        private UDFFinder _udfFinder = UDFFinder.DEFAULT;
 
        /**
 * Register a new toolpack in this workbook.
 *
 * @param toopack the toolpack to register
 */
        public void AddToolPack(UDFFinder toopack)
        {
            AggregatingUDFFinder udfs = (AggregatingUDFFinder)_udfFinder;
            udfs.Add(toopack);
        }
        /*package*/
        internal UDFFinder GetUDFFinder()
        {
            return _udfFinder;
        }
 
        /// <summary>
        /// Gets the workbook.
        /// </summary>
        /// <value>The workbook.</value>
        public InternalWorkbook Workbook
        {
            get { return workbook; }
        }
 
        /// <summary>
        /// Gets the total number of named ranges in the workboko
        /// </summary>
        /// <value>The number of named ranges</value>
        public int NumberOfNames
        {
            get
            {
                int result = names.Count;
                return result;
            }
        }
        public HH.WMS.Utils.NPOI.SS.UserModel.IName GetName(String name)
        {
            int nameIndex = GetNameIndex(name);
            if (nameIndex < 0)
            {
                return null;
            }
            return (HSSFName)names[nameIndex];
        }
 
        /// <summary>
        /// Gets the Named range
        /// </summary>
        /// <param name="index">position of the named range</param>
        /// <returns>named range high level</returns>
        public HH.WMS.Utils.NPOI.SS.UserModel.IName GetNameAt(int index)
        {
            HSSFName result = names[index];
 
            return result;
        }
 
        /// <summary>
        /// Gets the named range name
        /// </summary>
        /// <param name="index">the named range index (0 based)</param>
        /// <returns>named range name</returns>
        public String GetNameName(int index)
        {
            String result = GetNameAt(index).NameName;
 
            return result;
        }
 
        /// <summary>
        /// TODO - make this less cryptic / move elsewhere
        /// </summary>
        /// <param name="reFindex">Index to REF entry in EXTERNSHEET record in the Link Table</param>
        /// <param name="definedNameIndex">zero-based to DEFINEDNAME or EXTERNALNAME record</param>
        /// <returns>the string representation of the defined or external name</returns>
        public String ResolveNameXText(int reFindex, int definedNameIndex)
        {
            return workbook.ResolveNameXText(reFindex, definedNameIndex);
        }
 
 
        /// <summary>
        /// Sets the printarea for the sheet provided
        /// i.e. Reference = $A$1:$B$2
        /// </summary>
        /// <param name="sheetIndex">Zero-based sheet index (0 Represents the first sheet to keep consistent with java)</param>
        /// <param name="reference">Valid name Reference for the Print Area</param>
        public void SetPrintArea(int sheetIndex, String reference)
        {
            NameRecord name = workbook.GetSpecificBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex + 1);
 
 
            if (name == null)
                name = workbook.CreateBuiltInName(NameRecord.BUILTIN_PRINT_AREA, sheetIndex + 1);
            //Adding one here because 0 indicates a global named region; doesnt make sense for print areas
            String[] parts = reference.Split(new char[]{','});
            StringBuilder sb = new StringBuilder(32);
            for (int i = 0; i < parts.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append(",");
                }
                SheetNameFormatter.AppendFormat(sb, GetSheetName(sheetIndex));
                sb.Append("!");
                sb.Append(parts[i]);
            }
            name.NameDefinition=(HSSFFormulaParser.Parse(sb.ToString(), this, FormulaType.CELL, sheetIndex));
 
        }
 
        /// <summary>
        /// Sets the print area.
        /// </summary>
        /// <param name="sheetIndex">Zero-based sheet index (0 = First Sheet)</param>
        /// <param name="startColumn">Column to begin printarea</param>
        /// <param name="endColumn">Column to end the printarea</param>
        /// <param name="startRow">Row to begin the printarea</param>
        /// <param name="endRow">Row to end the printarea</param>
        public void SetPrintArea(int sheetIndex, int startColumn, int endColumn,
                                  int startRow, int endRow)
        {
 
            //using absolute references because they don't Get copied and pasted anyway
            CellReference cell = new CellReference(startRow, startColumn, true, true);
            String reference = cell.FormatAsString();
 
            cell = new CellReference(endRow, endColumn, true, true);
            reference = reference + ":" + cell.FormatAsString();
 
            SetPrintArea(sheetIndex, reference);
        }
 
 
        /// <summary>
        /// Retrieves the reference for the printarea of the specified sheet, the sheet name Is Appended to the reference even if it was not specified.
        /// </summary>
        /// <param name="sheetIndex">Zero-based sheet index (0 Represents the first sheet to keep consistent with java)</param>
        /// <returns>String Null if no print area has been defined</returns>
        public String GetPrintArea(int sheetIndex)
        {
            NameRecord name = workbook.GetSpecificBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex + 1);
            if (name == null) return null;
            //Adding one here because 0 indicates a global named region; doesnt make sense for print areas
            return HSSFFormulaParser.ToFormulaString(this, name.NameDefinition);
        }
 
        /// <summary>
        /// Delete the printarea for the sheet specified
        /// </summary>
        /// <param name="sheetIndex">Zero-based sheet index (0 = First Sheet)</param>
        public void RemovePrintArea(int sheetIndex)
        {
            Workbook.RemoveBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex + 1);
        }
 
        /// <summary>
        /// Creates a new named range and Add it to the model
        /// </summary>
        /// <returns>named range high level</returns>
        public HH.WMS.Utils.NPOI.SS.UserModel.IName CreateName()
        {
            NameRecord nameRecord = workbook.CreateName();
 
            HSSFName newName = new HSSFName(this, nameRecord);
 
            names.Add(newName);
 
            return newName;
        }
 
        /// <summary>
        /// Gets the named range index by his name
        /// Note:
        /// Excel named ranges are case-insensitive and
        /// this method performs a case-insensitive search.
        /// </summary>
        /// <param name="name">named range name</param>
        /// <returns>named range index</returns>
        public int GetNameIndex(String name)
        {
            int retval = -1;
 
            for (int k = 0; k < names.Count; k++)
            {
                String nameName = GetNameName(k);
 
                if (nameName.Equals(name, StringComparison.OrdinalIgnoreCase))
                {
                    retval = k;
                    break;
                }
            }
            return retval;
        }
 
 
        /// <summary>
        /// Remove the named range by his index
        /// </summary>
        /// <param name="index">The named range index (0 based)</param>
        public void RemoveName(int index)
        {
            names.RemoveAt(index);
            workbook.RemoveName(index);
        }
 
        /// <summary>
        /// Creates the instance of HSSFDataFormat for this workbook.
        /// </summary>
        /// <returns>the HSSFDataFormat object</returns>
        public HH.WMS.Utils.NPOI.SS.UserModel.IDataFormat CreateDataFormat()
        {
            if (formatter == null)
                formatter = new HSSFDataFormat(workbook);
            return formatter;
        }
 
        /// <summary>
        /// Remove the named range by his name
        /// </summary>
        /// <param name="name">named range name</param>
        public void RemoveName(String name)
        {
            int index = GetNameIndex(name);
 
            RemoveName(index);
 
        }
 
        public HSSFPalette GetCustomPalette()
        {
            return new HSSFPalette(workbook.CustomPalette);
        }
 
        // /** Test only. Do not use */
        //public void InsertChartRecord()
        //{
        //    int loc = workbook.FindFirstRecordLocBySid(SSTRecord.sid);
        //    byte[] data = {
        //       (byte)0x0F, (byte)0x00, (byte)0x00, (byte)0xF0, (byte)0x52,
        //       (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
        //       (byte)0x06, (byte)0xF0, (byte)0x18, (byte)0x00, (byte)0x00,
        //       (byte)0x00, (byte)0x01, (byte)0x08, (byte)0x00, (byte)0x00,
        //       (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02,
        //       (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00,
        //       (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00,
        //       (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00,
        //       (byte)0x33, (byte)0x00, (byte)0x0B, (byte)0xF0, (byte)0x12,
        //       (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xBF, (byte)0x00,
        //       (byte)0x08, (byte)0x00, (byte)0x08, (byte)0x00, (byte)0x81,
        //       (byte)0x01, (byte)0x09, (byte)0x00, (byte)0x00, (byte)0x08,
        //       (byte)0xC0, (byte)0x01, (byte)0x40, (byte)0x00, (byte)0x00,
        //       (byte)0x08, (byte)0x40, (byte)0x00, (byte)0x1E, (byte)0xF1,
        //       (byte)0x10, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0D,
        //       (byte)0x00, (byte)0x00, (byte)0x08, (byte)0x0C, (byte)0x00,
        //       (byte)0x00, (byte)0x08, (byte)0x17, (byte)0x00, (byte)0x00,
        //       (byte)0x08, (byte)0xF7, (byte)0x00, (byte)0x00, (byte)0x10,
        //    };
        //    UnknownRecord r = new UnknownRecord((short)0x00EB, data);
        //    workbook.Records.Insert(loc, r);
        //}
 
        /// <summary>
        /// Spits out a list of all the drawing records in the workbook.
        /// </summary>
        /// <param name="fat">if set to <c>true</c> [fat].</param>
        public void DumpDrawingGroupRecords(bool fat)
        {
            DrawingGroupRecord r = (DrawingGroupRecord)workbook.FindFirstRecordBySid(DrawingGroupRecord.sid);
            r.Decode();
            IList escherRecords = r.EscherRecords;
 
            for (IEnumerator iterator = escherRecords.GetEnumerator(); iterator.MoveNext(); )
            {
                EscherRecord escherRecord = (EscherRecord)iterator.Current;
                if (fat)
                    Console.WriteLine(escherRecord.ToString());
                else
                    escherRecord.Display(0);
            }
        }
        internal void InitDrawings()
        {
            DrawingManager2 mgr = workbook.FindDrawingGroup();
            if (mgr != null)
            {
                for (int i = 0; i < NumberOfSheets; i++)
                {
                    IDrawing tmp = GetSheetAt(i).DrawingPatriarch;
                }
            }
            else
            {
                workbook.CreateDrawingGroup();
            }
        }
        /// <summary>
        /// Adds a picture to the workbook.
        /// </summary>
        /// <param name="pictureData">The bytes of the picture</param>
        /// <param name="format">The format of the picture.  One of 
        /// PictureType.</param>
        /// <returns>the index to this picture (1 based).</returns>
        public int AddPicture(byte[] pictureData, HH.WMS.Utils.NPOI.SS.UserModel.PictureType format)
        {
            InitDrawings();
 
            byte[] uid;
            using (MD5 md5 = MD5.Create())
            {
                uid = md5.ComputeHash(pictureData);
            }
            EscherBitmapBlip blipRecord = new EscherBitmapBlip();
            blipRecord.RecordId = (short)(EscherBitmapBlip.RECORD_ID_START + format);
            
            switch (format)
            {
                case HH.WMS.Utils.NPOI.SS.UserModel.PictureType.EMF:
                    blipRecord.Options = HSSFPictureData.MSOBI_EMF;
                    break;
                case HH.WMS.Utils.NPOI.SS.UserModel.PictureType.WMF:
                    blipRecord.Options = HSSFPictureData.MSOBI_WMF;
                    break;
                case HH.WMS.Utils.NPOI.SS.UserModel.PictureType.PICT:
                    blipRecord.Options = HSSFPictureData.MSOBI_PICT;
                    break;
                case HH.WMS.Utils.NPOI.SS.UserModel.PictureType.PNG:
                    blipRecord.Options = HSSFPictureData.MSOBI_PNG;
                    break;
                case HH.WMS.Utils.NPOI.SS.UserModel.PictureType.JPEG:
                    blipRecord.Options = HSSFPictureData.MSOBI_JPEG;
                    break;
                case HH.WMS.Utils.NPOI.SS.UserModel.PictureType.DIB:
                    blipRecord.Options = HSSFPictureData.MSOBI_DIB;
                    break;
            }
 
            blipRecord.UID = uid;
            blipRecord.Marker = (byte)0xFF;
            blipRecord.PictureData = pictureData;
 
            EscherBSERecord r = new EscherBSERecord();
            r.RecordId = EscherBSERecord.RECORD_ID;
            r.Options = (short)(0x0002 | ((int)format << 4));
            r.BlipTypeMacOS = (byte)format;
            r.BlipTypeWin32 = (byte)format;
            r.UID = uid;
            r.Tag = (short)0xFF;
            r.Size = pictureData.Length + 25;
            r.Ref = 1;
            r.Offset = 0;
            r.BlipRecord = blipRecord;
 
            return workbook.AddBSERecord(r);
        }
 
        /// <summary>
        /// Gets all pictures from the Workbook.
        /// </summary>
        /// <returns>the list of pictures (a list of HSSFPictureData objects.)</returns>
        public IList GetAllPictures()
        {
            // The drawing Group record always exists at the top level, so we won't need to do this recursively.
            List<HSSFPictureData> pictures = new List<HSSFPictureData>();
            IEnumerator recordIter = workbook.Records.GetEnumerator();
            while (recordIter.MoveNext())
            {
                Object obj = recordIter.Current;
                if (obj is AbstractEscherHolderRecord)
                {
                    ((AbstractEscherHolderRecord)obj).Decode();
                    IList escherRecords = ((AbstractEscherHolderRecord)obj).EscherRecords;
                    SearchForPictures(escherRecords, pictures);
                }
            }
            return pictures;
        }
        //public HSSFAutoFilter CreateAutoFilter(String formula)
        //{
        //    if (string.IsNullOrEmpty(formula))
        //        return null;
 
        //    HSSFAutoFilter autofilter = new HSSFAutoFilter(formula, this);
        //    return autofilter;
        //}
//        public HSSFAutoFilter CreateCustomFilter(string formula,)
 
        /// <summary>
        /// Performs a recursive search for pictures in the given list of escher records.
        /// </summary>
        /// <param name="escherRecords">the escher records.</param>
        /// <param name="pictures">the list to populate with the pictures.</param>
        private void SearchForPictures(IList escherRecords, List<HSSFPictureData> pictures)
        {
            IEnumerator recordIter = escherRecords.GetEnumerator();
            while (recordIter.MoveNext())
            {
                Object obj = recordIter.Current;
                if (obj is EscherRecord)
                {
                    EscherRecord escherRecord = (EscherRecord)obj;
 
                    if (escherRecord is EscherBSERecord)
                    {
                        EscherBlipRecord blip = ((EscherBSERecord)escherRecord).BlipRecord;
                        if (blip != null)
                        {
                            // TODO: Some kind of structure.
                            pictures.Add(new HSSFPictureData(blip));
                        }
                    }
 
                    // Recursive call.
                    SearchForPictures(escherRecord.ChildRecords, pictures);
                }
            }
        }
 
        /// <summary>
        /// Is the workbook protected with a password (not encrypted)?
        /// </summary>
        /// <value>
        ///     <c>true</c> if this instance is write protected; otherwise, <c>false</c>.
        /// </value>
        public bool IsWriteProtected
        {
            get { return this.workbook.IsWriteProtected; }
        }
 
        /// <summary>
        /// protect a workbook with a password (not encypted, just Sets Writeprotect
        /// flags and the password.
        /// </summary>
        /// <param name="password">password to set</param>
        /// <param name="username">The username.</param>
        public void WriteProtectWorkbook(String password, String username)
        {
            this.workbook.WriteProtectWorkbook(password, username);
        }
 
        /// <summary>
        /// Removes the Write protect flag
        /// </summary>
        public void UnwriteProtectWorkbook()
        {
            this.workbook.UnwriteProtectWorkbook();
        }
 
        /// <summary>
        /// Gets all embedded OLE2 objects from the Workbook.
        /// </summary>
        /// <returns>the list of embedded objects (a list of HSSFObjectData objects.)</returns>
        public IList<HSSFObjectData> GetAllEmbeddedObjects()
        {
            List<HSSFObjectData> objects = new List<HSSFObjectData>();
            for (int i = 0; i < NumberOfSheets; i++)
            {
                GetAllEmbeddedObjects(((HSSFSheet)GetSheetAt(i)).Sheet.Records, objects);
            }
            return objects;
        }
 
        /// <summary>
        /// Gets all embedded OLE2 objects from the Workbook.
        /// </summary>
        /// <param name="records">the list of records to search.</param>
        /// <param name="objects">the list of embedded objects to populate.</param>
        private void GetAllEmbeddedObjects(IList records, IList objects)
        {
            IEnumerator recordIter = records.GetEnumerator();
            while (recordIter.MoveNext())
            {
                Object obj = recordIter.Current;
                if (obj is ObjRecord)
                {
                    // TODO: More convenient way of determining if there Is stored binary.
                    // TODO: Link to the data stored in the other stream.
                    IEnumerator subRecordIter = ((ObjRecord)obj).SubRecords.GetEnumerator();
                    while (subRecordIter.MoveNext())
                    {
                        Object sub = subRecordIter.Current;
                        if (sub is EmbeddedObjectRefSubRecord)
                        {
                            objects.Add(new HSSFObjectData((ObjRecord)obj, directory));
                        }
                    }
                }
            }
        }
 
        /// <summary>
        /// Gets the new UID.
        /// </summary>
        /// <value>The new UID.</value>
        public byte[] NewUID
        {
            get
            {
                byte[] bytes = new byte[16];
                return bytes;
            }
        }
 
        /// <summary>
        /// Support foreach ISheet, e.g.
        /// HSSFWorkbook workbook = new HSSFWorkbook();
        /// foreach(ISheet sheet in workbook) ...
        /// </summary>
        /// <returns>Enumeration of all the sheets of this workbook</returns>
        public IEnumerator GetEnumerator()
        {
            return _sheets.GetEnumerator();
        }
 
 
        /// <summary>
        /// Whether the application shall perform a full recalculation when the workbook is opened.
        /// 
        /// Typically you want to force formula recalculation when you modify cell formulas or values
        /// of a workbook previously created by Excel. When set to true, this flag will tell Excel
        /// that it needs to recalculate all formulas in the workbook the next time the file is opened.
        /// 
        /// Note, that recalculation updates cached formula results and, thus, modifies the workbook.
        /// Depending on the version, Excel may prompt you with "Do you want to save the changes in <em>filename</em>?"
        /// on close.
        /// 
        /// Value is true if the application will perform a full recalculation of
        /// workbook values when the workbook is opened.
        /// 
        /// since 3.8
        /// </summary>
        public bool ForceFormulaRecalculation
        {
            set
            {
                InternalWorkbook iwb = Workbook;
                RecalcIdRecord recalc = iwb.RecalcId;
                recalc.EngineId=(0);
            }
            get
            {
                InternalWorkbook iwb = Workbook;
                RecalcIdRecord recalc = (RecalcIdRecord)iwb.FindFirstRecordBySid(RecalcIdRecord.sid);
                return recalc != null && recalc.EngineId != 0;
            }
        }
 
 
    }
}