chamilo /common/libraries/plugin/phpexcel/PHPExcel/Reader/Excel5.php

Language PHP Lines 6668
MD5 Hash ad7564683c78880c757f05fe428534fc Estimated Cost $108,570 (why?)
Repository https://bitbucket.org/chamilo/chamilo/ View Raw File View Project SPDX
   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
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
<?php
/**
 * PHPExcel
 *
 * Copyright (c) 2006 - 2011 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel_Reader_Excel5
 * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 * @version    1.7.6, 2011-02-27
 */

// Original file header of ParseXL (used as the base for this class):
// --------------------------------------------------------------------------------
// Adapted from Excel_Spreadsheet_Reader developed by users bizon153,
// trex005, and mmp11 (SourceForge.net)
// http://sourceforge.net/projects/phpexcelreader/
// Primary changes made by canyoncasa (dvc) for ParseXL 1.00 ...
//	 Modelled moreso after Perl Excel Parse/Write modules
//	 Added Parse_Excel_Spreadsheet object
//		 Reads a whole worksheet or tab as row,column array or as
//		 associated hash of indexed rows and named column fields
//	 Added variables for worksheet (tab) indexes and names
//	 Added an object call for loading individual woorksheets
//	 Changed default indexing defaults to 0 based arrays
//	 Fixed date/time and percent formats
//	 Includes patches found at SourceForge...
//		 unicode patch by nobody
//		 unpack("d") machine depedency patch by matchy
//		 boundsheet utf16 patch by bjaenichen
//	 Renamed functions for shorter names
//	 General code cleanup and rigor, including <80 column width
//	 Included a testcase Excel file and PHP example calls
//	 Code works for PHP 5.x

// Primary changes made by canyoncasa (dvc) for ParseXL 1.10 ...
// http://sourceforge.net/tracker/index.php?func=detail&aid=1466964&group_id=99160&atid=623334
//	 Decoding of formula conditions, results, and tokens.
//	 Support for user-defined named cells added as an array "namedcells"
//		 Patch code for user-defined named cells supports single cells only.
//		 NOTE: this patch only works for BIFF8 as BIFF5-7 use a different
//		 external sheet reference structure


/** PHPExcel root directory */
if (!defined('PHPEXCEL_ROOT')) {
	/**
	 * @ignore
	 */
	define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
	require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}

/**
 *	PHPExcel_Reader_Excel5
 *
 *	This class uses {@link http://sourceforge.net/projects/phpexcelreader/parseXL}
 *
 *	@category	PHPExcel
 *	@package	PHPExcel_Reader_Excel5
 *	@copyright	Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
 */
class PHPExcel_Reader_Excel5 implements PHPExcel_Reader_IReader
{
	// ParseXL definitions
	const XLS_BIFF8						= 0x0600;
	const XLS_BIFF7						= 0x0500;
	const XLS_WorkbookGlobals			= 0x0005;
	const XLS_Worksheet					= 0x0010;

	// record identifiers
	const XLS_Type_FORMULA				= 0x0006;
	const XLS_Type_EOF					= 0x000a;
	const XLS_Type_PROTECT				= 0x0012;
	const XLS_Type_OBJECTPROTECT		= 0x0063;
	const XLS_Type_SCENPROTECT			= 0x00dd;
	const XLS_Type_PASSWORD				= 0x0013;
	const XLS_Type_HEADER				= 0x0014;
	const XLS_Type_FOOTER				= 0x0015;
	const XLS_Type_EXTERNSHEET			= 0x0017;
	const XLS_Type_DEFINEDNAME			= 0x0018;
	const XLS_Type_VERTICALPAGEBREAKS	= 0x001a;
	const XLS_Type_HORIZONTALPAGEBREAKS	= 0x001b;
	const XLS_Type_NOTE					= 0x001c;
	const XLS_Type_SELECTION			= 0x001d;
	const XLS_Type_DATEMODE				= 0x0022;
	const XLS_Type_EXTERNNAME			= 0x0023;
	const XLS_Type_LEFTMARGIN			= 0x0026;
	const XLS_Type_RIGHTMARGIN			= 0x0027;
	const XLS_Type_TOPMARGIN			= 0x0028;
	const XLS_Type_BOTTOMMARGIN			= 0x0029;
	const XLS_Type_PRINTGRIDLINES		= 0x002b;
	const XLS_Type_FILEPASS				= 0x002f;
	const XLS_Type_FONT					= 0x0031;
	const XLS_Type_CONTINUE				= 0x003c;
	const XLS_Type_PANE					= 0x0041;
	const XLS_Type_CODEPAGE				= 0x0042;
	const XLS_Type_DEFCOLWIDTH 			= 0x0055;
	const XLS_Type_OBJ					= 0x005d;
	const XLS_Type_COLINFO				= 0x007d;
	const XLS_Type_IMDATA				= 0x007f;
	const XLS_Type_SHEETPR				= 0x0081;
	const XLS_Type_HCENTER				= 0x0083;
	const XLS_Type_VCENTER				= 0x0084;
	const XLS_Type_SHEET				= 0x0085;
	const XLS_Type_PALETTE				= 0x0092;
	const XLS_Type_SCL					= 0x00a0;
	const XLS_Type_PAGESETUP			= 0x00a1;
	const XLS_Type_MULRK				= 0x00bd;
	const XLS_Type_MULBLANK				= 0x00be;
	const XLS_Type_DBCELL				= 0x00d7;
	const XLS_Type_XF					= 0x00e0;
	const XLS_Type_MERGEDCELLS			= 0x00e5;
	const XLS_Type_MSODRAWINGGROUP		= 0x00eb;
	const XLS_Type_MSODRAWING			= 0x00ec;
	const XLS_Type_SST					= 0x00fc;
	const XLS_Type_LABELSST				= 0x00fd;
	const XLS_Type_EXTSST				= 0x00ff;
	const XLS_Type_EXTERNALBOOK			= 0x01ae;
	const XLS_Type_DATAVALIDATIONS		= 0x01b2;
	const XLS_Type_TXO					= 0x01b6;
	const XLS_Type_HYPERLINK			= 0x01b8;
	const XLS_Type_DATAVALIDATION		= 0x01be;
	const XLS_Type_DIMENSION			= 0x0200;
	const XLS_Type_BLANK				= 0x0201;
	const XLS_Type_NUMBER				= 0x0203;
	const XLS_Type_LABEL				= 0x0204;
	const XLS_Type_BOOLERR				= 0x0205;
	const XLS_Type_STRING				= 0x0207;
	const XLS_Type_ROW					= 0x0208;
	const XLS_Type_INDEX				= 0x020b;
	const XLS_Type_ARRAY				= 0x0221;
	const XLS_Type_DEFAULTROWHEIGHT 	= 0x0225;
	const XLS_Type_WINDOW2				= 0x023e;
	const XLS_Type_RK					= 0x027e;
	const XLS_Type_STYLE				= 0x0293;
	const XLS_Type_FORMAT				= 0x041e;
	const XLS_Type_SHAREDFMLA			= 0x04bc;
	const XLS_Type_BOF					= 0x0809;
	const XLS_Type_SHEETPROTECTION		= 0x0867;
	const XLS_Type_RANGEPROTECTION		= 0x0868;
	const XLS_Type_SHEETLAYOUT			= 0x0862;
	const XLS_Type_XFEXT				= 0x087d;
	const XLS_Type_UNKNOWN				= 0xffff;

	/**
	 *	Read data only?
	 *	Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
	 *		or whether it should read both data and formatting
	 *
	 *	@var	boolean
	 */
	private $_readDataOnly = false;

	/**
	 *	Restrict which sheets should be loaded?
	 *	This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
	 *
	 *	@var	array of string
	 */
	private $_loadSheetsOnly = null;

	/**
	 * PHPExcel_Reader_IReadFilter instance
	 *
	 * @var PHPExcel_Reader_IReadFilter
	 */
	private $_readFilter = null;

	/**
	 * Summary Information stream data.
	 *
	 * @var string
	 */
	private $_summaryInformation;

	/**
	 * Extended Summary Information stream data.
	 *
	 * @var string
	 */
	private $_documentSummaryInformation;

	/**
	 * User-Defined Properties stream data.
	 *
	 * @var string
	 */
	private $_userDefinedProperties;

	/**
	 * Workbook stream data. (Includes workbook globals substream as well as sheet substreams)
	 *
	 * @var string
	 */
	private $_data;

