/usr/share/perl5/Spreadsheet/ParseExcel.pm is in libspreadsheet-parseexcel-perl 0.5800-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 | package Spreadsheet::ParseExcel;
##############################################################################
#
# Spreadsheet::ParseExcel - Extract information from an Excel file.
#
# Copyright 2000-2008, Takanori Kawai
#
# perltidy with standard settings.
#
# Documentation after __END__
#
use strict;
use warnings;
use 5.008;
use OLE::Storage_Lite;
use IO::File;
use Config;
our $VERSION = '0.58';
use Spreadsheet::ParseExcel::Workbook;
use Spreadsheet::ParseExcel::Worksheet;
use Spreadsheet::ParseExcel::Font;
use Spreadsheet::ParseExcel::Format;
use Spreadsheet::ParseExcel::Cell;
use Spreadsheet::ParseExcel::FmtDefault;
my @aColor = (
'000000', # 0x00
'FFFFFF', 'FFFFFF', 'FFFFFF', 'FFFFFF',
'FFFFFF', 'FFFFFF', 'FFFFFF', 'FFFFFF', # 0x08
'FFFFFF', 'FF0000', '00FF00', '0000FF',
'FFFF00', 'FF00FF', '00FFFF', '800000', # 0x10
'008000', '000080', '808000', '800080',
'008080', 'C0C0C0', '808080', '9999FF', # 0x18
'993366', 'FFFFCC', 'CCFFFF', '660066',
'FF8080', '0066CC', 'CCCCFF', '000080', # 0x20
'FF00FF', 'FFFF00', '00FFFF', '800080',
'800000', '008080', '0000FF', '00CCFF', # 0x28
'CCFFFF', 'CCFFCC', 'FFFF99', '99CCFF',
'FF99CC', 'CC99FF', 'FFCC99', '3366FF', # 0x30
'33CCCC', '99CC00', 'FFCC00', 'FF9900',
'FF6600', '666699', '969696', '003366', # 0x38
'339966', '003300', '333300', '993300',
'993366', '333399', '333333', 'FFFFFF' # 0x40
);
use constant verExcel95 => 0x500;
use constant verExcel97 => 0x600;
use constant verBIFF2 => 0x00;
use constant verBIFF3 => 0x02;
use constant verBIFF4 => 0x04;
use constant verBIFF5 => 0x08;
use constant verBIFF8 => 0x18;
# Error code for some of the common parsing errors.
use constant ErrorNone => 0;
use constant ErrorNoFile => 1;
use constant ErrorNoExcelData => 2;
use constant ErrorFileEncrypted => 3;
our %error_strings = (
ErrorNone, '', # 0
ErrorNoFile, 'File not found', # 1
ErrorNoExcelData, 'No Excel data found in file', # 2
ErrorFileEncrypted, 'File is encrypted', # 3
);
our %ProcTbl = (
#Develpers' Kit P291
0x14 => \&_subHeader, # Header
0x15 => \&_subFooter, # Footer
0x18 => \&_subName, # NAME(?)
0x1A => \&_subVPageBreak, # Vertical Page Break
0x1B => \&_subHPageBreak, # Horizontal Page Break
0x22 => \&_subFlg1904, # 1904 Flag
0x26 => \&_subMargin, # Left Margin
0x27 => \&_subMargin, # Right Margin
0x28 => \&_subMargin, # Top Margin
0x29 => \&_subMargin, # Bottom Margin
0x2A => \&_subPrintHeaders, # Print Headers
0x2B => \&_subPrintGridlines, # Print Gridlines
0x3C => \&_subContinue, # Continue
0x43 => \&_subXF, # XF for Excel < 4.
0x0443 => \&_subXF, # XF for Excel = 4.
#Develpers' Kit P292
0x55 => \&_subDefColWidth, # Consider
0x5C => \&_subWriteAccess, # WRITEACCESS
0x7D => \&_subColInfo, # Colinfo
0x7E => \&_subRK, # RK
0x81 => \&_subWSBOOL, # WSBOOL
0x83 => \&_subHcenter, # HCENTER
0x84 => \&_subVcenter, # VCENTER
0x85 => \&_subBoundSheet, # BoundSheet
0x92 => \&_subPalette, # Palette, fgp
0x99 => \&_subStandardWidth, # Standard Col
#Develpers' Kit P293
0xA1 => \&_subSETUP, # SETUP
0xBD => \&_subMulRK, # MULRK
0xBE => \&_subMulBlank, # MULBLANK
0xD6 => \&_subRString, # RString
#Develpers' Kit P294
0xE0 => \&_subXF, # ExTended Format
0xE5 => \&_subMergeArea, # MergeArea (Not Documented)
0xFC => \&_subSST, # Shared String Table
0xFD => \&_subLabelSST, # Label SST
#Develpers' Kit P295
0x201 => \&_subBlank, # Blank
0x202 => \&_subInteger, # Integer(Not Documented)
0x203 => \&_subNumber, # Number
0x204 => \&_subLabel, # Label
0x205 => \&_subBoolErr, # BoolErr
0x207 => \&_subString, # STRING
0x208 => \&_subRow, # RowData
0x221 => \&_subArray, # Array (Consider)
0x225 => \&_subDefaultRowHeight, # Consider
0x31 => \&_subFont, # Font
0x231 => \&_subFont, # Font
0x27E => \&_subRK, # RK
0x41E => \&_subFormat, # Format
0x06 => \&_subFormula, # Formula
0x406 => \&_subFormula, # Formula
0x009 => \&_subBOF, # BOF(BIFF2)
0x209 => \&_subBOF, # BOF(BIFF3)
0x409 => \&_subBOF, # BOF(BIFF4)
0x809 => \&_subBOF, # BOF(BIFF5-8)
);
our $BIGENDIAN;
our $PREFUNC;
our $_CellHandler;
our $_NotSetCell;
our $_Object;
our $_use_perlio;
#------------------------------------------------------------------------------
# Spreadsheet::ParseExcel->new
#------------------------------------------------------------------------------
sub new {
my ( $class, %hParam ) = @_;
if ( not defined $_use_perlio ) {
if ( exists $Config{useperlio}
&& defined $Config{useperlio}
&& $Config{useperlio} eq "define" )
{
$_use_perlio = 1;
}
else {
$_use_perlio = 0;
require IO::Scalar;
import IO::Scalar;
}
}
# Check ENDIAN(Little: Interl etc. BIG: Sparc etc)
$BIGENDIAN =
( defined $hParam{Endian} ) ? $hParam{Endian}
: ( unpack( "H08", pack( "L", 2 ) ) eq '02000000' ) ? 0
: 1;
my $self = {};
bless $self, $class;
$self->{GetContent} = \&_subGetContent;
if ( $hParam{EventHandlers} ) {
$self->SetEventHandlers( $hParam{EventHandlers} );
}
else {
$self->SetEventHandlers( \%ProcTbl );
}
if ( $hParam{AddHandlers} ) {
foreach my $sKey ( keys( %{ $hParam{AddHandlers} } ) ) {
$self->SetEventHandler( $sKey, $hParam{AddHandlers}->{$sKey} );
}
}
$_CellHandler = $hParam{CellHandler} if ( $hParam{CellHandler} );
$_NotSetCell = $hParam{NotSetCell};
$_Object = $hParam{Object};
$self->{_error_status} = ErrorNone;
return $self;
}
#------------------------------------------------------------------------------
# Spreadsheet::ParseExcel->SetEventHandler
#------------------------------------------------------------------------------
sub SetEventHandler {
my ( $self, $key, $sub_ref ) = @_;
$self->{FuncTbl}->{$key} = $sub_ref;
}
#------------------------------------------------------------------------------
# Spreadsheet::ParseExcel->SetEventHandlers
#------------------------------------------------------------------------------
sub SetEventHandlers {
my ( $self, $rhTbl ) = @_;
$self->{FuncTbl} = undef;
foreach my $sKey ( keys %$rhTbl ) {
$self->{FuncTbl}->{$sKey} = $rhTbl->{$sKey};
}
}
###############################################################################
#
# Parse()
#
# Parse the Excel file and convert it into a tree of objects..
#
sub parse {
my ( $self, $source, $formatter ) = @_;
my $workbook = Spreadsheet::ParseExcel::Workbook->new();
$workbook->{SheetCount} = 0;
my ( $biff_data, $data_length ) = $self->_get_content( $source, $workbook );
return undef if not $biff_data;
if ($formatter) {
$workbook->{FmtClass} = $formatter;
}
else {
$workbook->{FmtClass} = Spreadsheet::ParseExcel::FmtDefault->new();
}
# Parse the BIFF data.
my $pos = 0;
my $record_header = substr( $biff_data, $pos, 4 );
$pos += 4;
while ( $pos <= $data_length ) {
my ( $record, $record_length ) = unpack( "v2", $record_header );
if ($record_length) {
$record_header = substr( $biff_data, $pos, $record_length );
$pos += $record_length;
}
# If the file contains a FILEPASS record we assume that it is encrypted
# and cannot be parsed.
if ( $record == 0x002F ) {
$self->{_error_status} = ErrorFileEncrypted;
return undef;
}
# Special case of a formula String with no string.
if ( $workbook->{_PrevPos}
&& ( defined $self->{FuncTbl}->{$record} )
&& ( $record != 0x207 ) )
{
my $iPos = $workbook->{_PrevPos};
$workbook->{_PrevPos} = undef;
my ( $row, $col, $format_index ) = @$iPos;
_NewCell(
$workbook, $row, $col,
Kind => 'Formula String',
Val => '',
FormatNo => $format_index,
Format => $workbook->{Format}[$format_index],
Numeric => 0,
Code => undef,
Book => $workbook,
);
}
# If the BIFF record matches 0x0*09 then it is a BOF record.
# We reset the _skip_chart flag to ensure we check the sheet type.
if ( ( $record & 0xF0FF ) == 0x09 ) {
$workbook->{_skip_chart} = 0;
}
if ( defined $self->{FuncTbl}->{$record} && !$workbook->{_skip_chart} ) {
$self->{FuncTbl}->{$record}->( $workbook, $record, $record_length, $record_header );
}
$PREFUNC = $record if ( $record != 0x3C ); #Not Continue
if ( ( $pos + 4 ) <= $data_length ) {
$record_header = substr( $biff_data, $pos, 4 );
}
$pos += 4;
return $workbook if defined $workbook->{_ParseAbort};
}
return $workbook;
}
###############################################################################
#
# _get_content()
#
# Get the Excel BIFF content from the file or filehandle.
#
sub _get_content {
my ( $self, $source, $workbook ) = @_;
my ( $biff_data, $data_length );
# Reset the error status in case method is called more than once.
$self->{_error_status} = ErrorNone;
my $ref = ref($source);
if ( ref($source) eq "SCALAR" ) {
# Specified by a scalar buffer.
( $biff_data, $data_length ) = $self->{GetContent}->($source);
}
elsif ( grep { $ref eq $_ } qw(IO::File IO::Handle IO::Wrap) ) {
# Specified by IO::File/Handle/Wrap object
$workbook->{File} = undef;
my $sBuff = join( '', $source->getlines );
my ( $sBIFF, $iLen ) = $self->{GetContent}->( \$sBuff );
return $sBIFF ? ( $sBIFF, $iLen ) : (undef);
}
elsif (( ref($source) =~ /GLOB/ ) || ( ref($source) eq 'Fh' ) ) {
# For CGI.pm (Light FileHandle)
binmode($source);
my $sWk;
my $sBuff = '';
while ( read( $source, $sWk, 4096 ) ) {
$sBuff .= $sWk;
}
( $biff_data, $data_length ) = $self->{GetContent}->( \$sBuff );
}
elsif ( ref($source) eq 'ARRAY' ) {
# Specified by file content
$workbook->{File} = undef;
my $sData = join( '', @$source );
( $biff_data, $data_length ) = $self->{GetContent}->( \$sData );
}
else {
# Specified by filename .
$workbook->{File} = $source;
if ( ! -e $source ) {
$self->{_error_status} = ErrorNoFile;
return undef;
}
( $biff_data, $data_length ) = $self->{GetContent}->($source);
}
# If the read was successful return the data.
if ($data_length) {
return ($biff_data, $data_length );
}
else {
$self->{_error_status} = ErrorNoExcelData;
return undef;
}
}
#------------------------------------------------------------------------------
# _subGetContent (for Spreadsheet::ParseExcel)
#------------------------------------------------------------------------------
sub _subGetContent {
my ($sFile) = @_;
my $oOl = OLE::Storage_Lite->new($sFile);
return ( undef, undef ) unless ($oOl);
my @aRes = $oOl->getPpsSearch(
[
OLE::Storage_Lite::Asc2Ucs('Book'),
OLE::Storage_Lite::Asc2Ucs('Workbook')
],
1, 1
);
return ( undef, undef ) if ( $#aRes < 0 );
#Hack from Herbert
if ( $aRes[0]->{Data} ) {
return ( $aRes[0]->{Data}, length( $aRes[0]->{Data} ) );
}
#Same as OLE::Storage_Lite
my $oIo;
#1. $sFile is Ref of scalar
if ( ref($sFile) eq 'SCALAR' ) {
if ($_use_perlio) {
open $oIo, "<", \$sFile;
}
else {
$oIo = IO::Scalar->new;
$oIo->open($sFile);
}
}
#2. $sFile is a IO::Handle object
elsif ( UNIVERSAL::isa( $sFile, 'IO::Handle' ) ) {
$oIo = $sFile;
binmode($oIo);
}
#3. $sFile is a simple filename string
elsif ( !ref($sFile) ) {
$oIo = IO::File->new;
$oIo->open("<$sFile") || return undef;
binmode($oIo);
}
my $sWk;
my $sBuff = '';
while ( $oIo->read( $sWk, 4096 ) ) { #4_096 has no special meanings
$sBuff .= $sWk;
}
$oIo->close();
#Not Excel file (simple method)
return ( undef, undef ) if ( substr( $sBuff, 0, 1 ) ne "\x09" );
return ( $sBuff, length($sBuff) );
}
#------------------------------------------------------------------------------
# _subBOF (for Spreadsheet::ParseExcel) Developers' Kit : P303
#------------------------------------------------------------------------------
sub _subBOF {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iVer, $iDt ) = unpack( "v2", $sWk );
#Workbook Global
if ( $iDt == 0x0005 ) {
$oBook->{Version} = unpack( "v", $sWk );
$oBook->{BIFFVersion} =
( $oBook->{Version} == verExcel95 ) ? verBIFF5 : verBIFF8;
$oBook->{_CurSheet} = undef;
$oBook->{_CurSheet_} = -1;
}
#Worksheet or Dialogsheet
elsif ( $iDt != 0x0020 ) { #if($iDt == 0x0010)
if ( defined $oBook->{_CurSheet_} ) {
$oBook->{_CurSheet} = $oBook->{_CurSheet_} + 1;
$oBook->{_CurSheet_}++;
(
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{SheetVersion},
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{SheetType},
)
= unpack( "v2", $sWk )
if ( length($sWk) > 4 );
}
else {
$oBook->{BIFFVersion} = int( $bOp / 0x100 );
if ( ( $oBook->{BIFFVersion} == verBIFF2 )
|| ( $oBook->{BIFFVersion} == verBIFF3 )
|| ( $oBook->{BIFFVersion} == verBIFF4 ) )
{
$oBook->{Version} = $oBook->{BIFFVersion};
$oBook->{_CurSheet} = 0;
$oBook->{Worksheet}[ $oBook->{SheetCount} ] =
Spreadsheet::ParseExcel::Worksheet->new(
_Name => '',
Name => '',
_Book => $oBook,
_SheetNo => $oBook->{SheetCount},
);
$oBook->{SheetCount}++;
}
}
}
else {
# Set flag to ignore all chart records until we reach another BOF.
$oBook->{_skip_chart} = 1;
}
}
#------------------------------------------------------------------------------
# _subBlank (for Spreadsheet::ParseExcel) DK:P303
#------------------------------------------------------------------------------
sub _subBlank {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iR, $iC, $iF ) = unpack( "v3", $sWk );
_NewCell(
$oBook, $iR, $iC,
Kind => 'BLANK',
Val => '',
FormatNo => $iF,
Format => $oBook->{Format}[$iF],
Numeric => 0,
Code => undef,
Book => $oBook,
);
#2.MaxRow, MaxCol, MinRow, MinCol
_SetDimension( $oBook, $iR, $iC, $iC );
}
#------------------------------------------------------------------------------
# _subInteger (for Spreadsheet::ParseExcel) Not in DK
#------------------------------------------------------------------------------
sub _subInteger {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iR, $iC, $iF, $sTxt, $sDum );
( $iR, $iC, $iF, $sDum, $sTxt ) = unpack( "v3cv", $sWk );
_NewCell(
$oBook, $iR, $iC,
Kind => 'INTEGER',
Val => $sTxt,
FormatNo => $iF,
Format => $oBook->{Format}[$iF],
Numeric => 0,
Code => undef,
Book => $oBook,
);
#2.MaxRow, MaxCol, MinRow, MinCol
_SetDimension( $oBook, $iR, $iC, $iC );
}
#------------------------------------------------------------------------------
# _subNumber (for Spreadsheet::ParseExcel) : DK: P354
#------------------------------------------------------------------------------
sub _subNumber {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iR, $iC, $iF ) = unpack( "v3", $sWk );
my $dVal = _convDval( substr( $sWk, 6, 8 ) );
_NewCell(
$oBook, $iR, $iC,
Kind => 'Number',
Val => $dVal,
FormatNo => $iF,
Format => $oBook->{Format}[$iF],
Numeric => 1,
Code => undef,
Book => $oBook,
);
#2.MaxRow, MaxCol, MinRow, MinCol
_SetDimension( $oBook, $iR, $iC, $iC );
}
#------------------------------------------------------------------------------
# _convDval (for Spreadsheet::ParseExcel)
#------------------------------------------------------------------------------
sub _convDval {
my ($sWk) = @_;
return
unpack( "d",
($BIGENDIAN) ? pack( "c8", reverse( unpack( "c8", $sWk ) ) ) : $sWk );
}
#------------------------------------------------------------------------------
# _subRString (for Spreadsheet::ParseExcel) DK:P405
#------------------------------------------------------------------------------
sub _subRString {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iR, $iC, $iF, $iL, $sTxt );
( $iR, $iC, $iF, $iL ) = unpack( "v4", $sWk );
my $bver = $oBook->{BIFFVersion};
my ( $rich, $sCode );
if ( $bver == verBIFF8 ) {
my ( $raBuff, @rest ) =
_convBIFF8String( $oBook, substr( $sWk, 6 ), 1 );
$sTxt = $raBuff->[0];
$sCode = ( $raBuff->[1] ) ? 'ucs2' : undef;
$rich = $raBuff->[2];
}
else {
$sTxt = substr( $sWk, 8, $iL );
$sCode = '_native_';
$rich = substr( $sWk, ( 8 + $iL ) + 1 )
if ( length($sWk) > ( 8 + $iL ) );
}
if ($rich) {
_NewCell(
$oBook, $iR, $iC,
Kind => 'RString',
Val => $sTxt,
FormatNo => $iF,
Format => $oBook->{Format}[$iF],
Numeric => 0,
Code => $sCode,
Book => $oBook,
Rich => $rich,
);
}
else {
_NewCell(
$oBook, $iR, $iC,
Kind => 'RString',
Val => $sTxt,
FormatNo => $iF,
Format => $oBook->{Format}[$iF],
Numeric => 0,
Code => $sCode,
Book => $oBook,
);
}
#2.MaxRow, MaxCol, MinRow, MinCol
_SetDimension( $oBook, $iR, $iC, $iC );
}
#------------------------------------------------------------------------------
# _subBoolErr (for Spreadsheet::ParseExcel) DK:P306
#------------------------------------------------------------------------------
sub _subBoolErr {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iR, $iC, $iF ) = unpack( "v3", $sWk );
my ( $iVal, $iFlg ) = unpack( "cc", substr( $sWk, 6, 2 ) );
my $sTxt = DecodeBoolErr( $iVal, $iFlg );
_NewCell(
$oBook, $iR, $iC,
Kind => 'BoolError',
Val => $sTxt,
FormatNo => $iF,
Format => $oBook->{Format}[$iF],
Numeric => 0,
Code => undef,
Book => $oBook,
);
#2.MaxRow, MaxCol, MinRow, MinCol
_SetDimension( $oBook, $iR, $iC, $iC );
}
###############################################################################
#
# _subRK()
#
# Decode the RK BIFF record.
#
sub _subRK {
my ( $workbook, $biff_number, $length, $data ) = @_;
my ( $row, $col, $format_index, $rk_number ) = unpack( 'vvvV', $data );
my $number = _decode_rk_number($rk_number);
_NewCell(
$workbook, $row, $col,
Kind => 'RK',
Val => $number,
FormatNo => $format_index,
Format => $workbook->{Format}->[$format_index],
Numeric => 1,
Code => undef,
Book => $workbook,
);
# Store the max and min row/col values.
_SetDimension( $workbook, $row, $col, $col );
}
#------------------------------------------------------------------------------
# _subArray (for Spreadsheet::ParseExcel) DK:P297
#------------------------------------------------------------------------------
sub _subArray {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iBR, $iER, $iBC, $iEC ) = unpack( "v2c2", $sWk );
}
#------------------------------------------------------------------------------
# _subFormula (for Spreadsheet::ParseExcel) DK:P336
#------------------------------------------------------------------------------
sub _subFormula {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iR, $iC, $iF ) = unpack( "v3", $sWk );
my ($iFlg) = unpack( "v", substr( $sWk, 12, 2 ) );
if ( $iFlg == 0xFFFF ) {
my ($iKind) = unpack( "c", substr( $sWk, 6, 1 ) );
my ($iVal) = unpack( "c", substr( $sWk, 8, 1 ) );
if ( ( $iKind == 1 ) or ( $iKind == 2 ) ) {
my $sTxt =
( $iKind == 1 )
? DecodeBoolErr( $iVal, 0 )
: DecodeBoolErr( $iVal, 1 );
_NewCell(
$oBook, $iR, $iC,
Kind => 'Formula Bool',
Val => $sTxt,
FormatNo => $iF,
Format => $oBook->{Format}[$iF],
Numeric => 0,
Code => undef,
Book => $oBook,
);
}
else { # Result (Reserve Only)
$oBook->{_PrevPos} = [ $iR, $iC, $iF ];
}
}
else {
my $dVal = _convDval( substr( $sWk, 6, 8 ) );
_NewCell(
$oBook, $iR, $iC,
Kind => 'Formula Number',
Val => $dVal,
FormatNo => $iF,
Format => $oBook->{Format}[$iF],
Numeric => 1,
Code => undef,
Book => $oBook,
);
}
#2.MaxRow, MaxCol, MinRow, MinCol
_SetDimension( $oBook, $iR, $iC, $iC );
}
#------------------------------------------------------------------------------
# _subString (for Spreadsheet::ParseExcel) DK:P414
#------------------------------------------------------------------------------
sub _subString {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
#Position (not enough for ARRAY)
my $iPos = $oBook->{_PrevPos};
return undef unless ($iPos);
$oBook->{_PrevPos} = undef;
my ( $iR, $iC, $iF ) = @$iPos;
my ( $iLen, $sTxt, $sCode );
if ( $oBook->{BIFFVersion} == verBIFF8 ) {
my ( $raBuff, $iLen ) = _convBIFF8String( $oBook, $sWk, 1 );
$sTxt = $raBuff->[0];
$sCode = ( $raBuff->[1] ) ? 'ucs2' : undef;
}
elsif ( $oBook->{BIFFVersion} == verBIFF5 ) {
$sCode = '_native_';
$iLen = unpack( "v", $sWk );
$sTxt = substr( $sWk, 2, $iLen );
}
else {
$sCode = '_native_';
$iLen = unpack( "c", $sWk );
$sTxt = substr( $sWk, 1, $iLen );
}
_NewCell(
$oBook, $iR, $iC,
Kind => 'String',
Val => $sTxt,
FormatNo => $iF,
Format => $oBook->{Format}[$iF],
Numeric => 0,
Code => $sCode,
Book => $oBook,
);
#2.MaxRow, MaxCol, MinRow, MinCol
_SetDimension( $oBook, $iR, $iC, $iC );
}
#------------------------------------------------------------------------------
# _subLabel (for Spreadsheet::ParseExcel) DK:P344
#------------------------------------------------------------------------------
sub _subLabel {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iR, $iC, $iF ) = unpack( "v3", $sWk );
my ( $sLbl, $sCode );
#BIFF8
if ( $oBook->{BIFFVersion} >= verBIFF8 ) {
my ( $raBuff, $iLen, $iStPos, $iLenS ) =
_convBIFF8String( $oBook, substr( $sWk, 6 ), 1 );
$sLbl = $raBuff->[0];
$sCode = ( $raBuff->[1] ) ? 'ucs2' : undef;
}
#Before BIFF8
else {
$sLbl = substr( $sWk, 8 );
$sCode = '_native_';
}
_NewCell(
$oBook, $iR, $iC,
Kind => 'Label',
Val => $sLbl,
FormatNo => $iF,
Format => $oBook->{Format}[$iF],
Numeric => 0,
Code => $sCode,
Book => $oBook,
);
#2.MaxRow, MaxCol, MinRow, MinCol
_SetDimension( $oBook, $iR, $iC, $iC );
}
###############################################################################
#
# _subMulRK()
#
# Decode the Multiple RK BIFF record.
#
sub _subMulRK {
my ( $workbook, $biff_number, $length, $data ) = @_;
# JMN: I don't know why this is here.
return if $workbook->{SheetCount} <= 0;
my ( $row, $first_col ) = unpack( "v2", $data );
my $last_col = unpack( "v", substr( $data, length($data) - 2, 2 ) );
# Iterate over the RK array and decode the data.
my $pos = 4;
for my $col ( $first_col .. $last_col ) {
my $data = substr( $data, $pos, 6 );
my ( $format_index, $rk_number ) = unpack 'vV', $data;
my $number = _decode_rk_number($rk_number);
_NewCell(
$workbook, $row, $col,
Kind => 'MulRK',
Val => $number,
FormatNo => $format_index,
Format => $workbook->{Format}->[$format_index],
Numeric => 1,
Code => undef,
Book => $workbook,
);
$pos += 6;
}
# Store the max and min row/col values.
_SetDimension( $workbook, $row, $first_col, $last_col );
}
#------------------------------------------------------------------------------
# _subMulBlank (for Spreadsheet::ParseExcel) DK:P349
#------------------------------------------------------------------------------
sub _subMulBlank {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iR, $iSc ) = unpack( "v2", $sWk );
my $iEc = unpack( "v", substr( $sWk, length($sWk) - 2, 2 ) );
my $iPos = 4;
for ( my $iC = $iSc ; $iC <= $iEc ; $iC++ ) {
my $iF = unpack( 'v', substr( $sWk, $iPos, 2 ) );
_NewCell(
$oBook, $iR, $iC,
Kind => 'MulBlank',
Val => '',
FormatNo => $iF,
Format => $oBook->{Format}[$iF],
Numeric => 0,
Code => undef,
Book => $oBook,
);
$iPos += 2;
}
#2.MaxRow, MaxCol, MinRow, MinCol
_SetDimension( $oBook, $iR, $iSc, $iEc );
}
#------------------------------------------------------------------------------
# _subLabelSST (for Spreadsheet::ParseExcel) DK: P345
#------------------------------------------------------------------------------
sub _subLabelSST {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iR, $iC, $iF, $iIdx ) = unpack( 'v3V', $sWk );
_NewCell(
$oBook, $iR, $iC,
Kind => 'PackedIdx',
Val => $oBook->{PkgStr}[$iIdx]->{Text},
FormatNo => $iF,
Format => $oBook->{Format}[$iF],
Numeric => 0,
Code => ( $oBook->{PkgStr}[$iIdx]->{Unicode} ) ? 'ucs2' : undef,
Book => $oBook,
Rich => $oBook->{PkgStr}[$iIdx]->{Rich},
);
#2.MaxRow, MaxCol, MinRow, MinCol
_SetDimension( $oBook, $iR, $iC, $iC );
}
#------------------------------------------------------------------------------
# _subFlg1904 (for Spreadsheet::ParseExcel) DK:P296
#------------------------------------------------------------------------------
sub _subFlg1904 {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
$oBook->{Flg1904} = unpack( "v", $sWk );
}
#------------------------------------------------------------------------------
# _subRow (for Spreadsheet::ParseExcel) DK:P403
#------------------------------------------------------------------------------
sub _subRow {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
#0. Get Worksheet info (MaxRow, MaxCol, MinRow, MinCol)
my ( $iR, $iSc, $iEc, $iHght, $undef1, $undef2, $iGr, $iXf ) =
unpack( "v8", $sWk );
$iEc--;
# TODO. we need to handle hidden rows:
# $iGr & 0x20
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{RowHeight}[$iR] = $iHght / 20;
#2.MaxRow, MaxCol, MinRow, MinCol
_SetDimension( $oBook, $iR, $iSc, $iEc );
}
#------------------------------------------------------------------------------
# _SetDimension (for Spreadsheet::ParseExcel)
#------------------------------------------------------------------------------
sub _SetDimension {
my ( $oBook, $iR, $iSc, $iEc ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
#2.MaxRow, MaxCol, MinRow, MinCol
#2.1 MinRow
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{MinRow} = $iR
unless ( defined $oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{MinRow} )
and ( $oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{MinRow} <= $iR );
#2.2 MaxRow
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{MaxRow} = $iR
unless ( defined $oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{MaxRow} )
and ( $oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{MaxRow} > $iR );
#2.3 MinCol
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{MinCol} = $iSc
unless ( defined $oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{MinCol} )
and ( $oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{MinCol} <= $iSc );
#2.4 MaxCol
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{MaxCol} = $iEc
unless ( defined $oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{MaxCol} )
and ( $oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{MaxCol} > $iEc );
}
#------------------------------------------------------------------------------
# _subDefaultRowHeight (for Spreadsheet::ParseExcel) DK: P318
#------------------------------------------------------------------------------
sub _subDefaultRowHeight {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
#1. RowHeight
my ( $iDum, $iHght ) = unpack( "v2", $sWk );
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{DefRowHeight} = $iHght / 20;
}
#------------------------------------------------------------------------------
# _subStandardWidth(for Spreadsheet::ParseExcel) DK:P413
#------------------------------------------------------------------------------
sub _subStandardWidth {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my $iW = unpack( "v", $sWk );
$oBook->{StandardWidth} = _convert_col_width( $oBook, $iW );
}
###############################################################################
#
# _subDefColWidth()
#
# Read the DEFCOLWIDTH Biff record. This gives the width in terms of chars
# and is different from the width in the COLINFO record.
#
sub _subDefColWidth {
my ( $self, $record, $length, $data ) = @_;
my $width = unpack 'v', $data;
# Adjustment for default Arial 10 width.
$width = 8.43 if $width == 8;
$self->{Worksheet}->[ $self->{_CurSheet} ]->{DefColWidth} = $width;
}
###############################################################################
#
# _convert_col_width()
#
# Converts from the internal Excel column width units to user units seen in the
# interface. It is first necessary to convert the internal width to pixels and
# then to user units. The conversion is specific to a default font of Arial 10.
# TODO, the conversion should be extended to other fonts and sizes.
#
sub _convert_col_width {
my $self = shift;
my $excel_width = shift;
# Convert from Excel units to pixels (rounded up).
my $pixels = int( 0.5 + $excel_width * 7 / 256 );
# Convert from pixels to user units.
# The conversion is different for columns <= 1 user unit (12 pixels).
my $user_width;
if ( $pixels <= 12 ) {
$user_width = $pixels / 12;
}
else {
$user_width = ( $pixels - 5 ) / 7;
}
# Round up to 2 decimal places.
$user_width = int( $user_width * 100 + 0.5 ) / 100;
return $user_width;
}
#------------------------------------------------------------------------------
# _subColInfo (for Spreadsheet::ParseExcel) DK:P309
#------------------------------------------------------------------------------
sub _subColInfo {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless defined $oBook->{_CurSheet};
my ( $iSc, $iEc, $iW, $iXF, $iGr ) = unpack( "v5", $sWk );
for ( my $i = $iSc ; $i <= $iEc ; $i++ ) {
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{ColWidth}[$i] =
_convert_col_width( $oBook, $iW );
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{ColFmtNo}[$i] = $iXF;
# TODO. we need to handle hidden cols: $iGr & 0x01.
}
}
#------------------------------------------------------------------------------
# _subSST (for Spreadsheet::ParseExcel) DK:P413
#------------------------------------------------------------------------------
sub _subSST {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
_subStrWk( $oBook, substr( $sWk, 8 ) );
}
#------------------------------------------------------------------------------
# _subContinue (for Spreadsheet::ParseExcel) DK:P311
#------------------------------------------------------------------------------
sub _subContinue {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
#if(defined $self->{FuncTbl}->{$bOp}) {
# $self->{FuncTbl}->{$PREFUNC}->($oBook, $bOp, $bLen, $sWk);
#}
_subStrWk( $oBook, $sWk, 1 ) if ( $PREFUNC == 0xFC );
}
#------------------------------------------------------------------------------
# _subWriteAccess (for Spreadsheet::ParseExcel) DK:P451
#------------------------------------------------------------------------------
sub _subWriteAccess {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return if ( defined $oBook->{_Author} );
#BIFF8
if ( $oBook->{BIFFVersion} >= verBIFF8 ) {
$oBook->{Author} = _convBIFF8String( $oBook, $sWk );
}
#Before BIFF8
else {
my ($iLen) = unpack( "c", $sWk );
$oBook->{Author} =
$oBook->{FmtClass}->TextFmt( substr( $sWk, 1, $iLen ), '_native_' );
}
}
#------------------------------------------------------------------------------
# _convBIFF8String (for Spreadsheet::ParseExcel)
#------------------------------------------------------------------------------
sub _convBIFF8String {
my ( $oBook, $sWk, $iCnvFlg ) = @_;
my ( $iLen, $iFlg ) = unpack( "vc", $sWk );
my ( $iHigh, $iExt, $iRich ) = ( $iFlg & 0x01, $iFlg & 0x04, $iFlg & 0x08 );
my ( $iStPos, $iExtCnt, $iRichCnt, $sStr );
#2. Rich and Ext
if ( $iRich && $iExt ) {
$iStPos = 9;
( $iRichCnt, $iExtCnt ) = unpack( 'vV', substr( $sWk, 3, 6 ) );
}
elsif ($iRich) { #Only Rich
$iStPos = 5;
$iRichCnt = unpack( 'v', substr( $sWk, 3, 2 ) );
$iExtCnt = 0;
}
elsif ($iExt) { #Only Ext
$iStPos = 7;
$iRichCnt = 0;
$iExtCnt = unpack( 'V', substr( $sWk, 3, 4 ) );
}
else { #Nothing Special
$iStPos = 3;
$iExtCnt = 0;
$iRichCnt = 0;
}
#3.Get String
if ($iHigh) { #Compressed
$iLen *= 2;
$sStr = substr( $sWk, $iStPos, $iLen );
_SwapForUnicode( \$sStr );
$sStr = $oBook->{FmtClass}->TextFmt( $sStr, 'ucs2' ) unless ($iCnvFlg);
}
else { #Not Compressed
$sStr = substr( $sWk, $iStPos, $iLen );
$sStr = $oBook->{FmtClass}->TextFmt( $sStr, undef ) unless ($iCnvFlg);
}
#4. return
if (wantarray) {
#4.1 Get Rich and Ext
if ( length($sWk) < $iStPos + $iLen + $iRichCnt * 4 + $iExtCnt ) {
return (
[ undef, $iHigh, undef, undef ],
$iStPos + $iLen + $iRichCnt * 4 + $iExtCnt,
$iStPos, $iLen
);
}
else {
return (
[
$sStr,
$iHigh,
substr( $sWk, $iStPos + $iLen, $iRichCnt * 4 ),
substr( $sWk, $iStPos + $iLen + $iRichCnt * 4, $iExtCnt )
],
$iStPos + $iLen + $iRichCnt * 4 + $iExtCnt,
$iStPos, $iLen
);
}
}
else {
return $sStr;
}
}
#------------------------------------------------------------------------------
# _subXF (for Spreadsheet::ParseExcel) DK:P453
#------------------------------------------------------------------------------
sub _subXF {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iFnt, $iIdx );
my (
$iLock, $iHidden, $iStyle, $i123, $iAlH, $iWrap,
$iAlV, $iJustL, $iRotate, $iInd, $iShrink, $iMerge,
$iReadDir, $iBdrD, $iBdrSL, $iBdrSR, $iBdrST, $iBdrSB,
$iBdrSD, $iBdrCL, $iBdrCR, $iBdrCT, $iBdrCB, $iBdrCD,
$iFillP, $iFillCF, $iFillCB
);
if ( $oBook->{BIFFVersion} == verBIFF4 ) {
# Minimal support for Excel 4. We just get the font and format indices
# so that the cell data value can be formatted.
( $iFnt, $iIdx,) = unpack( "CC", $sWk );
}
elsif ( $oBook->{BIFFVersion} == verBIFF8 ) {
my ( $iGen, $iAlign, $iGen2, $iBdr1, $iBdr2, $iBdr3, $iPtn );
( $iFnt, $iIdx, $iGen, $iAlign, $iGen2, $iBdr1, $iBdr2, $iBdr3, $iPtn )
= unpack( "v7Vv", $sWk );
$iLock = ( $iGen & 0x01 ) ? 1 : 0;
$iHidden = ( $iGen & 0x02 ) ? 1 : 0;
$iStyle = ( $iGen & 0x04 ) ? 1 : 0;
$i123 = ( $iGen & 0x08 ) ? 1 : 0;
$iAlH = ( $iAlign & 0x07 );
$iWrap = ( $iAlign & 0x08 ) ? 1 : 0;
$iAlV = ( $iAlign & 0x70 ) / 0x10;
$iJustL = ( $iAlign & 0x80 ) ? 1 : 0;
$iRotate = ( ( $iAlign & 0xFF00 ) / 0x100 ) & 0x00FF;
$iRotate = 90 if ( $iRotate == 255 );
$iRotate = 90 - $iRotate if ( $iRotate > 90 );
$iInd = ( $iGen2 & 0x0F );
$iShrink = ( $iGen2 & 0x10 ) ? 1 : 0;
$iMerge = ( $iGen2 & 0x20 ) ? 1 : 0;
$iReadDir = ( ( $iGen2 & 0xC0 ) / 0x40 ) & 0x03;
$iBdrSL = $iBdr1 & 0x0F;
$iBdrSR = ( ( $iBdr1 & 0xF0 ) / 0x10 ) & 0x0F;
$iBdrST = ( ( $iBdr1 & 0xF00 ) / 0x100 ) & 0x0F;
$iBdrSB = ( ( $iBdr1 & 0xF000 ) / 0x1000 ) & 0x0F;
$iBdrCL = ( ( $iBdr2 & 0x7F ) ) & 0x7F;
$iBdrCR = ( ( $iBdr2 & 0x3F80 ) / 0x80 ) & 0x7F;
$iBdrD = ( ( $iBdr2 & 0xC000 ) / 0x4000 ) & 0x3;
$iBdrCT = ( ( $iBdr3 & 0x7F ) ) & 0x7F;
$iBdrCB = ( ( $iBdr3 & 0x3F80 ) / 0x80 ) & 0x7F;
$iBdrCD = ( ( $iBdr3 & 0x1FC000 ) / 0x4000 ) & 0x7F;
$iBdrSD = ( ( $iBdr3 & 0x1E00000 ) / 0x200000 ) & 0xF;
$iFillP = ( ( $iBdr3 & 0xFC000000 ) / 0x4000000 ) & 0x3F;
$iFillCF = ( $iPtn & 0x7F );
$iFillCB = ( ( $iPtn & 0x3F80 ) / 0x80 ) & 0x7F;
}
else {
my ( $iGen, $iAlign, $iPtn, $iPtn2, $iBdr1, $iBdr2 );
( $iFnt, $iIdx, $iGen, $iAlign, $iPtn, $iPtn2, $iBdr1, $iBdr2 ) =
unpack( "v8", $sWk );
$iLock = ( $iGen & 0x01 ) ? 1 : 0;
$iHidden = ( $iGen & 0x02 ) ? 1 : 0;
$iStyle = ( $iGen & 0x04 ) ? 1 : 0;
$i123 = ( $iGen & 0x08 ) ? 1 : 0;
$iAlH = ( $iAlign & 0x07 );
$iWrap = ( $iAlign & 0x08 ) ? 1 : 0;
$iAlV = ( $iAlign & 0x70 ) / 0x10;
$iJustL = ( $iAlign & 0x80 ) ? 1 : 0;
$iRotate = ( ( $iAlign & 0x300 ) / 0x100 ) & 0x3;
$iFillCF = ( $iPtn & 0x7F );
$iFillCB = ( ( $iPtn & 0x1F80 ) / 0x80 ) & 0x7F;
$iFillP = ( $iPtn2 & 0x3F );
$iBdrSB = ( ( $iPtn2 & 0x1C0 ) / 0x40 ) & 0x7;
$iBdrCB = ( ( $iPtn2 & 0xFE00 ) / 0x200 ) & 0x7F;
$iBdrST = ( $iBdr1 & 0x07 );
$iBdrSL = ( ( $iBdr1 & 0x38 ) / 0x8 ) & 0x07;
$iBdrSR = ( ( $iBdr1 & 0x1C0 ) / 0x40 ) & 0x07;
$iBdrCT = ( ( $iBdr1 & 0xFE00 ) / 0x200 ) & 0x7F;
$iBdrCL = ( $iBdr2 & 0x7F ) & 0x7F;
$iBdrCR = ( ( $iBdr2 & 0x3F80 ) / 0x80 ) & 0x7F;
}
push @{ $oBook->{Format} }, Spreadsheet::ParseExcel::Format->new(
FontNo => $iFnt,
Font => $oBook->{Font}[$iFnt],
FmtIdx => $iIdx,
Lock => $iLock,
Hidden => $iHidden,
Style => $iStyle,
Key123 => $i123,
AlignH => $iAlH,
Wrap => $iWrap,
AlignV => $iAlV,
JustLast => $iJustL,
Rotate => $iRotate,
Indent => $iInd,
Shrink => $iShrink,
Merge => $iMerge,
ReadDir => $iReadDir,
BdrStyle => [ $iBdrSL, $iBdrSR, $iBdrST, $iBdrSB ],
BdrColor => [ $iBdrCL, $iBdrCR, $iBdrCT, $iBdrCB ],
BdrDiag => [ $iBdrD, $iBdrSD, $iBdrCD ],
Fill => [ $iFillP, $iFillCF, $iFillCB ],
);
}
#------------------------------------------------------------------------------
# _subFormat (for Spreadsheet::ParseExcel) DK: P336
#------------------------------------------------------------------------------
sub _subFormat {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my $sFmt;
if ( $oBook->{BIFFVersion} <= verBIFF5 ) {
$sFmt = substr( $sWk, 3, unpack( 'c', substr( $sWk, 2, 1 ) ) );
$sFmt = $oBook->{FmtClass}->TextFmt( $sFmt, '_native_' );
}
else {
$sFmt = _convBIFF8String( $oBook, substr( $sWk, 2 ) );
}
my $format_index = unpack( 'v', substr( $sWk, 0, 2 ) );
# Excel 4 and earlier used an index of 0 to indicate that a built-in format
# that was stored implicitly.
if ( $oBook->{BIFFVersion} <= verBIFF4 && $format_index == 0 ) {
$format_index = keys %{ $oBook->{FormatStr} };
}
$oBook->{FormatStr}->{$format_index} = $sFmt;
}
#------------------------------------------------------------------------------
# _subPalette (for Spreadsheet::ParseExcel) DK: P393
#------------------------------------------------------------------------------
sub _subPalette {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
for ( my $i = 0 ; $i < unpack( 'v', $sWk ) ; $i++ ) {
# push @aColor, unpack('H6', substr($sWk, $i*4+2));
$aColor[ $i + 8 ] = unpack( 'H6', substr( $sWk, $i * 4 + 2 ) );
}
}
#------------------------------------------------------------------------------
# _subFont (for Spreadsheet::ParseExcel) DK:P333
#------------------------------------------------------------------------------
sub _subFont {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iHeight, $iAttr, $iCIdx, $iBold, $iSuper, $iUnderline, $sFntName );
my ( $bBold, $bItalic, $bUnderline, $bStrikeout );
if ( $oBook->{BIFFVersion} == verBIFF8 ) {
( $iHeight, $iAttr, $iCIdx, $iBold, $iSuper, $iUnderline ) =
unpack( "v5c", $sWk );
my ( $iSize, $iHigh ) = unpack( 'cc', substr( $sWk, 14, 2 ) );
if ($iHigh) {
$sFntName = substr( $sWk, 16, $iSize * 2 );
_SwapForUnicode( \$sFntName );
$sFntName = $oBook->{FmtClass}->TextFmt( $sFntName, 'ucs2' );
}
else {
$sFntName = substr( $sWk, 16, $iSize );
$sFntName = $oBook->{FmtClass}->TextFmt( $sFntName, '_native_' );
}
$bBold = ( $iBold >= 0x2BC ) ? 1 : 0;
$bItalic = ( $iAttr & 0x02 ) ? 1 : 0;
$bStrikeout = ( $iAttr & 0x08 ) ? 1 : 0;
$bUnderline = ($iUnderline) ? 1 : 0;
}
elsif ( $oBook->{BIFFVersion} == verBIFF5 ) {
( $iHeight, $iAttr, $iCIdx, $iBold, $iSuper, $iUnderline ) =
unpack( "v5c", $sWk );
$sFntName =
$oBook->{FmtClass}
->TextFmt( substr( $sWk, 15, unpack( "c", substr( $sWk, 14, 1 ) ) ),
'_native_' );
$bBold = ( $iBold >= 0x2BC ) ? 1 : 0;
$bItalic = ( $iAttr & 0x02 ) ? 1 : 0;
$bStrikeout = ( $iAttr & 0x08 ) ? 1 : 0;
$bUnderline = ($iUnderline) ? 1 : 0;
}
else {
( $iHeight, $iAttr ) = unpack( "v2", $sWk );
$iCIdx = undef;
$iSuper = 0;
$bBold = ( $iAttr & 0x01 ) ? 1 : 0;
$bItalic = ( $iAttr & 0x02 ) ? 1 : 0;
$bUnderline = ( $iAttr & 0x04 ) ? 1 : 0;
$bStrikeout = ( $iAttr & 0x08 ) ? 1 : 0;
$sFntName = substr( $sWk, 5, unpack( "c", substr( $sWk, 4, 1 ) ) );
}
push @{ $oBook->{Font} }, Spreadsheet::ParseExcel::Font->new(
Height => $iHeight / 20.0,
Attr => $iAttr,
Color => $iCIdx,
Super => $iSuper,
UnderlineStyle => $iUnderline,
Name => $sFntName,
Bold => $bBold,
Italic => $bItalic,
Underline => $bUnderline,
Strikeout => $bStrikeout,
);
#Skip Font[4]
push @{ $oBook->{Font} }, {} if ( scalar( @{ $oBook->{Font} } ) == 4 );
}
#------------------------------------------------------------------------------
# _subBoundSheet (for Spreadsheet::ParseExcel): DK: P307
#------------------------------------------------------------------------------
sub _subBoundSheet {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my ( $iPos, $iGr, $iKind ) = unpack( "Lc2", $sWk );
$iKind &= 0x0F;
return if ( ( $iKind != 0x00 ) && ( $iKind != 0x01 ) );
if ( $oBook->{BIFFVersion} >= verBIFF8 ) {
my ( $iSize, $iUni ) = unpack( "cc", substr( $sWk, 6, 2 ) );
my $sWsName = substr( $sWk, 8 );
if ( $iUni & 0x01 ) {
_SwapForUnicode( \$sWsName );
$sWsName = $oBook->{FmtClass}->TextFmt( $sWsName, 'ucs2' );
}
$oBook->{Worksheet}[ $oBook->{SheetCount} ] =
Spreadsheet::ParseExcel::Worksheet->new(
Name => $sWsName,
Kind => $iKind,
_Pos => $iPos,
_Book => $oBook,
_SheetNo => $oBook->{SheetCount},
);
}
else {
$oBook->{Worksheet}[ $oBook->{SheetCount} ] =
Spreadsheet::ParseExcel::Worksheet->new(
Name =>
$oBook->{FmtClass}->TextFmt( substr( $sWk, 7 ), '_native_' ),
Kind => $iKind,
_Pos => $iPos,
_Book => $oBook,
_SheetNo => $oBook->{SheetCount},
);
}
$oBook->{SheetCount}++;
}
#------------------------------------------------------------------------------
# _subHeader (for Spreadsheet::ParseExcel) DK: P340
#------------------------------------------------------------------------------
sub _subHeader {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
my $sW;
#BIFF8
if ( $oBook->{BIFFVersion} >= verBIFF8 ) {
$sW = _convBIFF8String( $oBook, $sWk );
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{Header} =
( $sW eq "\x00" ) ? undef : $sW;
}
#Before BIFF8
else {
my ($iLen) = unpack( "c", $sWk );
$sW =
$oBook->{FmtClass}->TextFmt( substr( $sWk, 1, $iLen ), '_native_' );
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{Header} =
( $sW eq "\x00\x00\x00" ) ? undef : $sW;
}
}
#------------------------------------------------------------------------------
# _subFooter (for Spreadsheet::ParseExcel) DK: P335
#------------------------------------------------------------------------------
sub _subFooter {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
my $sW;
#BIFF8
if ( $oBook->{BIFFVersion} >= verBIFF8 ) {
$sW = _convBIFF8String( $oBook, $sWk );
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{Footer} =
( $sW eq "\x00" ) ? undef : $sW;
}
#Before BIFF8
else {
my ($iLen) = unpack( "c", $sWk );
$sW =
$oBook->{FmtClass}->TextFmt( substr( $sWk, 1, $iLen ), '_native_' );
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{Footer} =
( $sW eq "\x00\x00\x00" ) ? undef : $sW;
}
}
#------------------------------------------------------------------------------
# _subHPageBreak (for Spreadsheet::ParseExcel) DK: P341
#------------------------------------------------------------------------------
sub _subHPageBreak {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my @aBreak;
my $iCnt = unpack( "v", $sWk );
return undef unless ( defined $oBook->{_CurSheet} );
#BIFF8
if ( $oBook->{BIFFVersion} >= verBIFF8 ) {
for ( my $i = 0 ; $i < $iCnt ; $i++ ) {
my ( $iRow, $iColB, $iColE ) =
unpack( 'v3', substr( $sWk, 2 + $i * 6, 6 ) );
# push @aBreak, [$iRow, $iColB, $iColE];
push @aBreak, $iRow;
}
}
#Before BIFF8
else {
for ( my $i = 0 ; $i < $iCnt ; $i++ ) {
my ($iRow) = unpack( 'v', substr( $sWk, 2 + $i * 2, 2 ) );
push @aBreak, $iRow;
# push @aBreak, [$iRow, 0, 255];
}
}
@aBreak = sort { $a <=> $b } @aBreak;
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{HPageBreak} = \@aBreak;
}
#------------------------------------------------------------------------------
# _subVPageBreak (for Spreadsheet::ParseExcel) DK: P447
#------------------------------------------------------------------------------
sub _subVPageBreak {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
my @aBreak;
my $iCnt = unpack( "v", $sWk );
#BIFF8
if ( $oBook->{BIFFVersion} >= verBIFF8 ) {
for ( my $i = 0 ; $i < $iCnt ; $i++ ) {
my ( $iCol, $iRowB, $iRowE ) =
unpack( 'v3', substr( $sWk, 2 + $i * 6, 6 ) );
push @aBreak, $iCol;
# push @aBreak, [$iCol, $iRowB, $iRowE];
}
}
#Before BIFF8
else {
for ( my $i = 0 ; $i < $iCnt ; $i++ ) {
my ($iCol) = unpack( 'v', substr( $sWk, 2 + $i * 2, 2 ) );
push @aBreak, $iCol;
# push @aBreak, [$iCol, 0, 65535];
}
}
@aBreak = sort { $a <=> $b } @aBreak;
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{VPageBreak} = \@aBreak;
}
#------------------------------------------------------------------------------
# _subMargin (for Spreadsheet::ParseExcel) DK: P306, 345, 400, 440
#------------------------------------------------------------------------------
sub _subMargin {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
# The "Mergin" options are a workaround for a backward compatible typo.
my $dWk = _convDval( substr( $sWk, 0, 8 ) );
if ( $bOp == 0x26 ) {
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{LeftMergin} = $dWk;
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{LeftMargin} = $dWk;
}
elsif ( $bOp == 0x27 ) {
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{RightMergin} = $dWk;
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{RightMargin} = $dWk;
}
elsif ( $bOp == 0x28 ) {
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{TopMergin} = $dWk;
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{TopMargin} = $dWk;
}
elsif ( $bOp == 0x29 ) {
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{BottomMergin} = $dWk;
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{BottomMargin} = $dWk;
}
}
#------------------------------------------------------------------------------
# _subHcenter (for Spreadsheet::ParseExcel) DK: P340
#------------------------------------------------------------------------------
sub _subHcenter {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
my $iWk = unpack( "v", $sWk );
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{HCenter} = $iWk;
}
#------------------------------------------------------------------------------
# _subVcenter (for Spreadsheet::ParseExcel) DK: P447
#------------------------------------------------------------------------------
sub _subVcenter {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
my $iWk = unpack( "v", $sWk );
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{VCenter} = $iWk;
}
#------------------------------------------------------------------------------
# _subPrintGridlines (for Spreadsheet::ParseExcel) DK: P397
#------------------------------------------------------------------------------
sub _subPrintGridlines {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
my $iWk = unpack( "v", $sWk );
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{PrintGrid} = $iWk;
}
#------------------------------------------------------------------------------
# _subPrintHeaders (for Spreadsheet::ParseExcel) DK: P397
#------------------------------------------------------------------------------
sub _subPrintHeaders {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
my $iWk = unpack( "v", $sWk );
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{PrintHeaders} = $iWk;
}
#------------------------------------------------------------------------------
# _subSETUP (for Spreadsheet::ParseExcel) DK: P409
#------------------------------------------------------------------------------
sub _subSETUP {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
# Workaround for some apps and older Excels that don't write a
# complete SETUP record.
return undef if $bLen != 34;
my $oWkS = $oBook->{Worksheet}[ $oBook->{_CurSheet} ];
my $iGrBit;
(
$oWkS->{PaperSize}, $oWkS->{Scale}, $oWkS->{PageStart},
$oWkS->{FitWidth}, $oWkS->{FitHeight}, $iGrBit,
$oWkS->{Res}, $oWkS->{VRes},
) = unpack( 'v8', $sWk );
$oWkS->{HeaderMargin} = _convDval( substr( $sWk, 16, 8 ) );
$oWkS->{FooterMargin} = _convDval( substr( $sWk, 24, 8 ) );
$oWkS->{Copis} = unpack( 'v2', substr( $sWk, 32, 2 ) );
$oWkS->{LeftToRight} = ( ( $iGrBit & 0x01 ) ? 1 : 0 );
$oWkS->{Landscape} = ( ( $iGrBit & 0x02 ) ? 1 : 0 );
$oWkS->{NoPls} = ( ( $iGrBit & 0x04 ) ? 1 : 0 );
$oWkS->{NoColor} = ( ( $iGrBit & 0x08 ) ? 1 : 0 );
$oWkS->{Draft} = ( ( $iGrBit & 0x10 ) ? 1 : 0 );
$oWkS->{Notes} = ( ( $iGrBit & 0x20 ) ? 1 : 0 );
$oWkS->{NoOrient} = ( ( $iGrBit & 0x40 ) ? 1 : 0 );
$oWkS->{UsePage} = ( ( $iGrBit & 0x80 ) ? 1 : 0 );
# The NoPls flag indicates that the values have not been taken from an
# actual printer and thus may not be accurate.
# Set default scale if NoPls otherwise it may be an invalid value of 0XFF.
$oWkS->{Scale} = 100 if $oWkS->{NoPls};
# Workaround for a backward compatible typo.
$oWkS->{HeaderMergin} = $oWkS->{HeaderMargin};
$oWkS->{FooterMergin} = $oWkS->{FooterMargin};
}
#------------------------------------------------------------------------------
# _subName (for Spreadsheet::ParseExcel) DK: P350
#------------------------------------------------------------------------------
sub _subName {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
my (
$iGrBit, $cKey, $cCh, $iCce, $ixAls,
$iTab, $cchCust, $cchDsc, $cchHep, $cchStatus
) = unpack( 'vc2v3c4', $sWk );
#Builtin Name + Length == 1
if ( ( $iGrBit & 0x20 ) && ( $cCh == 1 ) ) {
#BIFF8
if ( $oBook->{BIFFVersion} >= verBIFF8 ) {
my $iName = unpack( 'n', substr( $sWk, 14 ) );
my $iSheet = unpack( 'v', substr( $sWk, 8 ) ) - 1;
# Workaround for mal-formed Excel workbooks where Print_Title is
# set as Global (i.e. itab = 0). Note, this will have to be
# treated differently when we get around to handling global names.
return undef if $iSheet == -1;
if ( $iName == 6 ) { #PrintArea
my ( $iSheetW, $raArea ) = _ParseNameArea( substr( $sWk, 16 ) );
$oBook->{PrintArea}[$iSheet] = $raArea;
}
elsif ( $iName == 7 ) { #Title
my ( $iSheetW, $raArea ) = _ParseNameArea( substr( $sWk, 16 ) );
my @aTtlR = ();
my @aTtlC = ();
foreach my $raI (@$raArea) {
if ( $raI->[3] == 0xFF ) { #Row Title
push @aTtlR, [ $raI->[0], $raI->[2] ];
}
else { #Col Title
push @aTtlC, [ $raI->[1], $raI->[3] ];
}
}
$oBook->{PrintTitle}[$iSheet] =
{ Row => \@aTtlR, Column => \@aTtlC };
}
}
else {
my $iName = unpack( 'c', substr( $sWk, 14 ) );
if ( $iName == 6 ) { #PrintArea
my ( $iSheet, $raArea ) =
_ParseNameArea95( substr( $sWk, 15 ) );
$oBook->{PrintArea}[$iSheet] = $raArea;
}
elsif ( $iName == 7 ) { #Title
my ( $iSheet, $raArea ) =
_ParseNameArea95( substr( $sWk, 15 ) );
my @aTtlR = ();
my @aTtlC = ();
foreach my $raI (@$raArea) {
if ( $raI->[3] == 0xFF ) { #Row Title
push @aTtlR, [ $raI->[0], $raI->[2] ];
}
else { #Col Title
push @aTtlC, [ $raI->[1], $raI->[3] ];
}
}
$oBook->{PrintTitle}[$iSheet] =
{ Row => \@aTtlR, Column => \@aTtlC };
}
}
}
}
#------------------------------------------------------------------------------
# ParseNameArea (for Spreadsheet::ParseExcel) DK: 494 (ptgAread3d)
#------------------------------------------------------------------------------
sub _ParseNameArea {
my ($sObj) = @_;
my ($iOp);
my @aRes = ();
$iOp = unpack( 'C', $sObj );
my $iSheet;
if ( $iOp == 0x3b ) {
my ( $iWkS, $iRs, $iRe, $iCs, $iCe ) =
unpack( 'v5', substr( $sObj, 1 ) );
$iSheet = $iWkS;
push @aRes, [ $iRs, $iCs, $iRe, $iCe ];
}
elsif ( $iOp == 0x29 ) {
my $iLen = unpack( 'v', substr( $sObj, 1, 2 ) );
my $iSt = 0;
while ( $iSt < $iLen ) {
my ( $iOpW, $iWkS, $iRs, $iRe, $iCs, $iCe ) =
unpack( 'cv5', substr( $sObj, $iSt + 3, 11 ) );
if ( $iOpW == 0x3b ) {
$iSheet = $iWkS;
push @aRes, [ $iRs, $iCs, $iRe, $iCe ];
}
if ( $iSt == 0 ) {
$iSt += 11;
}
else {
$iSt += 12; #Skip 1 byte;
}
}
}
return ( $iSheet, \@aRes );
}
#------------------------------------------------------------------------------
# ParseNameArea95 (for Spreadsheet::ParseExcel) DK: 494 (ptgAread3d)
#------------------------------------------------------------------------------
sub _ParseNameArea95 {
my ($sObj) = @_;
my ($iOp);
my @aRes = ();
$iOp = unpack( 'C', $sObj );
my $iSheet;
if ( $iOp == 0x3b ) {
$iSheet = unpack( 'v', substr( $sObj, 11, 2 ) );
my ( $iRs, $iRe, $iCs, $iCe ) =
unpack( 'v2C2', substr( $sObj, 15, 6 ) );
push @aRes, [ $iRs, $iCs, $iRe, $iCe ];
}
elsif ( $iOp == 0x29 ) {
my $iLen = unpack( 'v', substr( $sObj, 1, 2 ) );
my $iSt = 0;
while ( $iSt < $iLen ) {
my $iOpW = unpack( 'c', substr( $sObj, $iSt + 3, 6 ) );
$iSheet = unpack( 'v', substr( $sObj, $iSt + 14, 2 ) );
my ( $iRs, $iRe, $iCs, $iCe ) =
unpack( 'v2C2', substr( $sObj, $iSt + 18, 6 ) );
push @aRes, [ $iRs, $iCs, $iRe, $iCe ] if ( $iOpW == 0x3b );
if ( $iSt == 0 ) {
$iSt += 21;
}
else {
$iSt += 22; #Skip 1 byte;
}
}
}
return ( $iSheet, \@aRes );
}
#------------------------------------------------------------------------------
# _subBOOL (for Spreadsheet::ParseExcel) DK: P452
#------------------------------------------------------------------------------
sub _subWSBOOL {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{PageFit} =
( ( unpack( 'v', $sWk ) & 0x100 ) ? 1 : 0 );
}
#------------------------------------------------------------------------------
# _subMergeArea (for Spreadsheet::ParseExcel) DK: (Not)
#------------------------------------------------------------------------------
sub _subMergeArea {
my ( $oBook, $bOp, $bLen, $sWk ) = @_;
return undef unless ( defined $oBook->{_CurSheet} );
my $iCnt = unpack( "v", $sWk );
my $oWkS = $oBook->{Worksheet}[ $oBook->{_CurSheet} ];
$oWkS->{MergedArea} = [] unless ( defined $oWkS->{MergedArea} );
for ( my $i = 0 ; $i < $iCnt ; $i++ ) {
my ( $iRs, $iRe, $iCs, $iCe ) =
unpack( 'v4', substr( $sWk, $i * 8 + 2, 8 ) );
for ( my $iR = $iRs ; $iR <= $iRe ; $iR++ ) {
for ( my $iC = $iCs ; $iC <= $iCe ; $iC++ ) {
$oWkS->{Cells}[$iR][$iC]->{Merged} = 1
if ( defined $oWkS->{Cells}[$iR][$iC] );
}
}
push @{ $oWkS->{MergedArea} }, [ $iRs, $iCs, $iRe, $iCe ];
}
}
#------------------------------------------------------------------------------
# DecodeBoolErr (for Spreadsheet::ParseExcel) DK: P306
#------------------------------------------------------------------------------
sub DecodeBoolErr {
my ( $iVal, $iFlg ) = @_;
if ($iFlg) { # ERROR
if ( $iVal == 0x00 ) {
return "#NULL!";
}
elsif ( $iVal == 0x07 ) {
return "#DIV/0!";
}
elsif ( $iVal == 0x0F ) {
return "#VALUE!";
}
elsif ( $iVal == 0x17 ) {
return "#REF!";
}
elsif ( $iVal == 0x1D ) {
return "#NAME?";
}
elsif ( $iVal == 0x24 ) {
return "#NUM!";
}
elsif ( $iVal == 0x2A ) {
return "#N/A!";
}
else {
return "#ERR";
}
}
else {
return ($iVal) ? "TRUE" : "FALSE";
}
}
###############################################################################
#
# _decode_rk_number()
#
# Convert an encoded RK number into a real number. The RK encoding is
# explained in some detail in the MS docs. It is a way of storing applicable
# ints and doubles in 32bits (30 data + 2 info bits) in order to save space.
#
sub _decode_rk_number {
my $rk_number = shift;
my $number;
# Check the main RK type.
if ( $rk_number & 0x02 ) {
# RK Type 2 and 4, a packed integer.
# Shift off the info bits.
$number = $rk_number >> 2;
# Convert from unsigned to signed if required.
$number -= 0x40000000 if $number & 0x20000000;
}
else {
# RK Type 1 and 3, a truncated IEEE Double.
# Pack the RK number into the high 30 bits of an IEEE double.
$number = pack "VV", 0x0000, $rk_number & 0xFFFFFFFC;
# Reverse the packed IEEE double on big-endian machines.
$number = reverse $number if $BIGENDIAN;
# Unpack the number.
$number = unpack "d", $number;
}
# RK Types 3 and 4 were multiplied by 100 prior to encoding.
$number /= 100 if $rk_number & 0x01;
return $number;
}
###############################################################################
#
# _subStrWk()
#
# Extract the workbook strings from the SST (Shared String Table) record and
# any following CONTINUE records.
#
# The workbook strings are initially contained in the SST block but may also
# occupy one or more CONTINUE blocks. Reading the CONTINUE blocks is made a
# little tricky by the fact that they can contain an additional initial byte
# if a string is continued from a previous block.
#
# Parsing is further complicated by the fact that the continued section of the
# string may have a different encoding (ASCII or UTF-8) from the previous
# section. Excel does this to save space.
#
sub _subStrWk {
my ( $self, $biff_data, $is_continue ) = @_;
if ($is_continue) {
# We are reading a CONTINUE record.
if ( $self->{_buffer} eq '' ) {
# A CONTINUE block with no previous SST.
$self->{_buffer} .= $biff_data;
}
elsif ( !defined $self->{_string_continued} ) {
# The CONTINUE block starts with a new (non-continued) string.
# Strip the Grbit byte and store the string data.
$self->{_buffer} .= substr $biff_data, 1;
}
else {
# A CONTINUE block that starts with a continued string.
# The first byte (Grbit) of the CONTINUE record indicates if (0)
# the continued string section is single bytes or (1) double bytes.
my $grbit = ord $biff_data;
my ( $str_position, $str_length ) = @{ $self->{_previous_info} };
my $buff_length = length $self->{_buffer};
if ( $buff_length >= ( $str_position + $str_length ) ) {
# Not in a string.
$self->{_buffer} .= $biff_data;
}
elsif ( ( $self->{_string_continued} & 0x01 ) == ( $grbit & 0x01 ) )
{
# Same encoding as the previous block of the string.
$self->{_buffer} .= substr( $biff_data, 1 );
}
else {
# Different encoding to the previous block of the string.
if ( $grbit & 0x01 ) {
# Current block is UTF-16, previous was ASCII.
my ( undef, $cch ) = unpack 'vc', $self->{_buffer};
substr( $self->{_buffer}, 2, 1 ) = pack( 'C', $cch | 0x01 );
# Convert the previous ASCII, single character, portion of
# the string into a double character UTF-16 string by
# inserting zero bytes.
for (
my $i = ( $buff_length - $str_position ) ;
$i >= 1 ;
$i--
)
{
substr( $self->{_buffer}, $str_position + $i, 0 ) =
"\x00";
}
}
else {
# Current block is ASCII, previous was UTF-16.
# Convert the current ASCII, single character, portion of
# the string into a double character UTF-16 string by
# inserting null bytes.
my $change_length =
( $str_position + $str_length ) - $buff_length;
# Length of the current CONTINUE record data.
my $biff_length = length $biff_data;
# Restrict the portion to be changed to the current block
# if the string extends over more than one block.
if ( $change_length > ( $biff_length - 1 ) * 2 ) {
$change_length = ( $biff_length - 1 ) * 2;
}
# Insert the null bytes.
for ( my $i = ( $change_length / 2 ) ; $i >= 1 ; $i-- ) {
substr( $biff_data, $i + 1, 0 ) = "\x00";
}
}
# Strip the Grbit byte and store the string data.
$self->{_buffer} .= substr $biff_data, 1;
}
}
}
else {
# Not a CONTINUE block therefore an SST block.
$self->{_buffer} .= $biff_data;
}
# Reset the state variables.
$self->{_string_continued} = undef;
$self->{_previous_info} = undef;
# Extract out any full strings from the current buffer leaving behind a
# partial string that is continued into the next block, or an empty
# buffer is no string is continued.
while ( length $self->{_buffer} >= 4 ) {
my ( $str_info, $length, $str_position, $str_length ) =
_convBIFF8String( $self, $self->{_buffer}, 1 );
if ( defined $str_info->[0] ) {
push @{ $self->{PkgStr} },
{
Text => $str_info->[0],
Unicode => $str_info->[1],
Rich => $str_info->[2],
Ext => $str_info->[3],
};
$self->{_buffer} = substr( $self->{_buffer}, $length );
}
else {
$self->{_string_continued} = $str_info->[1];
$self->{_previous_info} = [ $str_position, $str_length ];
last;
}
}
}
#------------------------------------------------------------------------------
# _SwapForUnicode (for Spreadsheet::ParseExcel)
#------------------------------------------------------------------------------
sub _SwapForUnicode {
my ($sObj) = @_;
# for(my $i = 0; $i<length($$sObj); $i+=2){
for ( my $i = 0 ; $i < ( int( length($$sObj) / 2 ) * 2 ) ; $i += 2 ) {
my $sIt = substr( $$sObj, $i, 1 );
substr( $$sObj, $i, 1 ) = substr( $$sObj, $i + 1, 1 );
substr( $$sObj, $i + 1, 1 ) = $sIt;
}
}
#------------------------------------------------------------------------------
# _NewCell (for Spreadsheet::ParseExcel)
#------------------------------------------------------------------------------
sub _NewCell {
my ( $oBook, $iR, $iC, %rhKey ) = @_;
my ( $sWk, $iLen );
return undef unless ( defined $oBook->{_CurSheet} );
my $FmtClass = $oBook->{FmtClass};
$rhKey{Type} =
$FmtClass->ChkType( $rhKey{Numeric}, $rhKey{Format}{FmtIdx} );
my $FmtStr = $oBook->{FormatStr}{ $rhKey{Format}{FmtIdx} };
# Set "Date" type if required for numbers in a MulRK BIFF block.
if ( defined $FmtStr && $rhKey{Type} eq "Numeric" ) {
# Match a range of possible date formats. Note: this isn't important
# except for reporting. The number will still be converted to a date
# by ExcelFmt() even if 'Type' isn't set to 'Date'.
if ( $FmtStr =~ m{^[dmy][-\\/dmy]*$}i ) {
$rhKey{Type} = "Date";
}
}
my $oCell = Spreadsheet::ParseExcel::Cell->new(
Val => $rhKey{Val},
FormatNo => $rhKey{FormatNo},
Format => $rhKey{Format},
Code => $rhKey{Code},
Type => $rhKey{Type},
);
$oCell->{_Kind} = $rhKey{Kind};
$oCell->{_Value} = $FmtClass->ValFmt( $oCell, $oBook );
if ( $rhKey{Rich} ) {
my @aRich = ();
my $sRich = $rhKey{Rich};
for ( my $iWk = 0 ; $iWk < length($sRich) ; $iWk += 4 ) {
my ( $iPos, $iFnt ) = unpack( 'v2', substr( $sRich, $iWk ) );
push @aRich, [ $iPos, $oBook->{Font}[$iFnt] ];
}
$oCell->{Rich} = \@aRich;
}
if ( defined $_CellHandler ) {
if ( defined $_Object ) {
no strict;
ref($_CellHandler) eq "CODE"
? $_CellHandler->(
$_Object, $oBook, $oBook->{_CurSheet}, $iR, $iC, $oCell
)
: $_CellHandler->callback( $_Object, $oBook, $oBook->{_CurSheet},
$iR, $iC, $oCell );
}
else {
$_CellHandler->( $oBook, $oBook->{_CurSheet}, $iR, $iC, $oCell );
}
}
unless ($_NotSetCell) {
$oBook->{Worksheet}[ $oBook->{_CurSheet} ]->{Cells}[$iR][$iC] = $oCell;
}
return $oCell;
}
#------------------------------------------------------------------------------
# ColorIdxToRGB (for Spreadsheet::ParseExcel)
#
# TODO JMN Make this a Workbook method and re-document.
#
#------------------------------------------------------------------------------
sub ColorIdxToRGB {
my ( $sPkg, $iIdx ) = @_;
return ( ( defined $aColor[$iIdx] ) ? $aColor[$iIdx] : $aColor[0] );
}
###############################################################################
#
# error().
#
# Return an error string for a failed parse().
#
sub error {
my $self = shift;
my $parse_error = $self->{_error_status};
if (exists $error_strings{$parse_error}) {
return $error_strings{$parse_error};
}
else {
return 'Unknown parse error';
}
}
###############################################################################
#
# error_code().
#
# Return an error code for a failed parse().
#
sub error_code {
my $self = shift;
return $self->{_error_status};
}
###############################################################################
#
# Mapping between legacy method names and new names.
#
{
no warnings; # Ignore warnings about variables used only once.
*Parse = *parse;
}
1;
__END__
=head1 NAME
Spreadsheet::ParseExcel - Read information from an Excel file.
=head1 SYNOPSIS
#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel;
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('Book1.xls');
if ( !defined $workbook ) {
die $parser->error(), ".\n";
}
for my $worksheet ( $workbook->worksheets() ) {
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
for my $row ( $row_min .. $row_max ) {
for my $col ( $col_min .. $col_max ) {
my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;
print "Row, Col = ($row, $col)\n";
print "Value = ", $cell->value(), "\n";
print "Unformatted = ", $cell->unformatted(), "\n";
print "\n";
}
}
}
=head1 DESCRIPTION
The Spreadsheet::ParseExcel module can be used to read information from Excel 95-2003 binary files.
The module cannot read files in the Excel 2007 Open XML XLSX format. See the L<Spreadsheet::XLSX> module instead.
=head1 Parser
=head2 new()
The C<new()> method is used to create a new C<Spreadsheet::ParseExcel> parser object.
my $parser = Spreadsheet::ParseExcel->new();
As an advanced feature it is also possible to pass a call-back handler to the parser to control the parsing of the spreadsheet.
$parser = Spreadsheet::ParseExcel->new(
[
CellHandler => \&cell_handler,
NotSetCell => 1,
]);
The call-back can be used to ignore certain cells or to reduce memory usage. See the section L<Reducing the memory usage of Spreadsheet::ParseExcel> for more information.
=head2 parse($filename, [$formatter])
The Parser C<parse()> method returns a L</Workbook> object.
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('Book1.xls');
If an error occurs C<parse()> returns C<undef>. In general programs should contain a test for failed parsing as follows:
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('Book1.xls');
if ( !defined $workbook ) {
die $parser->error(), ".\n";
}
The C<$filename> parameter is generally the file to be parsed. However, it can also be a filehandle or a scalar reference.
The optional C<$formatter> array ref can be an reference to a L</Formatter Class> to format the value of cells.
Note: Versions of Spreadsheet::ParseExcel prior to 0.50 also documented a Workbook C<parse()> method as a syntactic shortcut for the above C<new()> and C<parse()> combination. This is now deprecated since it breaks error handling.
=head2 error()
The Parser C<error()> method returns an error string if a C<parse()> fails:
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('Book1.xls');
if ( !defined $workbook ) {
die $parser->error(), ".\n";
}
If you wish to generate you own error string you can use the C<error_code()> method instead (see below). The C<error()> and C<error_code()> values are as follows:
error() error_code()
======= ============
'' 0
'File not found' 1
'No Excel data found in file' 2
'File is encrypted' 3
Spreadsheet::ParseExcel doesn't try to decrypt an encrypted Excel file. That is beyond the current scope of the module.
The C<error_code()> method is explained below.
=head2 error_code()
The Parser C<error_code()> method returns an error code if a C<parse()> fails:
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('Book1.xls');
if ( !defined $workbook ) {
die "Got error code ", $parser->error_code, ".\n";
}
This can be useful if you wish to employ you own error strings or error handling methods.
=head1 Workbook
A C<Spreadsheet::ParseExcel::Workbook> is created via the C<Spreadsheet::ParseExcel> C<parse()> method:
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('Book1.xls');
The main methods of the Workbook class are:
$workbook->worksheets()
$workbook->worksheet()
$workbook->worksheet_count()
$workbook->get_filename()
These more commonly used methods of the Workbook class are outlined below. The other, less commonly used, methods are documented in L<Spreadsheet::ParseExcel::Worksheet>.
=head2 worksheets()
Returns an array of L</Worksheet> objects. This was most commonly used to iterate over the worksheets in a workbook:
for my $worksheet ( $workbook->worksheets() ) {
...
}
=head2 worksheet()
The C<worksheet()> method returns a single C<Worksheet> object using either its name or index:
$worksheet = $workbook->worksheet('Sheet1');
$worksheet = $workbook->worksheet(0);
Returns C<undef> if the sheet name or index doesn't exist.
=head2 worksheet_count()
The C<worksheet_count()> method returns the number of Worksheet objects in the Workbook.
my $worksheet_count = $workbook->worksheet_count();
=head2 get_filename()
The C<get_filename()> method returns the name of the Excel file of C<undef> if the data was read from a filehandle rather than a file.
my $filename = $workbook->get_filename();
=head2 Other Workbook Methods
For full documentation of the methods available via a Workbook object see L<Spreadsheet::ParseExcel::Workbook>.
=head1 Worksheet
The C<Spreadsheet::ParseExcel::Worksheet> class encapsulates the properties of an Excel worksheet.
A Worksheet object is obtained via the L</worksheets()> or L</worksheet()> methods.
for my $worksheet ( $workbook->worksheets() ) {
...
}
# Or:
$worksheet = $workbook->worksheet('Sheet1');
$worksheet = $workbook->worksheet(0);
The most commonly used methods of the Worksheet class are:
$worksheet->get_cell()
$worksheet->row_range()
$worksheet->col_range()
$worksheet->get_name()
The Spreadsheet::ParseExcel::Worksheet class exposes a lot of methods but in general very few are required unless you are writing an advanced filter.
The most commonly used methods are detailed below. The others are documented in L<Spreadsheet::ParseExcel::Worksheet>.
=head2 get_cell($row, $col)
Return the L</Cell> object at row C<$row> and column C<$col> if it is defined. Otherwise returns undef.
my $cell = $worksheet->get_cell($row, $col);
=head2 row_range()
Returns a two-element list C<($min, $max)> containing the minimum and maximum defined rows in the worksheet. If there is no row defined C<$max> is smaller than C<$min>.
my ( $row_min, $row_max ) = $worksheet->row_range();
=head2 col_range()
Returns a two-element list C<($min, $max)> containing the minimum and maximum of defined columns in the worksheet. If there is no column defined C<$max> is smaller than C<$min>.
my ( $col_min, $col_max ) = $worksheet->col_range();
=head2 get_name()
The C<get_name()> method returns the name of the worksheet, such as 'Sheet1'.
my $name = $worksheet->get_name();
=head2 Other Worksheet Methods
For other, less commonly used, Worksheet methods see L<Spreadsheet::ParseExcel::Worksheet>.
=head1 Cell
The C<Spreadsheet::ParseExcel::Cell> class has the following main methods.
$cell->value()
$cell->unformatted()
=head2 value()
The C<value()> method returns the formatted value of the cell.
my $value = $cell->value();
Formatted in this sense refers to the numeric format of the cell value. For example a number such as 40177 might be formatted as 40,117, 40117.000 or even as the date 2009/12/30.
If the cell doesn't contain a numeric format then the formatted and unformatted cell values are the same, see the C<unformatted()> method below.
For a defined C<$cell> the C<value()> method will always return a value.
In the case of a cell with formatting but no numeric or string contents the method will return the empty string C<''>.
=head2 unformatted()
The C<unformatted()> method returns the unformatted value of the cell.
my $unformatted = $cell->unformatted();
Returns the cell value without a numeric format. See the C<value()> method above.
=head2 Other Cell Methods
For other, less commonly used, Worksheet methods see L<Spreadsheet::ParseExcel::Cell>.
=head1 Format
The C<Spreadsheet::ParseExcel::Format> class has the following properties:
=head2 Format properties
$format->{Font}
$format->{AlignH}
$format->{AlignV}
$format->{Indent}
$format->{Wrap}
$format->{Shrink}
$format->{Rotate}
$format->{JustLast}
$format->{ReadDir}
$format->{BdrStyle}
$format->{BdrColor}
$format->{BdrDiag}
$format->{Fill}
$format->{Lock}
$format->{Hidden}
$format->{Style}
These properties are generally only of interest to advanced users. Casual users can skip this section.
=head2 $format->{Font}
Returns the L</Font> object for the Format.
=head2 $format->{AlignH}
Returns the horizontal alignment of the format where the value has the following meaning:
0 => No alignment
1 => Left
2 => Center
3 => Right
4 => Fill
5 => Justify
6 => Center across
7 => Distributed/Equal spaced
=head2 $format->{AlignV}
Returns the vertical alignment of the format where the value has the following meaning:
0 => Top
1 => Center
2 => Bottom
3 => Justify
4 => Distributed/Equal spaced
=head2 $format->{Indent}
Returns the indent level of the C<Left> horizontal alignment.
=head2 $format->{Wrap}
Returns true if textwrap is on.
=head2 $format->{Shrink}
Returns true if "Shrink to fit" is set for the format.
=head2 $format->{Rotate}
Returns the text rotation. In Excel97+, it returns the angle in degrees of the text rotation.
In Excel95 or earlier it returns a value as follows:
0 => No rotation
1 => Top down
2 => 90 degrees anti-clockwise,
3 => 90 clockwise
=head2 $format->{JustLast}
Return true if the "justify last" property is set for the format.
=head2 $format->{ReadDir}
Returns the direction that the text is read from.
=head2 $format->{BdrStyle}
Returns an array ref of border styles as follows:
[ $left, $right, $top, $bottom ]
=head2 $format->{BdrColor}
Returns an array ref of border color indexes as follows:
[ $left, $right, $top, $bottom ]
=head2 $format->{BdrDiag}
Returns an array ref of diagonal border kind, style and color index as follows:
[$kind, $style, $color ]
Where kind is:
0 => None
1 => Right-Down
2 => Right-Up
3 => Both
=head2 $format->{Fill}
Returns an array ref of fill pattern and color indexes as follows:
[ $pattern, $front_color, $back_color ]
=head2 $format->{Lock}
Returns true if the cell is locked.
=head2 $format->{Hidden}
Returns true if the cell is Hidden.
=head2 $format->{Style}
Returns true if the format is a Style format.
=head1 Font
I<Spreadsheet::ParseExcel::Font>
Format class has these properties:
=head1 Font Properties
$font->{Name}
$font->{Bold}
$font->{Italic}
$font->{Height}
$font->{Underline}
$font->{UnderlineStyle}
$font->{Color}
$font->{Strikeout}
$font->{Super}
=head2 $font->{Name}
Returns the name of the font, for example 'Arial'.
=head2 $font->{Bold}
Returns true if the font is bold.
=head2 $font->{Italic}
Returns true if the font is italic.
=head2 $font->{Height}
Returns the size (height) of the font.
=head2 $font->{Underline}
Returns true if the font in underlined.
=head2 $font->{UnderlineStyle}
Returns the style of an underlined font where the value has the following meaning:
0 => None
1 => Single
2 => Double
33 => Single accounting
34 => Double accounting
=head2 $font->{Color}
Returns the color index for the font. The index can be converted to a RGB string using the C<ColorIdxToRGB()> Parser method.
=head2 $font->{Strikeout}
Returns true if the font has the strikeout property set.
=head2 $font->{Super}
Returns one of the following values if the superscript or subscript property of the font is set:
0 => None
1 => Superscript
2 => Subscript
=head1 Formatter class
I<Spreadsheet::ParseExcel::Fmt*>
Formatter class will convert cell data.
Spreadsheet::ParseExcel includes 2 formatter classes. C<FmtDefault> and C<FmtJapanese>. It is also possible to create a user defined formatting class.
The formatter class C<Spreadsheet::ParseExcel::Fmt*> should provide the following functions:
=head2 ChkType($self, $is_numeric, $format_index)
Method to check the type of data in the cell. Should return C<Date>, C<Numeric> or C<Text>. It is passed the following parameters:
=over
=item $self
A scalar reference to the Formatter object.
=item $is_numeric
If true, the value seems to be number.
=item $format_index
The index number for the cell Format object.
=back
=head2 TextFmt($self, $string_data, $string_encoding)
Converts the string data in the cell into the correct encoding. It is passed the following parameters:
=over
=item $self
A scalar reference to the Formatter object.
=item $string_data
The original string/text data.
=item $string_encoding
The character encoding of original string/text.
=back
=head2 ValFmt($self, $cell, $workbook)
Convert the original unformatted cell value into the appropriate formatted value. For instance turn a number into a formatted date. It is passed the following parameters:
=over
=item $self
A scalar reference to the Formatter object.
=item $cell
A scalar reference to the Cell object.
=item $workbook
A scalar reference to the Workbook object.
=back
=head2 FmtString($self, $cell, $workbook)
Get the format string for the Cell. It is passed the following parameters:
=over
=item $self
A scalar reference to the Formatter object.
=item $cell
A scalar reference to the Cell object.
=item $workbook
A scalar reference to the Workbook object.
=back
=head1 Reducing the memory usage of Spreadsheet::ParseExcel
In some cases a C<Spreadsheet::ParseExcel> application may consume a lot of memory when processing a large Excel file and, as a result, may fail to complete. The following explains why this can occur and how to resolve it.
C<Spreadsheet::ParseExcel> processes an Excel file in two stages. In the first stage it extracts the Excel binary stream from the OLE container file using C<OLE::Storage_Lite>. In the second stage it parses the binary stream to read workbook, worksheet and cell data which it then stores in memory. The majority of the memory usage is required for storing cell data.
The reason for this is that as the Excel file is parsed and each cell is encountered a cell handling function creates a relatively large nested cell object that contains the cell value and all of the data that relates to the cell formatting. For large files (a 10MB Excel file on a 256MB system) this overhead can cause the system to grind to a halt.
However, in a lot of cases when an Excel file is being processed the only information that is required are the cell values. In these cases it is possible to avoid most of the memory overhead by specifying your own cell handling function and by telling Spreadsheet::ParseExcel not to store the parsed cell data. This is achieved by passing a cell handler function to C<new()> when creating the parse object. Here is an example.
#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel;
my $parser = Spreadsheet::ParseExcel->new(
CellHandler => \&cell_handler,
NotSetCell => 1
);
my $workbook = $parser->parse('file.xls');
sub cell_handler {
my $workbook = $_[0];
my $sheet_index = $_[1];
my $row = $_[2];
my $col = $_[3];
my $cell = $_[4];
# Do something useful with the formatted cell value
print $cell->value(), "\n";
}
The user specified cell handler is passed as a code reference to C<new()> along with the parameter C<NotSetCell> which tells Spreadsheet::ParseExcel not to store the parsed cell. Note, you don't have to iterate over the rows and columns, this happens automatically as part of the parsing.
The cell handler is passed 5 arguments. The first, C<$workbook>, is a reference to the C<Spreadsheet::ParseExcel::Workbook> object that represent the parsed workbook. This can be used to access any of the C<Spreadsheet::ParseExcel::Workbook> methods, see L</Workbook>. The second C<$sheet_index> is the zero-based index of the worksheet being parsed. The third and fourth, C<$row> and C<$col>, are the zero-based row and column number of the cell. The fifth, C<$cell>, is a reference to the C<Spreadsheet::ParseExcel::Cell> object. This is used to extract the data from the cell. See L</Cell> for more information.
This technique can be useful if you are writing an Excel to database filter since you can put your DB calls in the cell handler.
If you don't want all of the data in the spreadsheet you can add some control logic to the cell handler. For example we can extend the previous example so that it only prints the first 10 rows of the first two worksheets in the parsed workbook by adding some C<if()> statements to the cell handler:
#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel;
my $parser = Spreadsheet::ParseExcel->new(
CellHandler => \&cell_handler,
NotSetCell => 1
);
my $workbook = $parser->parse('file.xls');
sub cell_handler {
my $workbook = $_[0];
my $sheet_index = $_[1];
my $row = $_[2];
my $col = $_[3];
my $cell = $_[4];
# Skip some worksheets and rows (inefficiently).
return if $sheet_index >= 3;
return if $row >= 10;
# Do something with the formatted cell value
print $cell->value(), "\n";
}
However, this still processes the entire workbook. If you wish to save some additional processing time you can abort the parsing after you have read the data that you want, using the workbook C<ParseAbort> method:
#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel;
my $parser = Spreadsheet::ParseExcel->new(
CellHandler => \&cell_handler,
NotSetCell => 1
);
my $workbook = $parser->parse('file.xls');
sub cell_handler {
my $workbook = $_[0];
my $sheet_index = $_[1];
my $row = $_[2];
my $col = $_[3];
my $cell = $_[4];
# Skip some worksheets and rows (more efficiently).
if ( $sheet_index >= 1 and $row >= 10 ) {
$workbook->ParseAbort(1);
return;
}
# Do something with the formatted cell value
print $cell->value(), "\n";
}
=head1 KNOWN PROBLEMS
=over
=item * Issues reported by users: http://rt.cpan.org/Public/Dist/Display.html?Name=Spreadsheet-ParseExcel
=item * This module cannot read the values of formulas from files created with Spreadsheet::WriteExcel unless the user specified the values when creating the file (which is generally not the case). The reason for this is that Spreadsheet::WriteExcel writes the formula but not the formula result since it isn't in a position to calculate arbitrary Excel formulas without access to Excel's formula engine.
=item * If Excel has date fields where the specified format is equal to the system-default for the short-date locale, Excel does not store the format, but defaults to an internal format which is system dependent. In these cases ParseExcel uses the date format 'yyyy-mm-dd'.
=back
=head1 REPORTING A BUG
Bugs can be reported via rt.cpan.org. See the following for instructions on bug reporting for Spreadsheet::ParseExcel
http://rt.cpan.org/Public/Dist/Display.html?Name=Spreadsheet-ParseExcel
=head1 SEE ALSO
=over
=item * xls2csv by Ken Prows (http://search.cpan.org/~ken/xls2csv-1.06/script/xls2csv).
=item * xls2csv and xlscat by H.Merijn Brand (these utilities are part of Spreadsheet::Read, see below).
=item * excel2txt by Ken Youens-Clark, (http://search.cpan.org/~kclark/excel2txt/excel2txt). This is an excellent example of an Excel filter using Spreadsheet::ParseExcel. It can produce CSV, Tab delimited, Html, XML and Yaml.
=item * XLSperl by Jon Allen (http://search.cpan.org/~jonallen/XLSperl/bin/XLSperl). This application allows you to use Perl "one-liners" with Microsoft Excel files.
=item * Spreadsheet::XLSX (http://search.cpan.org/~dmow/Spreadsheet-XLSX/lib/Spreadsheet/XLSX.pm) by Dmitry Ovsyanko. A module with a similar interface to Spreadsheet::ParseExcel for parsing Excel 2007 XLSX OpenXML files.
=item * Spreadsheet::Read (http://search.cpan.org/~hmbrand/Spreadsheet-Read/Read.pm) by H.Merijn Brand. A single interface for reading several different spreadsheet formats.
=item * Spreadsheet::WriteExcel (http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel/lib/Spreadsheet/WriteExcel.pm). A perl module for creating new Excel files.
=item * Spreadsheet::ParseExcel::SaveParser (http://search.cpan.org/~jmcnamara/Spreadsheet-ParseExcel/lib/Spreadsheet/ParseExcel/SaveParser.pm). This is a combination of Spreadsheet::ParseExcel and Spreadsheet::WriteExcel and it allows you to "rewrite" an Excel file. See the following example (http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel/lib/Spreadsheet/WriteExcel.pm#MODIFYING_AND_REWRITING_EXCEL_FILES). It is part of the Spreadsheet::ParseExcel distro.
=item * Text::CSV_XS (http://search.cpan.org/~hmbrand/Text-CSV_XS/CSV_XS.pm) by H.Merijn Brand. A fast and rigorous module for reading and writing CSV data. Don't consider rolling your own CSV handling, use this module instead.
=back
=head1 MAILING LIST
There is a Google group for discussing and asking questions about Spreadsheet::ParseExcel. This is a good place to search to see if your question has been asked before: http://groups-beta.google.com/group/spreadsheet-parseexcel/
=head1 DONATIONS
If you'd care to donate to the Spreadsheet::ParseExcel project, you can do so via PayPal: http://tinyurl.com/7ayes
=head1 TODO
=over
=item * The current maintenance work is directed towards making the documentation more useful, improving and simplifying the API, and improving the maintainability of the code base. After that new features will be added.
=item * Fix open bugs and documentation for SaveParser.
=item * Add Formula support, Hyperlink support, Named Range support.
=item * Improve Spreadsheet::ParseExcel::SaveParser compatibility with Spreadsheet::WriteExcel.
=item * Improve Unicode and other encoding support. This will probably require dropping support for perls prior to 5.8+.
=back
=head1 ACKNOWLEDGEMENTS
From Kawai Takanori:
First of all, I would like to acknowledge the following valuable programs and modules:
XHTML, OLE::Storage and Spreadsheet::WriteExcel.
In no particular order: Yamaji Haruna, Simamoto Takesi, Noguchi Harumi, Ikezawa Kazuhiro, Suwazono Shugo, Hirofumi Morisada, Michael Edwards, Kim Namusk, Slaven Rezic, Grant Stevens, H.Merijn Brand and many many people + Kawai Mikako.
=head1 DISCLAIMER OF WARRANTY
Because this software is licensed free of charge, there is no warranty for the software, to the extent permitted by applicable law. Except when otherwise stated in writing the copyright holders and/or other parties provide the software "as is" without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the software is with you. Should the software prove defective, you assume the cost of all necessary servicing, repair, or correction.
In no event unless required by applicable law or agreed to in writing will any copyright holder, or any other party who may modify and/or redistribute the software as permitted by the above licence, be liable to you for damages, including any general, special, incidental, or consequential damages arising out of the use or inability to use the software (including but not limited to loss of data or data being rendered inaccurate or losses sustained by you or third parties or a failure of the software to operate with any other software), even if such holder or other party has been advised of the possibility of such damages.
=head1 LICENSE
Either the Perl Artistic Licence http://dev.perl.org/licenses/artistic.html or the GPL http://www.opensource.org/licenses/gpl-license.php
=head1 AUTHOR
Current maintainer 0.40+: John McNamara jmcnamara@cpan.org
Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
Original author: Kawai Takanori (Hippo2000) kwitknr@cpan.org
=head1 COPYRIGHT
Copyright (c) 2009-2010 John McNamara
Copyright (c) 2006-2008 Gabor Szabo
Copyright (c) 2000-2006 Kawai Takanori
All rights reserved. This is free software. You may distribute under the terms of either the GNU General Public License or the Artistic License.
=cut
|