	/**
	 * Size in bytes of $this->_data
	 *
	 * @var int
	 */
	private $_dataSize;

	/**
	 * Current position in stream
	 *
	 * @var integer
	 */
	private $_pos;

	/**
	 * Workbook to be returned by the reader.
	 *
	 * @var PHPExcel
	 */
	private $_phpExcel;

	/**
	 * Worksheet that is currently being built by the reader.
	 *
	 * @var PHPExcel_Worksheet
	 */
	private $_phpSheet;

	/**
	 * BIFF version
	 *
	 * @var int
	 */
	private $_version;

	/**
	 * Codepage set in the Excel file being read. Only important for BIFF5 (Excel 5.0 - Excel 95)
	 * For BIFF8 (Excel 97 - Excel 2003) this will always have the value 'UTF-16LE'
	 *
	 * @var string
	 */
	private $_codepage;

	/**
	 * Shared formats
	 *
	 * @var array
	 */
	private $_formats;

	/**
	 * Shared fonts
	 *
	 * @var array
	 */
	private $_objFonts;

	/**
	 * Color palette
	 *
	 * @var array
	 */
	private $_palette;

	/**
	 * Worksheets
	 *
	 * @var array
	 */
	private $_sheets;

	/**
	 * External books
	 *
	 * @var array
	 */
	private $_externalBooks;

	/**
	 * REF structures. Only applies to BIFF8.
	 *
	 * @var array
	 */
	private $_ref;

	/**
	 * External names
	 *
	 * @var array
	 */
	private $_externalNames;

	/**
	 * Defined names
	 *
	 * @var array
	 */
	private $_definedname;

	/**
	 * Shared strings. Only applies to BIFF8.
	 *
	 * @var array
	 */
	private $_sst;

	/**
	 * Panes are frozen? (in sheet currently being read). See WINDOW2 record.
	 *
	 * @var boolean
	 */
	private $_frozen;

	/**
	 * Fit printout to number of pages? (in sheet currently being read). See SHEETPR record.
	 *
	 * @var boolean
	 */
	private $_isFitToPages;

	/**
	 * Objects. One OBJ record contributes with one entry.
	 *
	 * @var array
	 */
	private $_objs;

	/**
	 * Text Objects. One TXO record corresponds with one entry.
	 *
	 * @var array
	 */
	private $_textObjects;

	/**
	 * Cell Annotations (BIFF8)
	 *
	 * @var array
	 */
	private $_cellNotes;

	/**
	 * The combined MSODRAWINGGROUP data
	 *
	 * @var string
	 */
	private $_drawingGroupData;

	/**
	 * The combined MSODRAWING data (per sheet)
	 *
	 * @var string
	 */
	private $_drawingData;

	/**
	 * Keep track of XF index
	 *
	 * @var int
	 */
	private $_xfIndex;

	/**
	 * Mapping of XF index (that is a cell XF) to final index in cellXf collection
	 *
	 * @var array
	 */
	private $_mapCellXfIndex;

	/**
	 * Mapping of XF index (that is a style XF) to final index in cellStyleXf collection
	 *
	 * @var array
	 */
	private $_mapCellStyleXfIndex;

	/**
	 * The shared formulas in a sheet. One SHAREDFMLA record contributes with one value.
	 *
	 * @var array
	 */
	private $_sharedFormulas;

	/**
	 * The shared formula parts in a sheet. One FORMULA record contributes with one value if it
	 * refers to a shared formula.
	 *
	 * @var array
	 */
	private $_sharedFormulaParts;


	/**
	 *	Read data only?
	 *		If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
	 *		If false (the default) it will read data and formatting.
	 *
	 *	@return	boolean
	 */
	public function getReadDataOnly()
	{
		return $this->_readDataOnly;
	}

	/**
	 *	Set read data only
	 *		Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
	 *		Set to false (the default) to advise the Reader to read both data and formatting for cells.
	 *
	 *	@param	boolean	$pValue
	 *
	 *	@return	PHPExcel_Reader_Excel5
	 */
	public function setReadDataOnly($pValue = false)
	{
		$this->_readDataOnly = $pValue;
		return $this;
	}

	/**
	 *	Get which sheets to load
	 *		Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
	 *			indicating that all worksheets in the workbook should be loaded.
	 *
	 *	@return mixed
	 */
	public function getLoadSheetsOnly()
	{
		return $this->_loadSheetsOnly;
	}

	/**
	 *	Set which sheets to load
	 *
	 *	@param mixed $value
	 *		This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
	 *		If NULL, then it tells the Reader to read all worksheets in the workbook
	 *
	 *	@return PHPExcel_Reader_Excel5
	 */
	public function setLoadSheetsOnly($value = null)
	{
		$this->_loadSheetsOnly = is_array($value) ?
			$value : array($value);
		return $this;
	}

	/**
	 *	Set all sheets to load
	 *		Tells the Reader to load all worksheets from the workbook.
	 *
	 *	@return	PHPExcel_Reader_Excel5
	 */
	public function setLoadAllSheets()
	{
		$this->_loadSheetsOnly = null;
		return $this;
	}

	/**
	 * Read filter
	 *
	 * @return PHPExcel_Reader_IReadFilter
	 */
	public function getReadFilter() {
		return $this->_readFilter;
	}

	/**
	 * Set read filter
	 *
	 * @param PHPExcel_Reader_IReadFilter $pValue
	 * @return PHPExcel_Reader_Excel5
	 */
	public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
		$this->_readFilter = $pValue;
		return $this;
	}

	/**
	 * Create a new PHPExcel_Reader_Excel5 instance
	 */
	public function __construct() {
		$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
	}

	/**
	 * Can the current PHPExcel_Reader_IReader read the file?
	 *
	 * @param 	string 		$pFileName
	 * @return 	boolean
	 */
	public function canRead($pFilename)
	{
		// Check if file exists
		if (!file_exists($pFilename)) {
			throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
		}

		try {
			// Use ParseXL for the hard work.
			$ole = new PHPExcel_Shared_OLERead();

			// get excel data
			$res = $ole->read($pFilename);
			return true;

		} catch (Exception $e) {
			return false;
		}
	}

	/**
	 * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
	 *
	 * @param 	string 		$pFilename
	 * @throws 	Exception
	 */
	public function listWorksheetNames($pFilename)
	{
		// Check if file exists
		if (!file_exists($pFilename)) {
			throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
		}

		$worksheetNames = array();

		// Read the OLE file
		$this->_loadOLE($pFilename);

		// total byte size of Excel data (workbook global substream + sheet substreams)
		$this->_dataSize = strlen($this->_data);

		$this->_pos		= 0;
		$this->_sheets	= array();

		// Parse Workbook Global Substream
		while ($this->_pos < $this->_dataSize) {
			$code = self::_GetInt2d($this->_data, $this->_pos);

			switch ($code) {
				case self::XLS_Type_BOF:	$this->_readBof();		break;
				case self::XLS_Type_SHEET:	$this->_readSheet();	break;
				case self::XLS_Type_EOF:	$this->_readDefault();	break 2;
				default:					$this->_readDefault();	break;
			}
		}

		foreach ($this->_sheets as $sheet) {
			if ($sheet['sheetType'] != 0x00) {
				// 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
				continue;
			}

			$worksheetNames[] = $sheet['name'];
		}

		return $worksheetNames;
	}


	/**
	 * Loads PHPExcel from file
	 *
	 * @param 	string 		$pFilename
	 * @return 	PHPExcel
	 * @throws 	Exception
	 */
	public function load($pFilename)
	{
		// Read the OLE file
		$this->_loadOLE($pFilename);

		// Initialisations
		$this->_phpExcel = new PHPExcel;
		$this->_phpExcel->removeSheetByIndex(0); // remove 1st sheet
		if (!$this->_readDataOnly) {
			$this->_phpExcel->removeCellStyleXfByIndex(0); // remove the default style
			$this->_phpExcel->removeCellXfByIndex(0); // remove the default style
		}

		// Read the summary information stream (containing meta data)
		$this->_readSummaryInformation();

		// Read the Additional document summary information stream (containing application-specific meta data)
		$this->_readDocumentSummaryInformation();

		// total byte size of Excel data (workbook global substream + sheet substreams)
		$this->_dataSize = strlen($this->_data);

		// initialize
		$this->_pos					= 0;
		$this->_codepage			= 'CP1252';
		$this->_formats				= array();
		$this->_objFonts			= array();
		$this->_palette				= array();
		$this->_sheets				= array();
		$this->_externalBooks		= array();
		$this->_ref					= array();
		$this->_definedname			= array();
		$this->_sst					= array();
		$this->_drawingGroupData	= '';
		$this->_xfIndex				= '';
		$this->_mapCellXfIndex		= array();
		$this->_mapCellStyleXfIndex	= array();

		// Parse Workbook Global Substream
		while ($this->_pos < $this->_dataSize) {
			$code = self::_GetInt2d($this->_data, $this->_pos);

			switch ($code) {
				case self::XLS_Type_BOF:			$this->_readBof();				break;
				case self::XLS_Type_FILEPASS:		$this->_readFilepass();			break;
				case self::XLS_Type_CODEPAGE:		$this->_readCodepage();			break;
				case self::XLS_Type_DATEMODE:		$this->_readDateMode();			break;
				case self::XLS_Type_FONT:			$this->_readFont();				break;
				case self::XLS_Type_FORMAT:			$this->_readFormat();			break;
				case self::XLS_Type_XF:				$this->_readXf();				break;
				case self::XLS_Type_XFEXT:			$this->_readXfExt();			break;
				case self::XLS_Type_STYLE:			$this->_readStyle();			break;
				case self::XLS_Type_PALETTE:		$this->_readPalette();			break;
				case self::XLS_Type_SHEET:			$this->_readSheet();			break;
				case self::XLS_Type_EXTERNALBOOK:	$this->_readExternalBook();		break;
				case self::XLS_Type_EXTERNNAME:		$this->_readExternName();		break;
				case self::XLS_Type_EXTERNSHEET:	$this->_readExternSheet();		break;
				case self::XLS_Type_DEFINEDNAME:	$this->_readDefinedName();		break;
				case self::XLS_Type_MSODRAWINGGROUP:	$this->_readMsoDrawingGroup();	break;
				case self::XLS_Type_SST:			$this->_readSst();				break;
				case self::XLS_Type_EOF:			$this->_readDefault();			break 2;
				default:							$this->_readDefault();			break;
			}
		}

		// Resolve indexed colors for font, fill, and border colors
		// Cannot be resolved already in XF record, because PALETTE record comes afterwards
		if (!$this->_readDataOnly) {
			foreach ($this->_objFonts as $objFont) {
				if (isset($objFont->colorIndex)) {
					$color = self::_readColor($objFont->colorIndex,$this->_palette,$this->_version);
					$objFont->getColor()->setRGB($color['rgb']);
				}
			}

			foreach ($this->_phpExcel->getCellXfCollection() as $objStyle) {
				// fill start and end color
				$fill = $objStyle->getFill();

				if (isset($fill->startcolorIndex)) {
					$startColor = self::_readColor($fill->startcolorIndex,$this->_palette,$this->_version);
					$fill->getStartColor()->setRGB($startColor['rgb']);
				}

				if (isset($fill->endcolorIndex)) {
					$endColor = self::_readColor($fill->endcolorIndex,$this->_palette,$this->_version);
					$fill->getEndColor()->setRGB($endColor['rgb']);
				}

				// border colors
				$top      = $objStyle->getBorders()->getTop();
				$right    = $objStyle->getBorders()->getRight();
				$bottom   = $objStyle->getBorders()->getBottom();
				$left     = $objStyle->getBorders()->getLeft();
				$diagonal = $objStyle->getBorders()->getDiagonal();

				if (isset($top->colorIndex)) {
					$borderTopColor = self::_readColor($top->colorIndex,$this->_palette,$this->_version);
					$top->getColor()->setRGB($borderTopColor['rgb']);
				}

				if (isset($right->colorIndex)) {
					$borderRightColor = self::_readColor($right->colorIndex,$this->_palette,$this->_version);
					$right->getColor()->setRGB($borderRightColor['rgb']);
				}

				if (isset($bottom->colorIndex)) {
					$borderBottomColor = self::_readColor($bottom->colorIndex,$this->_palette,$this->_version);
					$bottom->getColor()->setRGB($borderBottomColor['rgb']);
				}

				if (isset($left->colorIndex)) {
					$borderLeftColor = self::_readColor($left->colorIndex,$this->_palette,$this->_version);
					$left->getColor()->setRGB($borderLeftColor['rgb']);
				}

				if (isset($diagonal->colorIndex)) {
					$borderDiagonalColor = self::_readColor($diagonal->colorIndex,$this->_palette,$this->_version);
					$diagonal->getColor()->setRGB($borderDiagonalColor['rgb']);
				}
			}
		}

		// treat MSODRAWINGGROUP records, workbook-level Escher
		if (!$this->_readDataOnly && $this->_drawingGroupData) {
			$escherWorkbook = new PHPExcel_Shared_Escher();
			$reader = new PHPExcel_Reader_Excel5_Escher($escherWorkbook);
			$escherWorkbook = $reader->load($this->_drawingGroupData);

			// debug Escher stream
			//$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
			//$debug->load($this->_drawingGroupData);
		}

		// Parse the individual sheets
		foreach ($this->_sheets as $sheet) {

			if ($sheet['sheetType'] != 0x00) {
				// 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
				continue;
			}

			// check if sheet should be skipped
			if (isset($this->_loadSheetsOnly) && !in_array($sheet['name'], $this->_loadSheetsOnly)) {
				continue;
			}

			// add sheet to PHPExcel object
			$this->_phpSheet = $this->_phpExcel->createSheet();
			$this->_phpSheet->setTitle($sheet['name']);
			$this->_phpSheet->setSheetState($sheet['sheetState']);

			$this->_pos = $sheet['offset'];

			// Initialize isFitToPages. May change after reading SHEETPR record.
			$this->_isFitToPages = false;

			// Initialize drawingData
			$this->_drawingData = '';

			// Initialize objs
			$this->_objs = array();

			// Initialize shared formula parts
			$this->_sharedFormulaParts = array();

			// Initialize shared formulas
			$this->_sharedFormulas = array();

			// Initialize text objs
			$this->_textObjects = array();

			// Initialize cell annotations
			$this->_cellNotes = array();
			$this->textObjRef = -1;

			while ($this->_pos <= $this->_dataSize - 4) {
				$code = self::_GetInt2d($this->_data, $this->_pos);

				switch ($code) {
					case self::XLS_Type_BOF:					$this->_readBof();						break;
					case self::XLS_Type_PRINTGRIDLINES:			$this->_readPrintGridlines();			break;
					case self::XLS_Type_DEFAULTROWHEIGHT:		$this->_readDefaultRowHeight();			break;
					case self::XLS_Type_SHEETPR:				$this->_readSheetPr();					break;
					case self::XLS_Type_HORIZONTALPAGEBREAKS:	$this->_readHorizontalPageBreaks();		break;
					case self::XLS_Type_VERTICALPAGEBREAKS:		$this->_readVerticalPageBreaks();		break;
					case self::XLS_Type_HEADER:					$this->_readHeader();					break;
					case self::XLS_Type_FOOTER:					$this->_readFooter();					break;
					case self::XLS_Type_HCENTER:				$this->_readHcenter();					break;
					case self::XLS_Type_VCENTER:				$this->_readVcenter();					break;
					case self::XLS_Type_LEFTMARGIN:				$this->_readLeftMargin();				break;
					case self::XLS_Type_RIGHTMARGIN:			$this->_readRightMargin();				break;
					case self::XLS_Type_TOPMARGIN:				$this->_readTopMargin();				break;
					case self::XLS_Type_BOTTOMMARGIN:			$this->_readBottomMargin();				break;
					case self::XLS_Type_PAGESETUP:				$this->_readPageSetup();				break;
					case self::XLS_Type_PROTECT:				$this->_readProtect();					break;
					case self::XLS_Type_SCENPROTECT:			$this->_readScenProtect();				break;
					case self::XLS_Type_OBJECTPROTECT:			$this->_readObjectProtect();			break;
					case self::XLS_Type_PASSWORD:				$this->_readPassword();					break;
					case self::XLS_Type_DEFCOLWIDTH:			$this->_readDefColWidth();				break;
					case self::XLS_Type_COLINFO:				$this->_readColInfo();					break;
					case self::XLS_Type_DIMENSION:				$this->_readDefault();					break;
					case self::XLS_Type_ROW:					$this->_readRow();						break;
					case self::XLS_Type_DBCELL:					$this->_readDefault();					break;
					case self::XLS_Type_RK:						$this->_readRk();						break;
					case self::XLS_Type_LABELSST:				$this->_readLabelSst();					break;
					case self::XLS_Type_MULRK:					$this->_readMulRk();					break;
					case self::XLS_Type_NUMBER:					$this->_readNumber();					break;
					case self::XLS_Type_FORMULA:				$this->_readFormula();					break;
					case self::XLS_Type_SHAREDFMLA:				$this->_readSharedFmla();				break;
					case self::XLS_Type_BOOLERR:				$this->_readBoolErr();					break;
					case self::XLS_Type_MULBLANK:				$this->_readMulBlank();					break;
					case self::XLS_Type_LABEL:					$this->_readLabel();					break;
					case self::XLS_Type_BLANK:					$this->_readBlank();					break;
					case self::XLS_Type_MSODRAWING:				$this->_readMsoDrawing();				break;
					case self::XLS_Type_OBJ:					$this->_readObj();						break;
					case self::XLS_Type_WINDOW2:				$this->_readWindow2();					break;
					case self::XLS_Type_SCL:					$this->_readScl();						break;
					case self::XLS_Type_PANE:					$this->_readPane();						break;
					case self::XLS_Type_SELECTION:				$this->_readSelection();				break;
					case self::XLS_Type_MERGEDCELLS:			$this->_readMergedCells();				break;
					case self::XLS_Type_HYPERLINK:				$this->_readHyperLink();				break;
					case self::XLS_Type_DATAVALIDATIONS:		$this->_readDataValidations();			break;
					case self::XLS_Type_DATAVALIDATION:			$this->_readDataValidation();			break;
					case self::XLS_Type_SHEETLAYOUT:			$this->_readSheetLayout();				break;
					case self::XLS_Type_SHEETPROTECTION:		$this->_readSheetProtection();			break;
					case self::XLS_Type_RANGEPROTECTION:		$this->_readRangeProtection();			break;
					case self::XLS_Type_NOTE:					$this->_readNote();						break;
					//case self::XLS_Type_IMDATA:				$this->_readImData();					break;
					case self::XLS_Type_TXO:					$this->_readTextObject();				break;
					case self::XLS_Type_CONTINUE:				$this->_readContinue();					break;
					case self::XLS_Type_EOF:					$this->_readDefault();					break 2;
					default:									$this->_readDefault();					break;
				}

			}

			// treat MSODRAWING records, sheet-level Escher
			if (!$this->_readDataOnly && $this->_drawingData) {
				$escherWorksheet = new PHPExcel_Shared_Escher();
				$reader = new PHPExcel_Reader_Excel5_Escher($escherWorksheet);
				$escherWorksheet = $reader->load($this->_drawingData);

				// debug Escher stream
				//$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
				//$debug->load($this->_drawingData);

				// get all spContainers in one long array, so they can be mapped to OBJ records
				$allSpContainers = $escherWorksheet->getDgContainer()->getSpgrContainer()->getAllSpContainers();
			}

			// treat OBJ records
			foreach ($this->_objs as $n => $obj) {
//				echo '<hr /><b>Object</b> reference is ',$n,'<br />';
//				var_dump($obj);
//				echo '<br />';

				// the first shape container never has a corresponding OBJ record, hence $n + 1
				$spContainer = $allSpContainers[$n + 1];

				// we skip all spContainers that are a part of a group shape since we cannot yet handle those
				if ($spContainer->getNestingLevel() > 1) {
					continue;
				}

				// calculate the width and height of the shape
				list($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($spContainer->getStartCoordinates());
				list($endColumn, $endRow) = PHPExcel_Cell::coordinateFromString($spContainer->getEndCoordinates());

				$startOffsetX = $spContainer->getStartOffsetX();
				$startOffsetY = $spContainer->getStartOffsetY();
				$endOffsetX = $spContainer->getEndOffsetX();
				$endOffsetY = $spContainer->getEndOffsetY();

				$width = PHPExcel_Shared_Excel5::getDistanceX($this->_phpSheet, $startColumn, $startOffsetX, $endColumn, $endOffsetX);
				$height = PHPExcel_Shared_Excel5::getDistanceY($this->_phpSheet, $startRow, $startOffsetY, $endRow, $endOffsetY);

				// calculate offsetX and offsetY of the shape
				$offsetX = $startOffsetX * PHPExcel_Shared_Excel5::sizeCol($this->_phpSheet, $startColumn) / 1024;
				$offsetY = $startOffsetY * PHPExcel_Shared_Excel5::sizeRow($this->_phpSheet, $startRow) / 256;

				switch ($obj['otObjType']) {

				case 0x19:
					// Note
//					echo 'Cell Annotation Object<br />';
//					echo 'Object ID is ',$obj['idObjID'],'<br />';
//
					if (isset($this->_cellNotes[$obj['idObjID']])) {
						$cellNote = $this->_cellNotes[$obj['idObjID']];

//						echo '_cellNotes[',$obj['idObjID'],']: ';
//						var_dump($cellNote);
//						echo '<br />';
//
						if (isset($this->_textObjects[$obj['idObjID']])) {
							$textObject = $this->_textObjects[$obj['idObjID']];
//							echo '_textObject: ';
//							var_dump($textObject);
//							echo '<br />';
//
							$this->_cellNotes[$obj['idObjID']]['objTextData'] = $textObject;
							$text = $textObject['text'];
						}
//						echo $text,'<br />';
					}
					break;

				case 0x08:
//					echo 'Picture Object<br />';
					// picture

					// get index to BSE entry (1-based)
					$BSEindex = $spContainer->getOPT(0x0104);
					$BSECollection = $escherWorkbook->getDggContainer()->getBstoreContainer()->getBSECollection();
					$BSE = $BSECollection[$BSEindex - 1];
					$blipType = $BSE->getBlipType();

					// need check because some blip types are not supported by Escher reader such as EMF
					if ($blip = $BSE->getBlip()) {
						$ih = imagecreatefromstring($blip->getData());
						$drawing = new PHPExcel_Worksheet_MemoryDrawing();
						$drawing->setImageResource($ih);

						// width, height, offsetX, offsetY
						$drawing->setResizeProportional(false);
						$drawing->setWidth($width);
						$drawing->setHeight($height);
						$drawing->setOffsetX($offsetX);
						$drawing->setOffsetY($offsetY);

						switch ($blipType) {
							case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG:
								$drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
								$drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG);
								break;

							case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG:
								$drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
								$drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG);
								break;
						}

						$drawing->setWorksheet($this->_phpSheet);
						$drawing->setCoordinates($spContainer->getStartCoordinates());
					}

					break;

				default:
					// other object type
					break;

				}
			}

			// treat SHAREDFMLA records
			if ($this->_version == self::XLS_BIFF8) {
				foreach ($this->_sharedFormulaParts as $cell => $baseCell) {
					list($column, $row) = PHPExcel_Cell::coordinateFromString($cell);
					if ( !is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($column, $row, $this->_phpSheet->getTitle()) ) {
						$formula = $this->_getFormulaFromStructure($this->_sharedFormulas[$baseCell], $cell);
						$this->_phpSheet->getCell($cell)->setValueExplicit('=' . $formula, PHPExcel_Cell_DataType::TYPE_FORMULA);
					}
				}
			}

			if (count($this->_cellNotes) > 0) {
				foreach($this->_cellNotes as $note => $noteDetails) {
//					echo '<b>Cell annotation ',$note,'</b><br />';
//					var_dump($noteDetails);
//					echo '<br />';
					$cellAddress = str_replace('$','',$noteDetails['cellRef']);
					$this->_phpSheet->getComment( $cellAddress )
													->setAuthor( $noteDetails['author'] )
													->setText($this->_parseRichText($noteDetails['objTextData']['text']) );
				}
			}
		}

		// add the named ranges (defined names)
		foreach ($this->_definedname as $definedName) {
			if ($definedName['isBuiltInName']) {
				switch ($definedName['name']) {

				case pack('C', 0x06):
					// print area
					//	in general, formula looks like this: Foo!$C$7:$J$66,Bar!$A$1:$IV$2

					$ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?

					$extractedRanges = array();
					foreach ($ranges as $range) {
						// $range should look like one of these
						//		Foo!$C$7:$J$66
						//		Bar!$A$1:$IV$2

						$explodes = explode('!', $range);	// FIXME: what if sheetname contains exclamation mark?
						$sheetName = $explodes[0];

						if (count($explodes) == 2) {
							$extractedRanges[] = str_replace('$', '', $explodes[1]); // C7:J66
						}
					}
					if ($docSheet = $this->_phpExcel->getSheetByName($sheetName)) {
						$docSheet->getPageSetup()->setPrintArea(implode(',', $extractedRanges)); // C7:J66,A1:IV2
					}
					break;

				case pack('C', 0x07):
					// print titles (repeating rows)
					// Assuming BIFF8, there are 3 cases
					// 1. repeating rows
					//		formula looks like this: Sheet!$A$1:$IV$2
					//		rows 1-2 repeat
					// 2. repeating columns
					//		formula looks like this: Sheet!$A$1:$B$65536
					//		columns A-B repeat
					// 3. both repeating rows and repeating columns
					//		formula looks like this: Sheet!$A$1:$B$65536,Sheet!$A$1:$IV$2

					$ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?

					foreach ($ranges as $range) {
						// $range should look like this one of these
						//		Sheet!$A$1:$B$65536
						//		Sheet!$A$1:$IV$2

						$explodes = explode('!', $range);

						if (count($explodes) == 2) {
							if ($docSheet = $this->_phpExcel->getSheetByName($explodes[0])) {

								$extractedRange = $explodes[1];
								$extractedRange = str_replace('$', '', $extractedRange);

								$coordinateStrings = explode(':', $extractedRange);
								if (count($coordinateStrings) == 2) {
									list($firstColumn, $firstRow) = PHPExcel_Cell::coordinateFromString($coordinateStrings[0]);
									list($lastColumn, $lastRow) = PHPExcel_Cell::coordinateFromString($coordinateStrings[1]);

									if ($firstColumn == 'A' and $lastColumn == 'IV') {
										// then we have repeating rows
										$docSheet->getPageSetup()->setRowsToRepeatAtTop(array($firstRow, $lastRow));
									} elseif ($firstRow == 1 and $lastRow == 65536) {
										// then we have repeating columns
										$docSheet->getPageSetup()->setColumnsToRepeatAtLeft(array($firstColumn, $lastColumn));
									}
								}
							}
						}
					}
					break;

				}
			} else {
				// Extract range
				$explodes = explode('!', $definedName['formula']);

				if (count($explodes) == 2) {
					if ($docSheet = $this->_phpExcel->getSheetByName($explodes[0])) {
						$extractedRange = $explodes[1];
						$extractedRange = str_replace('$', '', $extractedRange);

						$localOnly = ($definedName['scope'] == 0) ? false : true;
						$scope = ($definedName['scope'] == 0) ?
							null : $this->_phpExcel->getSheetByName($this->_sheets[$definedName['scope'] - 1]['name']);

						$this->_phpExcel->addNamedRange( new PHPExcel_NamedRange((string)$definedName['name'], $docSheet, $extractedRange, $localOnly, $scope) );
					}
				}
			}
		}

		return $this->_phpExcel;
	}

	/**
	 * Use OLE reader to extract the relevant data streams from the OLE file
	 *
	 * @param string $pFilename
	 */
	private function _loadOLE($pFilename)
	{
		// OLE reader
		$ole = new PHPExcel_Shared_OLERead();

		// get excel data,
		$res = $ole->read($pFilename);
		// Get workbook data: workbook stream + sheet streams
		$this->_data = $ole->getStream($ole->wrkbook);

		// Get summary information data
		$this->_summaryInformation = $ole->getStream($ole->summaryInformation);

		// Get additional document summary information data
		$this->_documentSummaryInformation = $ole->getStream($ole->documentSummaryInformation);

		// Get user-defined property data
//		$this->_userDefinedProperties = $ole->getUserDefinedProperties();
	}

	/**
	 * Read summary information
	 */
	private function _readSummaryInformation()
	{
		if (!isset($this->_summaryInformation)) {
			return;
		}

		// offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
		// offset: 2; size: 2;
		// offset: 4; size: 2; OS version
		// offset: 6; size: 2; OS indicator
		// offset: 8; size: 16
		// offset: 24; size: 4; section count
		$secCount = self::_GetInt4d($this->_summaryInformation, 24);

		// offset: 28; size: 16; first section's class id: e0 85 9f f2 f9 4f 68 10 ab 91 08 00 2b 27 b3 d9
		// offset: 44; size: 4
		$secOffset = self::_GetInt4d($this->_summaryInformation, 44);

		// section header
		// offset: $secOffset; size: 4; section length
		$secLength = self::_GetInt4d($this->_summaryInformation, $secOffset);

		// offset: $secOffset+4; size: 4; property count
		$countProperties = self::_GetInt4d($this->_summaryInformation, $secOffset+4);

		// initialize code page (used to resolve string values)
		$codePage = 'CP1252';

		// offset: ($secOffset+8); size: var
		// loop through property decarations and properties
		for ($i = 0; $i < $countProperties; ++$i) {

			// offset: ($secOffset+8) + (8 * $i); size: 4; property ID
			$id = self::_GetInt4d($this->_summaryInformation, ($secOffset+8) + (8 * $i));

			// Use value of property id as appropriate
			// offset: ($secOffset+12) + (8 * $i); size: 4; offset from beginning of section (48)
			$offset = self::_GetInt4d($this->_summaryInformation, ($secOffset+12) + (8 * $i));

			$type = self::_GetInt4d($this->_summaryInformation, $secOffset + $offset);

			// initialize property value
			$value = null;

			// extract property value based on property type
			switch ($type) {
				case 0x02: // 2 byte signed integer
					$value = self::_GetInt2d($this->_summaryInformation, $secOffset + 4 + $offset);
					break;

				case 0x03: // 4 byte signed integer
					$value = self::_GetInt4d($this->_summaryInformation, $secOffset + 4 + $offset);
					break;

				case 0x13: // 4 byte unsigned integer
					// not needed yet, fix later if necessary
					break;

				case 0x1E: // null-terminated string prepended by dword string length
					$byteLength = self::_GetInt4d($this->_summaryInformation, $secOffset + 4 + $offset);
					$value = substr($this->_summaryInformation, $secOffset + 8 + $offset, $byteLength);
					$value = PHPExcel_Shared_String::ConvertEncoding($value, 'UTF-8', $codePage);
					$value = rtrim($value);
					break;

				case 0x40: // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
					// PHP-time
					$value = PHPExcel_Shared_OLE::OLE2LocalDate(substr($this->_summaryInformation, $secOffset + 4 + $offset, 8));
					break;

				case 0x47: // Clipboard format
					// not needed yet, fix later if necessary
					break;
			}

			switch ($id) {
				case 0x01:	//	Code Page
					$codePage = PHPExcel_Shared_CodePage::NumberToName($value);
					break;

				case 0x02:	//	Title
					$this->_phpExcel->getProperties()->setTitle($value);
					break;

				case 0x03:	//	Subject
					$this->_phpExcel->getProperties()->setSubject($value);
					break;

				case 0x04:	//	Author (Creator)
					$this->_phpExcel->getProperties()->setCreator($value);
					break;

				case 0x05:	//	Keywords
					$this->_phpExcel->getProperties()->setKeywords($value);
					break;

				case 0x06:	//	Comments (Description)
					$this->_phpExcel->getProperties()->setDescription($value);
					break;

				case 0x07:	//	Template
					//	Not supported by PHPExcel
					break;

				case 0x08:	//	Last Saved By (LastModifiedBy)
					$this->_phpExcel->getProperties()->setLastModifiedBy($value);
					break;

				case 0x09:	//	Revision
					//	Not supported by PHPExcel
					break;

				case 0x0A:	//	Total Editing Time
					//	Not supported by PHPExcel
					break;

				case 0x0B:	//	Last Printed
					//	Not supported by PHPExcel
					break;

				case 0x0C:	//	Created Date/Time
					$this->_phpExcel->getProperties()->setCreated($value);
					break;

				case 0x0D:	//	Modified Date/Time
					$this->_phpExcel->getProperties()->setModified($value);
					break;

				case 0x0E:	//	Number of Pages
					//	Not supported by PHPExcel
					break;

				case 0x0F:	//	Number of Words
					//	Not supported by PHPExcel
					break;

				case 0x10:	//	Number of Characters
					//	Not supported by PHPExcel
					break;

				case 0x11:	//	Thumbnail
					//	Not supported by PHPExcel
					break;

				case 0x12:	//	Name of creating application
					//	Not supported by PHPExcel
					break;

				case 0x13:	//	Security
					//	Not supported by PHPExcel
					break;

			}
		}
	}

	/**
	 * Read additional document summary information
	 */
	private function _readDocumentSummaryInformation()
	{
		if (!isset($this->_documentSummaryInformation)) {
			return;
		}

		//	offset: 0;	size: 2;	must be 0xFE 0xFF (UTF-16 LE byte order mark)
		//	offset: 2;	size: 2;
		//	offset: 4;	size: 2;	OS version
		//	offset: 6;	size: 2;	OS indicator
		//	offset: 8;	size: 16
		//	offset: 24;	size: 4;	section count
		$secCount = self::_GetInt4d($this->_documentSummaryInformation, 24);
//		echo '$secCount = ',$secCount,'<br />';

		// offset: 28;	size: 16;	first section's class id: 02 d5 cd d5 9c 2e 1b 10 93 97 08 00 2b 2c f9 ae
		// offset: 44;	size: 4;	first section offset
		$secOffset = self::_GetInt4d($this->_documentSummaryInformation, 44);
//		echo '$secOffset = ',$secOffset,'<br />';

		//	section header
		//	offset: $secOffset;	size: 4;	section length
		$secLength = self::_GetInt4d($this->_documentSummaryInformation, $secOffset);
//		echo '$secLength = ',$secLength,'<br />';

		//	offset: $secOffset+4;	size: 4;	property count
		$countProperties = self::_GetInt4d($this->_documentSummaryInformation, $secOffset+4);
//		echo '$countProperties = ',$countProperties,'<br />';

		// initialize code page (used to resolve string values)
		$codePage = 'CP1252';

		//	offset: ($secOffset+8);	size: var
		//	loop through property decarations and properties
		for ($i = 0; $i < $countProperties; ++$i) {
//			echo 'Property ',$i,'<br />';
			//	offset: ($secOffset+8) + (8 * $i);	size: 4;	property ID
			$id = self::_GetInt4d($this->_documentSummaryInformation, ($secOffset+8) + (8 * $i));
//			echo 'ID is ',$id,'<br />';

			// Use value of property id as appropriate
			// offset: 60 + 8 * $i;	size: 4;	offset from beginning of section (48)
			$offset = self::_GetInt4d($this->_documentSummaryInformation, ($secOffset+12) + (8 * $i));

			$type = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + $offset);
//			echo 'Type is ',$type,', ';

			// initialize property value
			$value = null;

			// extract property value based on property type
			switch ($type) {
				case 0x02:	//	2 byte signed integer
					$value = self::_GetInt2d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
					break;

				case 0x03:	//	4 byte signed integer
					$value = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
					break;

				case 0x13:	//	4 byte unsigned integer
					// not needed yet, fix later if necessary
					break;

				case 0x1E:	//	null-terminated string prepended by dword string length
					$byteLength = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
					$value = substr($this->_documentSummaryInformation, $secOffset + 8 + $offset, $byteLength);
					$value = PHPExcel_Shared_String::ConvertEncoding($value, 'UTF-8', $codePage);
					$value = rtrim($value);
					break;

				case 0x40:	//	Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
					// PHP-Time
					$value = PHPExcel_Shared_OLE::OLE2LocalDate(substr($this->_documentSummaryInformation, $secOffset + 4 + $offset, 8));
					break;

				case 0x47:	//	Clipboard format
					// not needed yet, fix later if necessary
					break;
			}

			switch ($id) {
				case 0x01:	//	Code Page
					$codePage = PHPExcel_Shared_CodePage::NumberToName($value);
					break;

				case 0x02:	//	Category
					$this->_phpExcel->getProperties()->setCategory($value);
					break;

				case 0x03:	//	Presentation Target
					//	Not supported by PHPExcel
					break;

				case 0x04:	//	Bytes
					//	Not supported by PHPExcel
					break;

				case 0x05:	//	Lines
					//	Not supported by PHPExcel
					break;

				case 0x06:	//	Paragraphs
					//	Not supported by PHPExcel
					break;

				case 0x07:	//	Slides
					//	Not supported by PHPExcel
					break;

				case 0x08:	//	Notes
					//	Not supported by PHPExcel
					break;

				case 0x09:	//	Hidden Slides
					//	Not supported by PHPExcel
					break;

				case 0x0A:	//	MM Clips
					//	Not supported by PHPExcel
					break;

				case 0x0B:	//	Scale Crop
					//	Not supported by PHPExcel
					break;

				case 0x0C:	//	Heading Pairs
					//	Not supported by PHPExcel
					break;

				case 0x0D:	//	Titles of Parts
					//	Not supported by PHPExcel
					break;

				case 0x0E:	//	Manager
					$this->_phpExcel->getProperties()->setManager($value);
					break;

				case 0x0F:	//	Company
					$this->_phpExcel->getProperties()->setCompany($value);
					break;

				case 0x10:	//	Links up-to-date
					//	Not supported by PHPExcel
					break;

			}
		}
	}

	/**
	 * Reads a general type of BIFF record. Does nothing except for moving stream pointer forward to next record.
	 */
	private function _readDefault()
	{
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
//		$recordData = substr($this->_data, $this->_pos + 4, $length);

		// move stream pointer to next record
		$this->_pos += 4 + $length;
	}


	/**
	 *	The NOTE record specifies a comment associated with a particular cell. In Excel 95 (BIFF7) and earlier versions,
	 *		this record stores a note (cell note). This feature was significantly enhanced in Excel 97.
	 */
	private function _readNote()
	{
//		echo '<b>Read Cell Annotation</b><br />';
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
		$recordData = substr($this->_data, $this->_pos + 4, $length);

		// move stream pointer to next record
		$this->_pos += 4 + $length;

		if ($this->_readDataOnly) {
			return;
		}

		$cellAddress = $this->_readBIFF8CellAddress(substr($recordData, 0, 4));
		if ($this->_version == self::XLS_BIFF8) {
			$noteObjID = self::_GetInt2d($recordData, 6);
			$noteAuthor = self::_readUnicodeStringLong(substr($recordData, 8));
			$noteAuthor = $noteAuthor['value'];
//			echo 'Note Address=',$cellAddress,'<br />';
//			echo 'Note Object ID=',$noteObjID,'<br />';
//			echo 'Note Author=',$noteAuthor,'<hr />';
//
			$this->_cellNotes[$noteObjID] = array('cellRef'		=> $cellAddress,
												  'objectID'	=> $noteObjID,
												  'author'		=> $noteAuthor
												 );
		} else {
			$extension = false;
			if ($cellAddress == '$B$65536') {
				//	If the address row is -1 and the column is 0, (which translates as $B$65536) then this is a continuation
				//		note from the previous cell annotation. We're not yet handling this, so annotations longer than the
				//		max 2048 bytes will probably throw a wobbly.
				$row = self::_GetInt2d($recordData, 0);
				$extension = true;
				$cellAddress = array_pop(array_keys($this->_phpSheet->getComments()));
			}
//			echo 'Note Address=',$cellAddress,'<br />';

			$cellAddress = str_replace('$','',$cellAddress);
			$noteLength = self::_GetInt2d($recordData, 4);
			$noteText = trim(substr($recordData, 6));
//			echo 'Note Length=',$noteLength,'<br />';
//			echo 'Note Text=',$noteText,'<br />';

			if ($extension) {
				//	Concatenate this extension with the currently set comment for the cell
				$comment = $this->_phpSheet->getComment( $cellAddress );
				$commentText = $comment->getText()->getPlainText();
				$comment->setText($this->_parseRichText($commentText.$noteText) );
			} else {
				//	Set comment for the cell
				$this->_phpSheet->getComment( $cellAddress )
//													->setAuthor( $author )
													->setText($this->_parseRichText($noteText) );
			}
		}

	}

	/**
	 *	The TEXT Object record contains the text associated with a cell annotation.
	 */
	private function _readTextObject()
	{
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
		$recordData = substr($this->_data, $this->_pos + 4, $length);

		// move stream pointer to next record
		$this->_pos += 4 + $length;

		if ($this->_readDataOnly) {
			return;
		}

		// recordData consists of an array of subrecords looking like this:
		//	grbit: 2 bytes; Option Flags
		//	rot: 2 bytes; rotation
		//	cchText: 2 bytes; length of the text (in the first continue record)
		//	cbRuns: 2 bytes; length of the formatting (in the second continue record)
		// followed by the continuation records containing the actual text and formatting
		$grbitOpts	= self::_GetInt2d($recordData, 0);
		$rot		= self::_GetInt2d($recordData, 2);
		$cchText	= self::_GetInt2d($recordData, 10);
		$cbRuns		= self::_GetInt2d($recordData, 12);
		$text		= $this->_getSplicedRecordData();

		$this->_textObjects[$this->textObjRef] = array(
				'text'		=> substr($text["recordData"],$text["spliceOffsets"][0]+1,$cchText),
				'format'	=> substr($text["recordData"],$text["spliceOffsets"][1],$cbRuns),
				'alignment'	=> $grbitOpts,
				'rotation'	=> $rot
			 );

//		echo '<b>_readTextObject()</b><br />';
//		var_dump($this->_textObjects[$this->textObjRef]);
//		echo '<br />';
	}

	/**
	 * Read BOF
	 */
	private function _readBof()
	{
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
		$recordData = substr($this->_data, $this->_pos + 4, $length);

		// move stream pointer to next record
		$this->_pos += 4 + $length;

		// offset: 2; size: 2; type of the following data
		$substreamType = self::_GetInt2d($recordData, 2);

		switch ($substreamType) {
			case self::XLS_WorkbookGlobals:
				$version = self::_GetInt2d($recordData, 0);
				if (($version != self::XLS_BIFF8) && ($version != self::XLS_BIFF7)) {
					throw new Exception('Cannot read this Excel file. Version is too old.');
				}
				$this->_version = $version;
				break;

			case self::XLS_Worksheet:
				// do not use this version information for anything
				// it is unreliable (OpenOffice doc, 5.8), use only version information from the global stream
				break;

			default:
				// substream, e.g. chart
				// just skip the entire substream
				do {
					$code = self::_GetInt2d($this->_data, $this->_pos);
					$this->_readDefault();
				} while ($code != self::XLS_Type_EOF && $this->_pos < $this->_dataSize);
				break;
		}
	}

	/**
	 * FILEPASS
	 *
	 * This record is part of the File Protection Block. It
	 * contains information about the read/write password of the
	 * file. All record contents following this record will be
	 * encrypted.
	 *
	 * --	"OpenOffice.org's Documentation of the Microsoft
	 * 		Excel File Format"
	 */
	private function _readFilepass()
	{
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
//		$recordData = substr($this->_data, $this->_pos + 4, $length);

		// move stream pointer to next record
		$this->_pos += 4 + $length;

		throw new Exception('Cannot read encrypted file');
	}

	/**
	 * CODEPAGE
	 *
	 * This record stores the text encoding used to write byte
	 * strings, stored as MS Windows code page identifier.
	 *
	 * --	"OpenOffice.org's Documentation of the Microsoft
	 * 		Excel File Format"
	 */
	private function _readCodepage()
	{
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
		$recordData = substr($this->_data, $this->_pos + 4, $length);

		// move stream pointer to next record
		$this->_pos += 4 + $length;

		// offset: 0; size: 2; code page identifier
		$codepage = self::_GetInt2d($recordData, 0);

		$this->_codepage = PHPExcel_Shared_CodePage::NumberToName($codepage);
	}

	/**
	 * DATEMODE
	 *
	 * This record specifies the base date for displaying date
	 * values. All dates are stored as count of days past this
	 * base date. In BIFF2-BIFF4 this record is part of the
	 * Calculation Settings Block. In BIFF5-BIFF8 it is
	 * stored in the Workbook Globals Substream.
	 *
	 * --	"OpenOffice.org's Documentation of the Microsoft
	 * 		Excel File Format"
	 */
	private function _readDateMode()
	{
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
		$recordData = substr($this->_data, $this->_pos + 4, $length);

		// move stream pointer to next record
		$this->_pos += 4 + $length;

		// offset: 0; size: 2; 0 = base 1900, 1 = base 1904
		PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900);
		if (ord($recordData{0}) == 1) {
			PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_MAC_1904);
		}
	}

	/**
	 * Read a FONT record
	 */
	private function _readFont()
	{
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
		$recordData = substr($this->_data, $this->_pos + 4, $length);

		// move stream pointer to next record
		$this->_pos += 4 + $length;

		if (!$this->_readDataOnly) {
			$objFont = new PHPExcel_Style_Font();

			// offset: 0; size: 2; height of the font (in twips = 1/20 of a point)
			$size = self::_GetInt2d($recordData, 0);
			$objFont->setSize($size / 20);

			// offset: 2; size: 2; option flags
				// bit: 0; mask 0x0001; bold (redundant in BIFF5-BIFF8)
				// bit: 1; mask 0x0002; italic
				$isItalic = (0x0002 & self::_GetInt2d($recordData, 2)) >> 1;
				if ($isItalic) $objFont->setItalic(true);

				// bit: 2; mask 0x0004; underlined (redundant in BIFF5-BIFF8)
				// bit: 3; mask 0x0008; strike
				$isStrike = (0x0008 & self::_GetInt2d($recordData, 2)) >> 3;
				if ($isStrike) $objFont->setStrikethrough(true);

			// offset: 4; size: 2; colour index
			$colorIndex = self::_GetInt2d($recordData, 4);
			$objFont->colorIndex = $colorIndex;

			// offset: 6; size: 2; font weight
			$weight = self::_GetInt2d($recordData, 6);
			switch ($weight) {
				case 0x02BC:
					$objFont->setBold(true);
					break;
			}

			// offset: 8; size: 2; escapement type
			$escapement = self::_GetInt2d($recordData, 8);
			switch ($escapement) {
				case 0x0001:
					$objFont->setSuperScript(true);
					break;
				case 0x0002:
					$objFont->setSubScript(true);
					break;
			}

			// offset: 10; size: 1; underline type
			$underlineType = ord($recordData{10});
			switch ($underlineType) {
				case 0x00:
					break; // no underline
				case 0x01:
					$objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
					break;
				case 0x02:
					$objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLE);
					break;
				case 0x21:
					$objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING);
					break;
				case 0x22:
					$objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING);
					break;
			}

			// offset: 11; size: 1; font family
			// offset: 12; size: 1; character set
			// offset: 13; size: 1; not used
			// offset: 14; size: var; font name
			if ($this->_version == self::XLS_BIFF8) {
				$string = self::_readUnicodeStringShort(substr($recordData, 14));
			} else {
				$string = $this->_readByteStringShort(substr($recordData, 14));
			}
			$objFont->setName($string['value']);

			$this->_objFonts[] = $objFont;
		}
	}

	/**
	 * FORMAT
	 *
	 * This record contains information about a number format.
	 * All FORMAT records occur together in a sequential list.
	 *
	 * In BIFF2-BIFF4 other records referencing a FORMAT record
	 * contain a zero-based index into this list. From BIFF5 on
	 * the FORMAT record contains the index itself that will be
	 * used by other records.
	 *
	 * --	"OpenOffice.org's Documentation of the Microsoft
	 * 		Excel File Format"
	 */
	private function _readFormat()
	{
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
		$recordData = substr($this->_data, $this->_pos + 4, $length);

		// move stream pointer to next record
		$this->_pos += 4 + $length;

		if (!$this->_readDataOnly) {
			$indexCode = self::_GetInt2d($recordData, 0);

			if ($this->_version == self::XLS_BIFF8) {
				$string = self::_readUnicodeStringLong(substr($recordData, 2));
			} else {
				// BIFF7
				$string = $this->_readByteStringShort(substr($recordData, 2));
			}

			$formatString = $string['value'];
			$this->_formats[$indexCode] = $formatString;
		}
	}

	/**
	 * XF - Extended Format
	 *
	 * This record contains formatting information for cells, rows, columns or styles.
	 * According to http://support.microsoft.com/kb/147732 there are always at least 15 cell style XF
	 * and 1 cell XF.
	 * Inspection of Excel files generated by MS Office Excel shows that XF records 0-14 are cell style XF
	 * and XF record 15 is a cell XF
	 * We only read the first cell style XF and skip the remaining cell style XF records
	 * We read all cell XF records.
	 *
	 * --	"OpenOffice.org's Documentation of the Microsoft
	 * 		Excel File Format"
	 */
	private function _readXf()
	{
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
		$recordData = substr($this->_data, $this->_pos + 4, $length);

		// move stream pointer to next record
		$this->_pos += 4 + $length;

		$objStyle = new PHPExcel_Style();

		if (!$this->_readDataOnly) {
			// offset:  0; size: 2; Index to FONT record
			if (self::_GetInt2d($recordData, 0) < 4) {
				$fontIndex = self::_GetInt2d($recordData, 0);
			} else {
				// this has to do with that index 4 is omitted in all BIFF versions for some strange reason
				// check the OpenOffice documentation of the FONT record
				$fontIndex = self::_GetInt2d($recordData, 0) - 1;
			}
			$objStyle->setFont($this->_objFonts[$fontIndex]);

			// offset:  2; size: 2; Index to FORMAT record
			$numberFormatIndex = self::_GetInt2d($recordData, 2);
			if (isset($this->_formats[$numberFormatIndex])) {
				// then we have user-defined format code
				$numberformat = array('code' => $this->_formats[$numberFormatIndex]);
			} elseif (($code = PHPExcel_Style_NumberFormat::builtInFormatCode($numberFormatIndex)) !== '') {
				// then we have built-in format code
				$numberformat = array('code' => $code);
			} else {
				// we set the general format code
				$numberformat = array('code' => 'General');
			}
			$objStyle->getNumberFormat()->setFormatCode($numberformat['code']);

			// offset:  4; size: 2; XF type, cell protection, and parent style XF
			// bit 2-0; mask 0x0007; XF_TYPE_PROT
			$xfTypeProt = self::_GetInt2d($recordData, 4);
			// bit 0; mask 0x01; 1 = cell is locked
			$isLocked = (0x01 & $xfTypeProt) >> 0;
			$objStyle->getProtection()->setLocked($isLocked ?
				PHPExcel_Style_Protection::PROTECTION_INHERIT : PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);

			// bit 1; mask 0x02; 1 = Formula is hidden
			$isHidden = (0x02 & $xfTypeProt) >> 1;
			$objStyle->getProtection()->setHidden($isHidden ?
				PHPExcel_Style_Protection::PROTECTION_PROTECTED : PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);

			// bit 2; mask 0x04; 0 = Cell XF, 1 = Cell Style XF
			$isCellStyleXf = (0x04 & $xfTypeProt) >> 2;

			// offset:  6; size: 1; Alignment and text break
			// bit 2-0, mask 0x07; horizontal alignment
			$horAlign = (0x07 & ord($recordData{6})) >> 0;
			switch ($horAlign) {
				case 0:
					$objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_GENERAL);
					break;
				case 1:
					$objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
					break;
				case 2:
					$objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
					break;
				case 3:
					$objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
					break;
				case 5:
					$objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
					break;
				case 6:
					$objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS);
					break;
			}
			// bit 3, mask 0x08; wrap text
			$wrapText = (0x08 & ord($recordData{6})) >> 3;
			switch ($wrapText) {
				case 0:
					$objStyle->getAlignment()->setWrapText(false);
					break;
				case 1:
					$objStyle->getAlignment()->setWrapText(true);
					break;
			}
			// bit 6-4, mask 0x70; vertical alignment
			$vertAlign = (0x70 & ord($recordData{6})) >> 4;
			switch ($vertAlign) {
				case 0:
					$objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
					break;
				case 1:
					$objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
					break;
				case 2:
					$objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_BOTTOM);
					break;
				case 3:
					$objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_JUSTIFY);
					break;
			}

			if ($this->_version == self::XLS_BIFF8) {
				// offset:  7; size: 1; XF_ROTATION: Text rotation angle
					$angle = ord($recordData{7});
					$rotation = 0;
					if ($angle <= 90) {
						$rotation = $angle;
					} else if ($angle <= 180) {
						$rotation = 90 - $angle;
					} else if ($angle == 255) {
						$rotation = -165;
					}
					$objStyle->getAlignment()->setTextRotation($rotation);

				// offset:  8; size: 1; Indentation, shrink to cell size, and text direction
					// bit: 3-0; mask: 0x0F; indent level
					$indent = (0x0F & ord($recordData{8})) >> 0;
					$objStyle->getAlignment()->setIndent($indent);

					// bit: 4; mask: 0x10; 1 = shrink content to fit into cell
					$shrinkToFit = (0x10 & ord($recordData{8})) >> 4;
					switch ($shrinkToFit) {
						case 0:
							$objStyle->getAlignment()->setShrinkToFit(false);
							break;
						case 1:
							$objStyle->getAlignment()->setShrinkToFit(true);
							break;
					}

				// offset:  9; size: 1; Flags used for attribute groups

				// offset: 10; size: 4; Cell border lines and background area
					// bit: 3-0; mask: 0x0000000F; left style
					if ($bordersLeftStyle = self::_mapBorderStyle((0x0000000F & self::_GetInt4d($recordData, 10)) >> 0)) {
						$objStyle->getBorders()->getLeft()->setBorderStyle($bordersLeftStyle);
					}
					// bit: 7-4; mask: 0x000000F0; right style
					if ($bordersRightStyle = self::_mapBorderStyle((0x000000F0 & self::_GetInt4d($recordData, 10)) >> 4)) {
						$objStyle->getBorders()->getRight()->setBorderStyle($bordersRightStyle);
					}
					// bit: 11-8; mask: 0x00000F00; top style
					if ($bordersTopStyle = self::_mapBorderStyle((0x00000F00 & self::_GetInt4d($recordData, 10)) >> 8)) {
						$objStyle->getBorders()->getTop()->setBorderStyle($bordersTopStyle);
					}
					// bit: 15-12; mask: 0x0000F000; bottom style
					if ($bordersBottomStyle = self::_mapBorderStyle((0x0000F000 & self::_GetInt4d($recordData, 10)) >> 12)) {
						$objStyle->getBorders()->getBottom()->setBorderStyle($bordersBottomStyle);
					}
					// bit: 22-16; mask: 0x007F0000; left color
					$objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & self::_GetInt4d($recordData, 10)) >> 16;

					// bit: 29-23; mask: 0x3F800000; right color
					$objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & self::_GetInt4d($recordData, 10)) >> 23;

					// bit: 30; mask: 0x40000000; 1 = diagonal line from top left to right bottom
					$diagonalDown = (0x40000000 & self::_GetInt4d($recordData, 10)) >> 30 ?
						true : false;

					// bit: 31; mask: 0x80000000; 1 = diagonal line from bottom left to top right
					$diagonalUp = (0x80000000 & self::_GetInt4d($recordData, 10)) >> 31 ?
						true : false;

					if ($diagonalUp == false && $diagonalDown == false) {
						$objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_NONE);
					} elseif ($diagonalUp == true && $diagonalDown == false) {
						$objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_UP);
					} elseif ($diagonalUp == false && $diagonalDown == true) {
						$objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_DOWN);
					} elseif ($diagonalUp == true && $diagonalDown == true) {
						$objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_BOTH);
					}

				// offset: 14; size: 4;
					// bit: 6-0; mask: 0x0000007F; top color
					$objStyle->getBorders()->getTop()->colorIndex = (0x0000007F & self::_GetInt4d($recordData, 14)) >> 0;

					// bit: 13-7; mask: 0x00003F80; bottom color
					$objStyle->getBorders()->getBottom()->colorIndex = (0x00003F80 & self::_GetInt4d($recordData, 14)) >> 7;

					// bit: 20-14; mask: 0x001FC000; diagonal color
					$objStyle->getBorders()->getDiagonal()->colorIndex = (0x001FC000 & self::_GetInt4d($recordData, 14)) >> 14;

					// bit: 24-21; mask: 0x01E00000; diagonal style
					if ($bordersDiagonalStyle = self::_mapBorderStyle((0x01E00000 & self::_GetInt4d($recordData, 14)) >> 21)) {
						$objStyle->getBorders()->getDiagonal()->setBorderStyle($bordersDiagonalStyle);
					}

					// bit: 31-26; mask: 0xFC000000 fill pattern
					if ($fillType = self::_mapFillPattern((0xFC000000 & self::_GetInt4d($recordData, 14)) >> 26)) {
						$objStyle->getFill()->setFillType($fillType);
					}
				// offset: 18; size: 2; pattern and background colour
					// bit: 6-0; mask: 0x007F; color index for pattern color
					$objStyle->getFill()->startcolorIndex = (0x007F & self::_GetInt2d($recordData, 18)) >> 0;

					// bit: 13-7; mask: 0x3F80; color index for pattern background
					$objStyle->getFill()->endcolorIndex = (0x3F80 & self::_GetInt2d($recordData, 18)) >> 7;
			} else {
		
Back to Top