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
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import static org.apache.poi.extractor.ExtractorFactory.OOXML_PACKAGE;
import static org.apache.poi.openxml4j.opc.TestContentType.isOldXercesActive;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeMap;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.poi.POIDataSamples;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ooxml.POIXMLDocumentPart.RelationPart;
import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.ooxml.POIXMLProperties;
import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.sl.usermodel.ObjectShape;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
import org.apache.poi.ss.formula.EvaluationConditionalFormatRule;
import org.apache.poi.ss.formula.FormulaParser;
import org.apache.poi.ss.formula.FormulaRenderer;
import org.apache.poi.ss.formula.FormulaShifter;
import org.apache.poi.ss.formula.FormulaType;
import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.formula.WorkbookEvaluatorProvider;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.functions.Function;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.NullOutputStream;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.TempFile;
import org.apache.poi.util.XMLHelper;
import org.apache.poi.xssf.SXSSFITestDataProvider;
import org.apache.poi.xssf.XLSBUnsupportedException;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.CalculationChain;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
import org.apache.xmlbeans.XmlException;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMergeCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMergeCells;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTFontImpl;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
public final class TestXSSFBugs extends BaseTestBugzillaIssues {
private static final POILogger LOG = POILogFactory.getLogger(TestXSSFBugs.class);
public TestXSSFBugs() {
super(XSSFITestDataProvider.instance);
}
/**
* test writing a file with large number of unique strings,
* open resulting file in Excel to check results!
*/
@Test
public void bug15375_2() throws IOException {
bug15375(1000);
}
/**
* Named ranges had the right reference, but
* the wrong sheet name
*/
@Test
public void bug45430() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("45430.xlsx")) {
assertFalse(wb.isMacroEnabled());
assertEquals(3, wb.getNumberOfNames());
assertEquals(0, wb.getName("SheetAA1").getCTName().getLocalSheetId());
assertFalse(wb.getName("SheetAA1").getCTName().isSetLocalSheetId());
assertEquals("SheetA!$A$1", wb.getName("SheetAA1").getRefersToFormula());
assertEquals("SheetA", wb.getName("SheetAA1").getSheetName());
assertEquals(0, wb.getName("SheetBA1").getCTName().getLocalSheetId());
assertFalse(wb.getName("SheetBA1").getCTName().isSetLocalSheetId());
assertEquals("SheetB!$A$1", wb.getName("SheetBA1").getRefersToFormula());
assertEquals("SheetB", wb.getName("SheetBA1").getSheetName());
assertEquals(0, wb.getName("SheetCA1").getCTName().getLocalSheetId());
assertFalse(wb.getName("SheetCA1").getCTName().isSetLocalSheetId());
assertEquals("SheetC!$A$1", wb.getName("SheetCA1").getRefersToFormula());
assertEquals("SheetC", wb.getName("SheetCA1").getSheetName());
// Save and re-load, still there
try (XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb)) {
assertEquals(3, nwb.getNumberOfNames());
assertEquals("SheetA!$A$1", nwb.getName("SheetAA1").getRefersToFormula());
}
}
}
/**
* We should carry vba macros over after save
*/
@Test
public void bug45431() throws IOException, InvalidFormatException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("45431.xlsm");
OPCPackage pkg1 = wb1.getPackage();
assertTrue(wb1.isMacroEnabled());
// Check the various macro related bits can be found
PackagePart vba = pkg1.getPart(
PackagingURIHelper.createPartName("/xl/vbaProject.bin")
);
assertNotNull(vba);
// And the drawing bit
PackagePart drw = pkg1.getPart(
PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
);
assertNotNull(drw);
// Save and re-open, both still there
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
pkg1.close();
wb1.close();
OPCPackage pkg2 = wb2.getPackage();
assertTrue(wb2.isMacroEnabled());
vba = pkg2.getPart(
PackagingURIHelper.createPartName("/xl/vbaProject.bin")
);
assertNotNull(vba);
drw = pkg2.getPart(
PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
);
assertNotNull(drw);
// And again, just to be sure
XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
pkg2.close();
wb2.close();
OPCPackage pkg3 = wb3.getPackage();
assertTrue(wb3.isMacroEnabled());
vba = pkg3.getPart(
PackagingURIHelper.createPartName("/xl/vbaProject.bin")
);
assertNotNull(vba);
drw = pkg3.getPart(
PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
);
assertNotNull(drw);
pkg3.close();
wb3.close();
}
@Test
public void bug47504() throws IOException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("47504.xlsx");
assertEquals(1, wb1.getNumberOfSheets());
XSSFSheet sh = wb1.getSheetAt(0);
XSSFDrawing drawing = sh.createDrawingPatriarch();
List<RelationPart> rels = drawing.getRelationParts();
assertEquals(1, rels.size());
assertEquals("Sheet1!A1", rels.get(0).getRelationship().getTargetURI().getFragment());
// And again, just to be sure
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
assertEquals(1, wb2.getNumberOfSheets());
sh = wb2.getSheetAt(0);
drawing = sh.createDrawingPatriarch();
rels = drawing.getRelationParts();
assertEquals(1, rels.size());
assertEquals("Sheet1!A1", rels.get(0).getRelationship().getTargetURI().getFragment());
wb2.close();
}
/**
* Excel will sometimes write a button with a textbox
* containing >br< (not closed!).
* Clearly Excel shouldn't do this, but test that we can
* read the file despite the naughtiness
*/
@Test
public void bug49020() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("BrNotClosed.xlsx")) {
assertNotNull(wb);
}
}
/**
* ensure that CTPhoneticPr is loaded by the ooxml test suite so that it is included in poi-ooxml-schemas
*/
@Test
public void bug49325() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49325.xlsx")) {
CTWorksheet sh = wb.getSheetAt(0).getCTWorksheet();
assertNotNull(sh.getPhoneticPr());
}
}
/**
* Names which are defined with a Sheet
* should return that sheet index properly
*/
@Test
public void bug48923() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48923.xlsx")) {
assertEquals(4, wb.getNumberOfNames());
Name b1 = wb.getName("NameB1");
Name b2 = wb.getName("NameB2");
Name sheet2 = wb.getName("NameSheet2");
Name test = wb.getName("Test");
assertNotNull(b1);
assertEquals("NameB1", b1.getNameName());
assertEquals("Sheet1", b1.getSheetName());
assertEquals(-1, b1.getSheetIndex());
assertFalse(b1.isDeleted());
assertFalse(b1.isHidden());
assertNotNull(b2);
assertEquals("NameB2", b2.getNameName());
assertEquals("Sheet1", b2.getSheetName());
assertEquals(-1, b2.getSheetIndex());
assertFalse(b2.isDeleted());
assertFalse(b2.isHidden());
assertNotNull(sheet2);
assertEquals("NameSheet2", sheet2.getNameName());
assertEquals("Sheet2", sheet2.getSheetName());
assertEquals(-1, sheet2.getSheetIndex());
assertNotNull(test);
assertEquals("Test", test.getNameName());
assertEquals("Sheet1", test.getSheetName());
assertEquals(-1, test.getSheetIndex());
}
}
/**
* Problem with evaluation formulas due to
* NameXPtgs.
* Blows up on:
* IF(B6= (ROUNDUP(B6,0) + ROUNDDOWN(B6,0))/2, MROUND(B6,2),ROUND(B6,0))
* <p>
* TODO: delete this test case when MROUND and VAR are implemented
*/
@Test
public void bug48539() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48539.xlsx")) {
assertEquals(3, wb.getNumberOfSheets());
assertEquals(0, wb.getNumberOfNames());
// Try each cell individually
XSSFFormulaEvaluator eval = new XSSFFormulaEvaluator(wb);
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
Sheet s = wb.getSheetAt(i);
for (Row r : s) {
for (Cell c : r) {
if (c.getCellType() == CellType.FORMULA) {
CellValue cv = eval.evaluate(c);
if (cv.getCellType() == CellType.NUMERIC) {
// assert that the calculated value agrees with
// the cached formula result calculated by Excel
String formula = c.getCellFormula();
double cachedFormulaResult = c.getNumericCellValue();
double evaluatedFormulaResult = cv.getNumberValue();
assertEquals(formula, cachedFormulaResult, evaluatedFormulaResult, 1E-7);
}
}
}
}
}
// Now all of them
XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
}
}
/**
* Foreground colours should be found even if
* a theme is used
*/
@Test
public void bug48779() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48779.xlsx")) {
XSSFCell cell = wb.getSheetAt(0).getRow(0).getCell(0);
XSSFCellStyle cs = cell.getCellStyle();
assertNotNull(cs);
assertEquals(1, cs.getIndex());
// Look at the low level xml elements
assertEquals(2, cs.getCoreXf().getFillId());
assertEquals(0, cs.getCoreXf().getXfId());
assertTrue(cs.getCoreXf().getApplyFill());
XSSFCellFill fg = wb.getStylesSource().getFillAt(2);
assertNotNull(fg.getFillForegroundColor());
assertEquals(0, fg.getFillForegroundColor().getIndexed());
assertEquals(0.0, fg.getFillForegroundColor().getTint(), 0);
assertEquals("FFFF0000", fg.getFillForegroundColor().getARGBHex());
assertNotNull(fg.getFillBackgroundColor());
assertEquals(64, fg.getFillBackgroundColor().getIndexed());
// Now look higher up
assertNotNull(cs.getFillForegroundXSSFColor());
assertEquals(0, cs.getFillForegroundColor());
assertEquals("FFFF0000", cs.getFillForegroundXSSFColor().getARGBHex());
assertEquals("FFFF0000", cs.getFillForegroundColorColor().getARGBHex());
assertEquals(64, cs.getFillBackgroundColor());
assertNull(cs.getFillBackgroundXSSFColor().getARGBHex());
assertNull(cs.getFillBackgroundColorColor().getARGBHex());
}
}
/**
* Ensure General and @ format are working properly
* for integers
*/
@Test
public void bug47490() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("GeneralFormatTests.xlsx")) {
Sheet s = wb.getSheetAt(1);
Row r;
DataFormatter df = new DataFormatter();
r = s.getRow(1);
assertEquals(1.0, r.getCell(2).getNumericCellValue(), 0);
assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
assertEquals("1", df.formatCellValue(r.getCell(2)));
assertEquals("1", df.formatRawCellContents(1.0, -1, "@"));
assertEquals("1", df.formatRawCellContents(1.0, -1, "General"));
r = s.getRow(2);
assertEquals(12.0, r.getCell(2).getNumericCellValue(), 0);
assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
assertEquals("12", df.formatCellValue(r.getCell(2)));
assertEquals("12", df.formatRawCellContents(12.0, -1, "@"));
assertEquals("12", df.formatRawCellContents(12.0, -1, "General"));
r = s.getRow(3);
assertEquals(123.0, r.getCell(2).getNumericCellValue(), 0);
assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
assertEquals("123", df.formatCellValue(r.getCell(2)));
assertEquals("123", df.formatRawCellContents(123.0, -1, "@"));
assertEquals("123", df.formatRawCellContents(123.0, -1, "General"));
}
}
/**
* A problem file from a non-standard source (a scientific instrument that saves its
* output as an .xlsx file) that have two issues:
* 1. The Content Type part name is lower-case: [content_types].xml
* 2. The file appears to use backslashes as path separators
* <p>
* The OPC spec tolerates both of these peculiarities, so does POI
*/
@Test
public void bug49609() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49609.xlsx")) {
assertEquals("FAM", wb.getSheetName(0));
assertEquals("Cycle", wb.getSheetAt(0).getRow(0).getCell(1).getStringCellValue());
}
}
@Test
public void bug49783() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("49783.xlsx")) {
Sheet sheet = wb.getSheetAt(0);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
Cell cell;
cell = sheet.getRow(0).getCell(0);
assertEquals("#REF!*#REF!", cell.getCellFormula());
assertEquals(CellType.ERROR, evaluator.evaluateInCell(cell).getCellType());
assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString());
Name nm1 = wb.getName("sale_1");
assertNotNull("name sale_1 should be present", nm1);
assertEquals("Sheet1!#REF!", nm1.getRefersToFormula());
Name nm2 = wb.getName("sale_2");
assertNotNull("name sale_2 should be present", nm2);
assertEquals("Sheet1!#REF!", nm2.getRefersToFormula());
cell = sheet.getRow(1).getCell(0);
assertEquals("sale_1*sale_2", cell.getCellFormula());
assertEquals(CellType.ERROR, evaluator.evaluateInCell(cell).getCellType());
assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString());
}
}
/**
* Creating a rich string of "hello world" and applying
* a font to characters 1-5 means we have two strings,
* "hello" and " world". As such, we need to apply
* preserve spaces to the 2nd bit, lest we end up
* with something like "helloworld" !
*/
@Test
public void bug49941() throws IOException {
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet s = wb1.createSheet();
XSSFRow r = s.createRow(0);
XSSFCell c = r.createCell(0);
// First without fonts
c.setCellValue(
new XSSFRichTextString(" with spaces ")
);
assertEquals(" with spaces ", c.getRichStringCellValue().toString());
assertEquals(0, c.getRichStringCellValue().getCTRst().sizeOfRArray());
assertTrue(c.getRichStringCellValue().getCTRst().isSetT());
// Should have the preserve set
assertEquals(
1,
c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().getLength()
);
assertEquals(
"preserve",
c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().item(0).getNodeValue()
);
// Save and check
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
s = wb2.getSheetAt(0);
r = s.getRow(0);
c = r.getCell(0);
assertEquals(" with spaces ", c.getRichStringCellValue().toString());
assertEquals(0, c.getRichStringCellValue().getCTRst().sizeOfRArray());
assertTrue(c.getRichStringCellValue().getCTRst().isSetT());
// Change the string
c.setCellValue(
new XSSFRichTextString("hello world")
);
assertEquals("hello world", c.getRichStringCellValue().toString());
// Won't have preserve
assertEquals(
0,
c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().getLength()
);
// Apply a font
XSSFFont f = wb2.createFont();
f.setBold(true);
c.getRichStringCellValue().applyFont(0, 5, f);
assertEquals("hello world", c.getRichStringCellValue().toString());
// Does need preserving on the 2nd part
assertEquals(2, c.getRichStringCellValue().getCTRst().sizeOfRArray());
assertEquals(
0,
c.getRichStringCellValue().getCTRst().getRArray(0).xgetT().getDomNode().getAttributes().getLength()
);
assertEquals(
1,
c.getRichStringCellValue().getCTRst().getRArray(1).xgetT().getDomNode().getAttributes().getLength()
);
assertEquals(
"preserve",
c.getRichStringCellValue().getCTRst().getRArray(1).xgetT().getDomNode().getAttributes().item(0).getNodeValue()
);
// Save and check
XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
wb2.close();
s = wb3.getSheetAt(0);
r = s.getRow(0);
c = r.getCell(0);
assertEquals("hello world", c.getRichStringCellValue().toString());
wb3.close();
}
/**
* Repeatedly writing the same file which has styles
*/
@Test
public void bug49940() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("styles.xlsx")) {
assertEquals(3, wb.getNumberOfSheets());
assertEquals(10, wb.getStylesSource().getNumCellStyles());
ByteArrayOutputStream b1 = new ByteArrayOutputStream();
ByteArrayOutputStream b2 = new ByteArrayOutputStream();
ByteArrayOutputStream b3 = new ByteArrayOutputStream();
wb.write(b1);
wb.write(b2);
wb.write(b3);
for (byte[] data : new byte[][]{
b1.toByteArray(), b2.toByteArray(), b3.toByteArray()
}) {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
XSSFWorkbook wb2 = new XSSFWorkbook(bais);
assertEquals(3, wb2.getNumberOfSheets());
assertEquals(10, wb2.getStylesSource().getNumCellStyles());
wb2.close();
}
}
}
/**
* Various ways of removing a cell formula should all zap the calcChain
* entry.
*/
@Test
public void bug49966() throws IOException {
try (XSSFWorkbook wb1 = XSSFTestDataSamples
.openSampleWorkbook("shared_formulas.xlsx")) {
XSSFSheet sheet = wb1.getSheetAt(0);
XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
// CalcChain has lots of entries
CalculationChain cc = wb1.getCalculationChain();
assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
assertEquals("A3", cc.getCTCalcChain().getCArray(1).getR());
assertEquals("A4", cc.getCTCalcChain().getCArray(2).getR());
assertEquals("A5", cc.getCTCalcChain().getCArray(3).getR());
assertEquals("A6", cc.getCTCalcChain().getCArray(4).getR());
assertEquals("A7", cc.getCTCalcChain().getCArray(5).getR());
assertEquals("A8", cc.getCTCalcChain().getCArray(6).getR());
assertEquals(40, cc.getCTCalcChain().sizeOfCArray());
XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
// Try various ways of changing the formulas
// If it stays a formula, chain entry should remain
// Otherwise should go
sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay
sheet.getRow(2).getCell(0).setCellFormula(null); // go
sheet.getRow(3).getCell(0).setCellFormula("14"); // stay
XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
sheet.getRow(4).getCell(0).setBlank(); // go
XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
validateCells(sheet);
sheet.getRow(5).removeCell(sheet.getRow(5).getCell(0)); // go
validateCells(sheet);
XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
sheet.getRow(6).getCell(0).setBlank(); // go
XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
sheet.getRow(7).getCell(0).setCellValue((String) null); // go
XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
// Save and check
try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1)) {
wb1.close();
assertEquals(35, cc.getCTCalcChain().sizeOfCArray());
cc = wb2.getCalculationChain();
assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
assertEquals("A4", cc.getCTCalcChain().getCArray(1).getR());
assertEquals("A9", cc.getCTCalcChain().getCArray(2).getR());
}
}
}
@Test
public void bug49966Row() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples
.openSampleWorkbook("shared_formulas.xlsx")) {
XSSFSheet sheet = wb.getSheetAt(0);
validateCells(sheet);
sheet.getRow(5).removeCell(sheet.getRow(5).getCell(0)); // go
validateCells(sheet);
}
}
private void validateCells(XSSFSheet sheet) {
for (Row row : sheet) {
// trigger handling
((XSSFRow) row).onDocumentWrite();
}
}
@Test
public void bug49156() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("49156.xlsx")) {
FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType() == CellType.FORMULA) {
formulaEvaluator.evaluateInCell(cell); // caused NPE on some cells
}
}
}
}
}
/**
* Newlines are valid characters in a formula
*/
@Test
public void bug50440And51875() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("NewlineInFormulas.xlsx")) {
Sheet s = wb.getSheetAt(0);
Cell c = s.getRow(0).getCell(0);
assertEquals("SUM(\n1,2\n)", c.getCellFormula());
assertEquals(3.0, c.getNumericCellValue(), 0);
FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
formulaEvaluator.evaluateFormulaCell(c);
assertEquals("SUM(\n1,2\n)", c.getCellFormula());
assertEquals(3.0, c.getNumericCellValue(), 0);
// For 51875
Cell b3 = s.getRow(2).getCell(1);
formulaEvaluator.evaluateFormulaCell(b3);
assertEquals("B1+B2", b3.getCellFormula()); // The newline is lost for shared formulas
assertEquals(3.0, b3.getNumericCellValue(), 0);
}
}
/**
* Moving a cell comment from one cell to another
*/
@Test
public void bug50795() throws IOException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("50795.xlsx");
XSSFSheet sheet = wb1.getSheetAt(0);
XSSFRow row = sheet.getRow(0);
XSSFCell cellWith = row.getCell(0);
XSSFCell cellWithoutComment = row.getCell(1);
assertNotNull(cellWith.getCellComment());
assertNull(cellWithoutComment.getCellComment());
String exp = "\u0410\u0432\u0442\u043e\u0440:\ncomment";
XSSFComment comment = cellWith.getCellComment();
assertEquals(exp, comment.getString().getString());
// Check we can write it out and read it back as-is
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
row = sheet.getRow(0);
cellWith = row.getCell(0);
cellWithoutComment = row.getCell(1);
// Double check things are as expected
assertNotNull(cellWith.getCellComment());
assertNull(cellWithoutComment.getCellComment());
comment = cellWith.getCellComment();
assertEquals(exp, comment.getString().getString());
// Move the comment
cellWithoutComment.setCellComment(comment);
// Write out and re-check
XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
wb2.close();
sheet = wb3.getSheetAt(0);
row = sheet.getRow(0);
// Ensure it swapped over
cellWith = row.getCell(0);
cellWithoutComment = row.getCell(1);
assertNull(cellWith.getCellComment());
assertNotNull(cellWithoutComment.getCellComment());
comment = cellWithoutComment.getCellComment();
assertEquals(exp, comment.getString().getString());
wb3.close();
}
/**
* When the cell background colour is set with one of the first
* two columns of the theme colour palette, the colours are
* shades of white or black.
* For those cases, ensure we don't break on reading the colour
*/
@Test
public void bug50299() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("50299.xlsx")) {
// Check all the colours
for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) {
Sheet s = wb.getSheetAt(sn);
for (Row r : s) {
for (Cell c : r) {
CellStyle cs = c.getCellStyle();
if (cs != null) {
cs.getFillForegroundColor();
}
}
}
}
// Check one bit in detail
// Check that we get back foreground=0 for the theme colours,
// and background=64 for the auto colouring
Sheet s = wb.getSheetAt(0);
assertEquals(0, s.getRow(0).getCell(8).getCellStyle().getFillForegroundColor());
assertEquals(64, s.getRow(0).getCell(8).getCellStyle().getFillBackgroundColor());
assertEquals(0, s.getRow(1).getCell(8).getCellStyle().getFillForegroundColor());
assertEquals(64, s.getRow(1).getCell(8).getCellStyle().getFillBackgroundColor());
}
}
/**
* Excel .xls style indexed colours in a .xlsx file
*/
@Test
public void bug50786() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50786-indexed_colours.xlsx")) {
XSSFSheet s = wb.getSheetAt(0);
XSSFRow r = s.getRow(2);
// Check we have the right cell
XSSFCell c = r.getCell(1);
assertEquals("test\u00a0", c.getRichStringCellValue().getString());
// It should be light green
XSSFCellStyle cs = c.getCellStyle();
assertEquals(42, cs.getFillForegroundColor());
assertEquals(42, cs.getFillForegroundColorColor().getIndexed());
assertNotNull(cs.getFillForegroundColorColor().getRGB());
assertEquals("FFCCFFCC", cs.getFillForegroundColorColor().getARGBHex());
}
}
/**
* If the border colours are set with themes, then we
* should still be able to get colours
*/
@Test
public void bug50846() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50846-border_colours.xlsx")) {
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row = sheet.getRow(0);
// Border from a theme, brown
XSSFCell cellT = row.getCell(0);
XSSFCellStyle styleT = cellT.getCellStyle();
XSSFColor colorT = styleT.getBottomBorderXSSFColor();
assertEquals(5, colorT.getTheme());
assertEquals("FFC0504D", colorT.getARGBHex());
// Border from a style direct, red
XSSFCell cellS = row.getCell(1);
XSSFCellStyle styleS = cellS.getCellStyle();
XSSFColor colorS = styleS.getBottomBorderXSSFColor();
assertEquals(0, colorS.getTheme());
assertEquals("FFFF0000", colorS.getARGBHex());
}
}
/**
* Fonts where their colours come from the theme rather
* then being set explicitly still should allow the
* fetching of the RGB.
*/
@Test
public void bug50784() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50784-font_theme_colours.xlsx")) {
XSSFSheet s = wb.getSheetAt(0);
XSSFRow r = s.getRow(0);
// Column 1 has a font with regular colours
XSSFCell cr = r.getCell(1);
XSSFFont fr = wb.getFontAt(cr.getCellStyle().getFontIndex());
XSSFColor colr = fr.getXSSFColor();
// No theme, has colours
assertEquals(0, colr.getTheme());
assertNotNull(colr.getRGB());
// Column 0 has a font with colours from a theme
XSSFCell ct = r.getCell(0);
XSSFFont ft = wb.getFontAt(ct.getCellStyle().getFontIndex());
XSSFColor colt = ft.getXSSFColor();
// Has a theme, which has the colours on it
assertEquals(9, colt.getTheme());
XSSFColor themeC = wb.getTheme().getThemeColor(colt.getTheme());
assertNotNull(themeC.getRGB());
assertNotNull(colt.getRGB());
assertEquals(themeC.getARGBHex(), colt.getARGBHex()); // The same colour
}
}
/**
* New lines were being eaten when setting a font on
* a rich text string
*/
@Test
public void bug48877() throws IOException {
String text = "Use \n with word wrap on to create a new line.\n" +
"This line finishes with two trailing spaces. ";
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet = wb1.createSheet();
Font font1 = wb1.createFont();
font1.setColor((short) 20);
Font font2 = wb1.createFont();
font2.setColor(Font.COLOR_RED);
Font font3 = wb1.getFontAt(0);
XSSFRow row = sheet.createRow(2);
XSSFCell cell = row.createCell(2);
XSSFRichTextString richTextString =
wb1.getCreationHelper().createRichTextString(text);
// Check the text has the newline
assertEquals(text, richTextString.getString());
// Apply the font
richTextString.applyFont(font3);
richTextString.applyFont(0, 3, font1);
cell.setCellValue(richTextString);
// To enable newlines you need set a cell styles with wrap=true
CellStyle cs = wb1.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
// Check the text has the
assertEquals(text, cell.getStringCellValue());
// Save the file and re-read it
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
row = sheet.getRow(2);
cell = row.getCell(2);
assertEquals(text, cell.getStringCellValue());
// Now add a 2nd, and check again
int fontAt = text.indexOf('\n', 6);
cell.getRichStringCellValue().applyFont(10, fontAt + 1, font2);
assertEquals(text, cell.getStringCellValue());
assertEquals(4, cell.getRichStringCellValue().numFormattingRuns());
assertEquals("Use", cell.getRichStringCellValue().getCTRst().getRArray(0).getT());
String r3 = cell.getRichStringCellValue().getCTRst().getRArray(2).getT();
assertEquals("line.\n", r3.substring(r3.length() - 6));
// Save and re-check
XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
wb2.close();
sheet = wb3.getSheetAt(0);
row = sheet.getRow(2);
cell = row.getCell(2);
assertEquals(text, cell.getStringCellValue());
wb3.close();
// FileOutputStream out = new FileOutputStream("/tmp/test48877.xlsx");
// wb.write(out);
// out.close();
}
/**
* Adding sheets when one has a table, then re-ordering
*/
@Test
public void bug50867() throws IOException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("50867_with_table.xlsx");
assertEquals(3, wb1.getNumberOfSheets());
XSSFSheet s1 = wb1.getSheetAt(0);
XSSFSheet s2 = wb1.getSheetAt(1);
XSSFSheet s3 = wb1.getSheetAt(2);
assertEquals(1, s1.getTables().size());
assertEquals(0, s2.getTables().size());
assertEquals(0, s3.getTables().size());
XSSFTable t = s1.getTables().get(0);
assertEquals("Tabella1", t.getName());
assertEquals("Tabella1", t.getDisplayName());
assertEquals("A1:C3", t.getCTTable().getRef());
// Add a sheet and re-order
XSSFSheet s4 = wb1.createSheet("NewSheet");
wb1.setSheetOrder(s4.getSheetName(), 0);
// Check on tables
assertEquals(1, s1.getTables().size());
assertEquals(0, s2.getTables().size());
assertEquals(0, s3.getTables().size());
assertEquals(0, s4.getTables().size());
// Refetch to get the new order
s1 = wb1.getSheetAt(0);
s2 = wb1.getSheetAt(1);
s3 = wb1.getSheetAt(2);
s4 = wb1.getSheetAt(3);
assertEquals(0, s1.getTables().size());
assertEquals(1, s2.getTables().size());
assertEquals(0, s3.getTables().size());
assertEquals(0, s4.getTables().size());
// Save and re-load
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
s1 = wb2.getSheetAt(0);
s2 = wb2.getSheetAt(1);
s3 = wb2.getSheetAt(2);
s4 = wb2.getSheetAt(3);
assertEquals(0, s1.getTables().size());
assertEquals(1, s2.getTables().size());
assertEquals(0, s3.getTables().size());
assertEquals(0, s4.getTables().size());
t = s2.getTables().get(0);
assertEquals("Tabella1", t.getName());
assertEquals("Tabella1", t.getDisplayName());
assertEquals("A1:C3", t.getCTTable().getRef());
// Add some more tables, and check
t = s2.createTable(null);
t.setName("New 2");
t.setDisplayName("New 2");
t = s3.createTable(null);
t.setName("New 3");
t.setDisplayName("New 3");
XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
wb2.close();
s1 = wb3.getSheetAt(0);
s2 = wb3.getSheetAt(1);
s3 = wb3.getSheetAt(2);
s4 = wb3.getSheetAt(3);
assertEquals(0, s1.getTables().size());
assertEquals(2, s2.getTables().size());
assertEquals(1, s3.getTables().size());
assertEquals(0, s4.getTables().size());
t = s2.getTables().get(0);
assertEquals("Tabella1", t.getName());
assertEquals("Tabella1", t.getDisplayName());
assertEquals("A1:C3", t.getCTTable().getRef());
t = s2.getTables().get(1);
assertEquals("New 2", t.getName());
assertEquals("New 2", t.getDisplayName());
t = s3.getTables().get(0);
assertEquals("New 3", t.getName());
assertEquals("New 3", t.getDisplayName());
// Check the relationships
assertEquals(0, s1.getRelations().size());
assertEquals(3, s2.getRelations().size());
assertEquals(1, s3.getRelations().size());
assertEquals(0, s4.getRelations().size());
assertEquals(
XSSFRelation.PRINTER_SETTINGS.getContentType(),
s2.getRelations().get(0).getPackagePart().getContentType()
);
assertEquals(
XSSFRelation.TABLE.getContentType(),
s2.getRelations().get(1).getPackagePart().getContentType()
);
assertEquals(
XSSFRelation.TABLE.getContentType(),
s2.getRelations().get(2).getPackagePart().getContentType()
);
assertEquals(
XSSFRelation.TABLE.getContentType(),
s3.getRelations().get(0).getPackagePart().getContentType()
);
assertEquals(
"/xl/tables/table3.xml",
s3.getRelations().get(0).getPackagePart().getPartName().toString()
);
wb3.close();
}
/**
* Setting repeating rows and columns shouldn't break
* any print settings that were there before
*/
@Test
public void bug49253() throws IOException {
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFWorkbook wb2 = new XSSFWorkbook();
CellRangeAddress cra = CellRangeAddress.valueOf("C2:D3");
// No print settings before repeating
XSSFSheet s1 = wb1.createSheet();
assertFalse(s1.getCTWorksheet().isSetPageSetup());
assertTrue(s1.getCTWorksheet().isSetPageMargins());
s1.setRepeatingColumns(cra);
s1.setRepeatingRows(cra);
assertTrue(s1.getCTWorksheet().isSetPageSetup());
assertTrue(s1.getCTWorksheet().isSetPageMargins());
PrintSetup ps1 = s1.getPrintSetup();
assertFalse(ps1.getValidSettings());
assertFalse(ps1.getLandscape());
// Had valid print settings before repeating
XSSFSheet s2 = wb2.createSheet();
PrintSetup ps2 = s2.getPrintSetup();
assertTrue(s2.getCTWorksheet().isSetPageSetup());
assertTrue(s2.getCTWorksheet().isSetPageMargins());
ps2.setLandscape(false);
assertTrue(ps2.getValidSettings());
assertFalse(ps2.getLandscape());
s2.setRepeatingColumns(cra);
s2.setRepeatingRows(cra);
ps2 = s2.getPrintSetup();
assertTrue(s2.getCTWorksheet().isSetPageSetup());
assertTrue(s2.getCTWorksheet().isSetPageMargins());
assertTrue(ps2.getValidSettings());
assertFalse(ps2.getLandscape());
wb1.close();
wb2.close();
}
/**
* Default Column style
*/
@Test
public void bug51037() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet s = wb.createSheet();
CellStyle defaultStyle = wb.getCellStyleAt((short) 0);
assertEquals(0, defaultStyle.getIndex());
CellStyle blueStyle = wb.createCellStyle();
blueStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
blueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
assertEquals(1, blueStyle.getIndex());
CellStyle pinkStyle = wb.createCellStyle();
pinkStyle.setFillForegroundColor(IndexedColors.PINK.getIndex());
pinkStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
assertEquals(2, pinkStyle.getIndex());
// Starts empty
assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
CTCols cols = s.getCTWorksheet().getColsArray(0);
assertEquals(0, cols.sizeOfColArray());
// Add some rows and columns
XSSFRow r1 = s.createRow(0);
XSSFRow r2 = s.createRow(1);
r1.createCell(0);
r1.createCell(2);
r2.createCell(0);
r2.createCell(3);
// Check no style is there
assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
assertEquals(0, cols.sizeOfColArray());
assertEquals(defaultStyle, s.getColumnStyle(0));
assertEquals(defaultStyle, s.getColumnStyle(2));
assertEquals(defaultStyle, s.getColumnStyle(3));
// Apply the styles
s.setDefaultColumnStyle(0, pinkStyle);
s.setDefaultColumnStyle(3, blueStyle);
// Check
assertEquals(pinkStyle, s.getColumnStyle(0));
assertEquals(defaultStyle, s.getColumnStyle(2));
assertEquals(blueStyle, s.getColumnStyle(3));
assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
assertEquals(2, cols.sizeOfColArray());
assertEquals(1, cols.getColArray(0).getMin());
assertEquals(1, cols.getColArray(0).getMax());
assertEquals(pinkStyle.getIndex(), cols.getColArray(0).getStyle());
assertEquals(4, cols.getColArray(1).getMin());
assertEquals(4, cols.getColArray(1).getMax());
assertEquals(blueStyle.getIndex(), cols.getColArray(1).getStyle());
// Save, re-load and re-check
XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
wb.close();
s = wbBack.getSheetAt(0);
defaultStyle = wbBack.getCellStyleAt(defaultStyle.getIndex());
blueStyle = wbBack.getCellStyleAt(blueStyle.getIndex());
pinkStyle = wbBack.getCellStyleAt(pinkStyle.getIndex());
assertEquals(pinkStyle, s.getColumnStyle(0));
assertEquals(defaultStyle, s.getColumnStyle(2));
assertEquals(blueStyle, s.getColumnStyle(3));
wbBack.close();
}
/**
* Repeatedly writing a file.
* Something with the SharedStringsTable currently breaks...
*/
@Test
public void bug46662() throws IOException {
// New file
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
wb1.close();
// Simple file
XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
XSSFTestDataSamples.writeOutAndReadBack(wb2).close();
XSSFTestDataSamples.writeOutAndReadBack(wb2).close();
XSSFTestDataSamples.writeOutAndReadBack(wb2).close();
wb2.close();
// Complex file
// TODO
}
/**
* Colours and styles when the list has gaps in it
*/
@Test
public void bug51222() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51222.xlsx")) {
XSSFSheet s = wb.getSheetAt(0);
XSSFCell cA4_EEECE1 = s.getRow(3).getCell(0);
XSSFCell cA5_1F497D = s.getRow(4).getCell(0);
// Check the text
assertEquals("A4", cA4_EEECE1.getRichStringCellValue().getString());
assertEquals("A5", cA5_1F497D.getRichStringCellValue().getString());
// Check the styles assigned to them
assertEquals(4, cA4_EEECE1.getCTCell().getS());
assertEquals(5, cA5_1F497D.getCTCell().getS());
// Check we look up the correct style
assertEquals(4, cA4_EEECE1.getCellStyle().getIndex());
assertEquals(5, cA5_1F497D.getCellStyle().getIndex());
// Check the fills on them at the low level
assertEquals(5, cA4_EEECE1.getCellStyle().getCoreXf().getFillId());
assertEquals(6, cA5_1F497D.getCellStyle().getCoreXf().getFillId());
// These should reference themes 2 and 3
assertEquals(2, wb.getStylesSource().getFillAt(5).getCTFill().getPatternFill().getFgColor().getTheme());
assertEquals(3, wb.getStylesSource().getFillAt(6).getCTFill().getPatternFill().getFgColor().getTheme());
// Ensure we get the right colours for these themes
// TODO fix
// assertEquals("FFEEECE1", wb.getTheme().getThemeColor(2).getARGBHex());
// assertEquals("FF1F497D", wb.getTheme().getThemeColor(3).getARGBHex());
// Finally check the colours on the styles
// TODO fix
// assertEquals("FFEEECE1", cA4_EEECE1.getCellStyle().getFillForegroundXSSFColor().getARGBHex());
// assertEquals("FF1F497D", cA5_1F497D.getCellStyle().getFillForegroundXSSFColor().getARGBHex());
}
}
@Test
public void bug51470() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51470.xlsx")) {
XSSFSheet sh0 = wb.getSheetAt(0);
XSSFSheet sh1 = wb.cloneSheet(0);
List<RelationPart> rels0 = sh0.getRelationParts();
List<RelationPart> rels1 = sh1.getRelationParts();
assertEquals(1, rels0.size());
assertEquals(1, rels1.size());
PackageRelationship pr0 = rels0.get(0).getRelationship();
PackageRelationship pr1 = rels1.get(0).getRelationship();
assertEquals(pr0.getTargetMode(), pr1.getTargetMode());
assertEquals(pr0.getTargetURI(), pr1.getTargetURI());
POIXMLDocumentPart doc0 = rels0.get(0).getDocumentPart();
POIXMLDocumentPart doc1 = rels1.get(0).getDocumentPart();
assertEquals(doc0, doc1);
}
}
/**
* Add comments to Sheet 1, when Sheet 2 already has
* comments (so /xl/comments1.xml is taken)
*/
@Test
public void bug51850() throws IOException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("51850.xlsx");
XSSFSheet sh1 = wb1.getSheetAt(0);
XSSFSheet sh2 = wb1.getSheetAt(1);
// Sheet 2 has comments
assertNotNull(sh2.getCommentsTable(false));
assertEquals(1, sh2.getCommentsTable(false).getNumberOfComments());
// Sheet 1 doesn't (yet)
assertNull(sh1.getCommentsTable(false));
// Try to add comments to Sheet 1
CreationHelper factory = wb1.getCreationHelper();
Drawing<?> drawing = sh1.createDrawingPatriarch();
ClientAnchor anchor = factory.createClientAnchor();
anchor.setCol1(0);
anchor.setCol2(4);
anchor.setRow1(0);
anchor.setRow2(1);
Comment comment1 = drawing.createCellComment(anchor);
comment1.setString(
factory.createRichTextString("I like this cell. It's my favourite."));
comment1.setAuthor("Bob T. Fish");
anchor = factory.createClientAnchor();
anchor.setCol1(0);
anchor.setCol2(4);
anchor.setRow1(1);
anchor.setRow2(1);
Comment comment2 = drawing.createCellComment(anchor);
comment2.setString(
factory.createRichTextString("This is much less fun..."));
comment2.setAuthor("Bob T. Fish");
Cell c1 = sh1.getRow(0).createCell(4);
c1.setCellValue(2.3);
c1.setCellComment(comment1);
Cell c2 = sh1.getRow(0).createCell(5);
c2.setCellValue(2.1);
c2.setCellComment(comment2);
// Save and re-load
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sh1 = wb2.getSheetAt(0);
sh2 = wb2.getSheetAt(1);
// Check the comments
assertNotNull(sh2.getCommentsTable(false));
assertEquals(1, sh2.getCommentsTable(false).getNumberOfComments());
assertNotNull(sh1.getCommentsTable(false));
assertEquals(2, sh1.getCommentsTable(false).getNumberOfComments());
wb2.close();
}
/**
* Sheet names with a , in them
*/
@Test
public void bug51963() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51963.xlsx")) {
Sheet sheet = wb.getSheetAt(0);
assertEquals("Abc,1", sheet.getSheetName());
Name name = wb.getName("Intekon.ProdCodes");
assertEquals("'Abc,1'!$A$1:$A$2", name.getRefersToFormula());
AreaReference ref = wb.getCreationHelper().createAreaReference(name.getRefersToFormula());
assertEquals(0, ref.getFirstCell().getRow());
assertEquals(0, ref.getFirstCell().getCol());
assertEquals(1, ref.getLastCell().getRow());
assertEquals(0, ref.getLastCell().getCol());
}
}
/**
* Sum across multiple workbooks
* eg =SUM($Sheet1.C1:$Sheet4.C1)
*/
@Test
public void bug48703() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48703.xlsx")) {
XSSFSheet sheet = wb.getSheetAt(0);
// Contains two forms, one with a range and one a list
XSSFRow r1 = sheet.getRow(0);
XSSFRow r2 = sheet.getRow(1);
XSSFCell c1 = r1.getCell(1);
XSSFCell c2 = r2.getCell(1);
assertEquals(20.0, c1.getNumericCellValue(), 0);
assertEquals("SUM(Sheet1!C1,Sheet2!C1,Sheet3!C1,Sheet4!C1)", c1.getCellFormula());
assertEquals(20.0, c2.getNumericCellValue(), 0);
assertEquals("SUM(Sheet1:Sheet4!C1)", c2.getCellFormula());
// Try evaluating both
XSSFFormulaEvaluator eval = new XSSFFormulaEvaluator(wb);
eval.evaluateFormulaCell(c1);
eval.evaluateFormulaCell(c2);
assertEquals(20.0, c1.getNumericCellValue(), 0);
assertEquals(20.0, c2.getNumericCellValue(), 0);
}
}
/**
* Bugzilla 51710: problems reading shared formuals from .xlsx
*/
@Test
public void bug51710() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("51710.xlsx")) {
final String[] columns = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"};
final int rowMax = 500; // bug triggers on row index 59
Sheet sheet = wb.getSheetAt(0);
// go through all formula cells
for (int rInd = 2; rInd <= rowMax; rInd++) {
Row row = sheet.getRow(rInd);
for (int cInd = 1; cInd <= 12; cInd++) {
Cell cell = row.getCell(cInd);
String formula = cell.getCellFormula();
CellReference ref = new CellReference(cell);
//simulate correct answer
String correct = "$A" + (rInd + 1) + "*" + columns[cInd] + "$2";
assertEquals("Incorrect formula in " + ref.formatAsString(), correct, formula);
}
}
}
}
/**
* Bug 53101:
*/
@Test
public void bug5301() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53101.xlsx")) {
FormulaEvaluator evaluator =
wb.getCreationHelper().createFormulaEvaluator();
// A1: SUM(B1: IZ1)
double a1Value =
evaluator.evaluate(wb.getSheetAt(0).getRow(0).getCell(0)).getNumberValue();
// Assert
assertEquals(259.0, a1Value, 0.0);
// KY: SUM(B1: IZ1)
/*double ky1Value =*/
assertEquals(259.0, evaluator.evaluate(wb.getSheetAt(0).getRow(0).getCell(310)).getNumberValue(), 0.0001);
// Assert
assertEquals(259.0, a1Value, 0.0);
}
}
@Test
public void bug54436() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("54436.xlsx")) {
if (!WorkbookEvaluator.getSupportedFunctionNames().contains("GETPIVOTDATA")) {
Function func = (args, srcRowIndex, srcColumnIndex) -> ErrorEval.NA;
WorkbookEvaluator.registerFunction("GETPIVOTDATA", func);
}
wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
}
}
/**
* Password Protected .xlsx files are now (as of 4.0.0) tested for the default password
* when opened via WorkbookFactory, so there's no EncryptedDocumentException thrown anymore
*/
@Test
public void bug55692_poifs() throws IOException {
// Via a POIFSFileSystem
try (POIFSFileSystem fsP = new POIFSFileSystem(
POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"))) {
WorkbookFactory.create(fsP);
}
}
@Test
public void bug55692_stream() throws IOException {
// Directly on a Stream, will go via POIFS and spot it's
// actually a .xlsx file encrypted with the default password, and open
try (Workbook wb = WorkbookFactory.create(
POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"))) {
assertNotNull(wb);
assertEquals(3, wb.getNumberOfSheets());
}
}
@Test
public void bug55692_poifs2() throws IOException {
// Via a POIFSFileSystem, will spot it's actually a .xlsx file
// encrypted with the default password, and open
try (POIFSFileSystem fsNP = new POIFSFileSystem(
POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"))) {
Workbook wb = WorkbookFactory.create(fsNP);
assertNotNull(wb);
assertEquals(3, wb.getNumberOfSheets());
wb.close();
}
}
@Test
public void bug53282() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53282b.xlsx")) {
Cell c = wb.getSheetAt(0).getRow(1).getCell(0);
assertEquals("#@_#", c.getStringCellValue());
assertEquals("http://invalid.uri", c.getHyperlink().getAddress());
}
}
/**
* Was giving NullPointerException
* at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead
* due to a lack of Styles Table
*/
@Test
public void bug56278() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56278.xlsx")) {
assertEquals(0, wb.getSheetIndex("Market Rates"));
// Save and re-check
Workbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);
assertEquals(0, nwb.getSheetIndex("Market Rates"));
nwb.close();
}
}
@Test
public void bug56315() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56315.xlsx")) {
Cell c = wb.getSheetAt(0).getRow(1).getCell(0);
CellValue cv = wb.getCreationHelper().createFormulaEvaluator().evaluate(c);
double rounded = cv.getNumberValue();
assertEquals(0.1, rounded, 0.0);
}
}
@Test
public void bug56468() throws IOException, InterruptedException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Hi");
sheet.setRepeatingRows(new CellRangeAddress(0, 0, 0, 0));
// small hack to try to make this test stable, previously it failed whenever the two written ZIP files had different file-creation
// dates stored.
// We try to do a loop until the current second changes in order to avoid problems with some date information that is written to the ZIP and thus
// causes differences
long start = System.currentTimeMillis() / 1000;
while (System.currentTimeMillis() / 1000 == start) {
Thread.sleep(10);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream(8096);
wb.write(bos);
byte[] firstSave = bos.toByteArray();
bos.reset();
wb.write(bos);
byte[] secondSave = bos.toByteArray();
/*OutputStream stream = new FileOutputStream("C:\\temp\\poi.xlsx");
try {
wb.write(stream);
} finally {
stream.close();
}*/
assertArrayEquals("Had: \n" + Arrays.toString(firstSave) + " and \n" + Arrays.toString(secondSave),
firstSave, secondSave);
wb.close();
}
/**
* ISO-8601 style cell formats with a T in them, eg
* cell format of "yyyy-MM-ddTHH:mm:ss"
*/
@Test
public void bug54034() throws IOException {
TimeZone tz = LocaleUtil.getUserTimeZone();
LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET"));
try {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("54034.xlsx")) {
Sheet sheet = wb.getSheet("Sheet1");
Row row = sheet.getRow(1);
Cell cell = row.getCell(2);
assertTrue(DateUtil.isCellDateFormatted(cell));
DataFormatter fmt = new DataFormatter();
assertEquals("yyyy\\-mm\\-dd\\Thh:mm", cell.getCellStyle().getDataFormatString());
assertEquals("2012-08-08T22:59", fmt.formatCellValue(cell));
}
} finally {
LocaleUtil.setUserTimeZone(tz);
}
}
@Test
public void testBug53798XLSX() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xlsx")) {
File xlsOutput = TempFile.createTempFile("testBug53798", ".xlsx");
bug53798Work(wb, xlsOutput);
}
}
@Ignore("Shifting rows is not yet implemented in SXSSFSheet")
@Test
public void testBug53798XLSXStream() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xlsx")) {
File xlsOutput = TempFile.createTempFile("testBug53798", ".xlsx");
SXSSFWorkbook wb2 = new SXSSFWorkbook(wb);
bug53798Work(wb2, xlsOutput);
wb2.close();
}
}
@Test
public void testBug53798XLS() throws IOException {
Workbook wb = HSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xls");
File xlsOutput = TempFile.createTempFile("testBug53798", ".xls");
bug53798Work(wb, xlsOutput);
wb.close();
}
/**
* SUMIF was throwing a NPE on some formulas
*/
@Test
public void testBug56420SumIfNPE() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56420.xlsx")) {
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
Sheet sheet = wb.getSheetAt(0);
Row r = sheet.getRow(2);
Cell c = r.getCell(2);
assertEquals("SUMIF($A$1:$A$4,A3,$B$1:$B$4)", c.getCellFormula());
Cell eval = evaluator.evaluateInCell(c);
assertEquals(0.0, eval.getNumericCellValue(), 0.0001);
}
}
private void bug53798Work(Workbook wb, File xlsOutput) throws IOException {
Sheet testSheet = wb.getSheetAt(0);
testSheet.shiftRows(2, 2, 1);
saveAndReloadReport(wb, xlsOutput);
// 1) corrupted xlsx (unreadable data in the first row of a shifted group) already comes about
// when shifted by less than -1 negative amount (try -2)
testSheet.shiftRows(3, 3, -1);
saveAndReloadReport(wb, xlsOutput);
testSheet.shiftRows(2, 2, 1);
saveAndReloadReport(wb, xlsOutput);
// 2) attempt to create a new row IN PLACE of a removed row by a negative shift causes corrupted
// xlsx file with unreadable data in the negative shifted row.
// NOTE it's ok to create any other row.
Row newRow = testSheet.createRow(3);
saveAndReloadReport(wb, xlsOutput);
Cell newCell = newRow.createCell(0);
saveAndReloadReport(wb, xlsOutput);
newCell.setCellValue("new Cell in row " + newRow.getRowNum());
saveAndReloadReport(wb, xlsOutput);
// 3) once a negative shift has been made any attempt to shift another group of rows
// (note: outside of previously negative shifted rows) by a POSITIVE amount causes POI exception:
// org.apache.xmlbeans.impl.values.XmlValueDisconnectedException.
// NOTE: another negative shift on another group of rows is successful, provided no new rows in
// place of previously shifted rows were attempted to be created as explained above.
testSheet.shiftRows(6, 7, 1); // -- CHANGE the shift to positive once the behaviour of
// the above has been tested
saveAndReloadReport(wb, xlsOutput);
}
/**
* XSSFCell.typeMismatch on certain blank cells when formatting
* with DataFormatter
*/
@Test
public void bug56702() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56702.xlsx")) {
Sheet sheet = wb.getSheetAt(0);
// Get wrong cell by row 8 & column 7
Cell cell = sheet.getRow(8).getCell(7);
assertEquals(CellType.NUMERIC, cell.getCellType());
// Check the value - will be zero as it is <c><v/></c>
assertEquals(0.0, cell.getNumericCellValue(), 0.001);
// Try to format
DataFormatter formatter = new DataFormatter();
formatter.formatCellValue(cell);
// Check the formatting
assertEquals("0", formatter.formatCellValue(cell));
}
}
/**
* Formulas which reference named ranges, either in other
* sheets, or workbook scoped but in other workbooks.
* Used to fail with with errors like
* org.apache.poi.ss.formula.FormulaParseException: Cell reference expected after sheet name at index 9
* org.apache.poi.ss.formula.FormulaParseException: Parse error near char 0 '[' in specified formula '[0]!NR_Global_B2'. Expected number, string, or defined name
*/
@Test
public void bug56737() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56737.xlsx")) {
// Check the named range definitions
Name nSheetScope = wb.getName("NR_To_A1");
Name nWBScope = wb.getName("NR_Global_B2");
assertNotNull(nSheetScope);
assertNotNull(nWBScope);
assertEquals("Defines!$A$1", nSheetScope.getRefersToFormula());
assertEquals("Defines!$B$2", nWBScope.getRefersToFormula());
// Check the different kinds of formulas
Sheet s = wb.getSheetAt(0);
Cell cRefSName = s.getRow(1).getCell(3);
Cell cRefWName = s.getRow(2).getCell(3);
assertEquals("Defines!NR_To_A1", cRefSName.getCellFormula());
// Note the formula, as stored in the file, has the external name index not filename
// TODO Provide a way to get the one with the filename
assertEquals("[0]!NR_Global_B2", cRefWName.getCellFormula());
// Try to evaluate them
FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
assertEquals("Test A1", eval.evaluate(cRefSName).getStringValue());
assertEquals(142, (int) eval.evaluate(cRefWName).getNumberValue());
// Try to evaluate everything
eval.evaluateAll();
}
}
private void saveAndReloadReport(Workbook wb, File outFile) throws IOException {
// run some method on the font to verify if it is "disconnected" already
//for(short i = 0;i < 256;i++)
{
Font font = wb.getFontAt(0);
if (font instanceof XSSFFont) {
XSSFFont xfont = (XSSFFont) wb.getFontAt(0);
CTFontImpl ctFont = (CTFontImpl) xfont.getCTFont();
assertEquals(0, ctFont.sizeOfBArray());
}
}
try (FileOutputStream fileOutStream = new FileOutputStream(outFile)) {
wb.write(fileOutStream);
}
try (FileInputStream is = new FileInputStream(outFile)) {
Workbook newWB = null;
try {
if (wb instanceof XSSFWorkbook) {
newWB = new XSSFWorkbook(is);
} else if (wb instanceof HSSFWorkbook) {
newWB = new HSSFWorkbook(is);
} else if (wb instanceof SXSSFWorkbook) {
newWB = new SXSSFWorkbook(new XSSFWorkbook(is));
} else {
throw new IllegalStateException("Unknown workbook: " + wb);
}
assertNotNull(newWB.getSheet("test"));
} finally {
if (newWB != null) {
newWB.close();
}
}
}
}
@Test
public void testBug56688_1() throws IOException {
XSSFWorkbook excel = XSSFTestDataSamples.openSampleWorkbook("56688_1.xlsx");
checkValue(excel, "-1.0"); /* Not 0.0 because POI sees date "0" minus one month as invalid date, which is -1! */
excel.close();
}
@Test
public void testBug56688_2() throws IOException {
XSSFWorkbook excel = XSSFTestDataSamples.openSampleWorkbook("56688_2.xlsx");
checkValue(excel, "#VALUE!");
excel.close();
}
@Test
public void testBug56688_3() throws IOException {
XSSFWorkbook excel = XSSFTestDataSamples.openSampleWorkbook("56688_3.xlsx");
checkValue(excel, "#VALUE!");
excel.close();
}
@Test
public void testBug56688_4() throws IOException {
XSSFWorkbook excel = XSSFTestDataSamples.openSampleWorkbook("56688_4.xlsx");
Calendar calendar = LocaleUtil.getLocaleCalendar();
calendar.add(Calendar.MONTH, 2);
double excelDate = DateUtil.getExcelDate(calendar.getTime());
NumberEval eval = new NumberEval(Math.floor(excelDate));
checkValue(excel, eval.getStringValue() + ".0");
excel.close();
}
/**
* New hyperlink with no initial cell reference, still need
* to be able to change it
*/
@Test
public void testBug56527() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFCreationHelper creationHelper = wb.getCreationHelper();
XSSFHyperlink hyperlink;
// Try with a cell reference
hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
sheet.addHyperlink(hyperlink);
hyperlink.setAddress("http://myurl");
hyperlink.setCellReference("B4");
assertEquals(3, hyperlink.getFirstRow());
assertEquals(1, hyperlink.getFirstColumn());
assertEquals(3, hyperlink.getLastRow());
assertEquals(1, hyperlink.getLastColumn());
// Try with explicit rows / columns
hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
sheet.addHyperlink(hyperlink);
hyperlink.setAddress("http://myurl");
hyperlink.setFirstRow(5);
hyperlink.setFirstColumn(3);
assertEquals(5, hyperlink.getFirstRow());
assertEquals(3, hyperlink.getFirstColumn());
assertEquals(5, hyperlink.getLastRow());
assertEquals(3, hyperlink.getLastColumn());
wb.close();
}
/**
* Shifting rows with a formula that references a
* function in another file
*/
@Test
public void bug56502() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56502.xlsx")) {
Sheet sheet = wb.getSheetAt(0);
Cell cFunc = sheet.getRow(3).getCell(0);
assertEquals("[1]!LUCANET(\"Ist\")", cFunc.getCellFormula());
Cell cRef = sheet.getRow(3).createCell(1);
cRef.setCellFormula("A3");
// Shift it down one row
sheet.shiftRows(1, sheet.getLastRowNum(), 1);
// Check the new formulas: Function won't change, Reference will
cFunc = sheet.getRow(4).getCell(0);
assertEquals("[1]!LUCANET(\"Ist\")", cFunc.getCellFormula());
cRef = sheet.getRow(4).getCell(1);
assertEquals("A4", cRef.getCellFormula());
}
}
@Test
public void bug54764() throws IOException, OpenXML4JException, XmlException {
OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("54764.xlsx");
// Check the core properties - will be found but empty, due
// to the expansion being too much to be considered valid
POIXMLProperties props = new POIXMLProperties(pkg);
assertNull(props.getCoreProperties().getTitle());
assertNull(props.getCoreProperties().getSubject());
assertNull(props.getCoreProperties().getDescription());
// Now check the spreadsheet itself
try {
new XSSFWorkbook(pkg).close();
fail("Should fail as too much expansion occurs");
} catch (POIXMLException e) {
// Expected
}
pkg.close();
// Try with one with the entities in the Content Types
try {
XSSFTestDataSamples.openSamplePackage("54764-2.xlsx").close();
fail("Should fail as too much expansion occurs");
} catch (Exception e) {
// Expected
}
// Check we can still parse valid files after all that
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx")) {
assertEquals(3, wb.getNumberOfSheets());
}
}
@Test
public void test54764WithSAXHelper() throws Exception {
File testFile = XSSFTestDataSamples.getSampleFile("54764.xlsx");
try (ZipFile zip = new ZipFile(testFile)) {
ZipArchiveEntry ze = zip.getEntry("xl/sharedStrings.xml");
XMLReader reader = XMLHelper.newXMLReader();
try {
reader.parse(new InputSource(zip.getInputStream(ze)));
fail("should have thrown SAXParseException");
} catch (SAXParseException e) {
assertNotNull(e.getMessage());
assertTrue(e.getMessage().contains("more than \"1\" entity"));
}
}
}
@Test
public void test54764WithDocumentHelper() throws Exception {
File testFile = XSSFTestDataSamples.getSampleFile("54764.xlsx");
try (ZipFile zip = new ZipFile(testFile)) {
ZipArchiveEntry ze = zip.getEntry("xl/sharedStrings.xml");
try {
DocumentHelper.readDocument(zip.getInputStream(ze));
fail("should have thrown SAXParseException");
} catch (SAXParseException e) {
assertNotNull(e.getMessage());
assertNotEquals(isOldXercesActive(), e.getMessage().contains("DOCTYPE is disallowed when the feature"));
}
}
}
/**
* CTDefinedNamesImpl should be included in the smaller
* poi-ooxml-schemas jar
*/
@Test
public void bug57176() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57176.xlsx")) {
CTDefinedNames definedNames = wb.getCTWorkbook().getDefinedNames();
List<CTDefinedName> definedNameList = definedNames.getDefinedNameList();
for (CTDefinedName defName : definedNameList) {
assertNotNull(defName.getName());
assertNotNull(defName.getStringValue());
}
assertEquals("TestDefinedName", definedNameList.get(0).getName());
}
}
/**
* .xlsb files are not supported, but we should generate a helpful
* error message if given one
*/
@Test
public void bug56800_xlsb() throws IOException {
// Can be opened at the OPC level
OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("Simple.xlsb");
// XSSF Workbook gives helpful error
try {
new XSSFWorkbook(pkg).close();
fail(".xlsb files not supported");
} catch (XLSBUnsupportedException e) {
// Good, detected and warned
}
// Workbook Factory gives helpful error on package
try {
XSSFWorkbookFactory.createWorkbook(pkg).close();
fail(".xlsb files not supported");
} catch (XLSBUnsupportedException e) {
// Good, detected and warned
}
// Workbook Factory gives helpful error on file
File xlsbFile = HSSFTestDataSamples.getSampleFile("Simple.xlsb");
try {
WorkbookFactory.create(xlsbFile).close();
fail(".xlsb files not supported");
} catch (XLSBUnsupportedException e) {
// Good, detected and warned
}
pkg.close();
}
private void checkValue(XSSFWorkbook excel, String expect) {
XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(excel);
evaluator.evaluateAll();
XSSFCell cell = excel.getSheetAt(0).getRow(1).getCell(1);
CellValue value = evaluator.evaluate(cell);
assertEquals(expect, value.formatAsString());
}
@Test
public void testBug57196() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57196.xlsx")) {
Sheet sheet = wb.getSheet("Feuil1");
Row mod = sheet.getRow(1);
mod.getCell(1).setCellValue(3);
HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
// FileOutputStream fileOutput = new FileOutputStream("/tmp/57196.xlsx");
// wb.write(fileOutput);
// fileOutput.close();
}
}
@Test
public void test57196_Detail() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("Sheet1");
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellFormula("DEC2HEX(HEX2DEC(O8)-O2+D2)");
XSSFFormulaEvaluator fe = new XSSFFormulaEvaluator(wb);
CellValue cv = fe.evaluate(cell);
assertNotNull(cv);
wb.close();
}
@Test
public void test57196_Detail2() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("Sheet1");
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellFormula("DEC2HEX(O2+D2)");
XSSFFormulaEvaluator fe = new XSSFFormulaEvaluator(wb);
CellValue cv = fe.evaluate(cell);
assertNotNull(cv);
wb.close();
}
@Test
public void test57196_WorkbookEvaluator() throws IOException {
String previousLogger = System.getProperty("org.apache.poi.util.POILogger");
//System.setProperty("org.apache.poi.util.POILogger", "org.apache.poi.util.SystemOutLogger");
//System.setProperty("poi.log.level", "3");
try {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("Sheet1");
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("0");
cell = row.createCell(1);
cell.setCellValue(0);
cell = row.createCell(2);
cell.setCellValue(0);
// simple formula worked
cell.setCellFormula("DEC2HEX(O2+D2)");
WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
// this already failed! Hex2Dec did not correctly handle RefEval
cell.setCellFormula("HEX2DEC(O8)");
workbookEvaluator.clearAllCachedResultValues();
workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
// slightly more complex one failed
cell.setCellFormula("HEX2DEC(O8)-O2+D2");
workbookEvaluator.clearAllCachedResultValues();
workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
// more complicated failed
cell.setCellFormula("DEC2HEX(HEX2DEC(O8)-O2+D2)");
workbookEvaluator.clearAllCachedResultValues();
workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
// what other similar functions
cell.setCellFormula("DEC2BIN(O8)-O2+D2");
workbookEvaluator.clearAllCachedResultValues();
workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
// what other similar functions
cell.setCellFormula("DEC2BIN(A1)");
workbookEvaluator.clearAllCachedResultValues();
workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
// what other similar functions
cell.setCellFormula("BIN2DEC(B1)");
workbookEvaluator.clearAllCachedResultValues();
workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
wb.close();
} finally {
if (previousLogger == null) {
System.clearProperty("org.apache.poi.util.POILogger");
} else {
System.setProperty("org.apache.poi.util.POILogger", previousLogger);
}
System.clearProperty("poi.log.level");
}
}
/**
* A .xlsx file with no Shared Strings table should open fine
* in read-only mode
*/
@SuppressWarnings("resource")
@Test
public void bug57482() throws IOException, InvalidFormatException {
for (PackageAccess access : new PackageAccess[]{
PackageAccess.READ_WRITE, PackageAccess.READ
}) {
File file = HSSFTestDataSamples.getSampleFile("57482-OnlyNumeric.xlsx");
OPCPackage pkg = OPCPackage.open(file, access);
try {
// Try to open it and read the contents
XSSFWorkbook wb1 = new XSSFWorkbook(pkg);
assertNotNull(wb1.getSharedStringSource());
assertEquals(0, wb1.getSharedStringSource().getCount());
DataFormatter fmt = new DataFormatter();
XSSFSheet s = wb1.getSheetAt(0);
assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0)));
assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0)));
// Add a text cell
s.getRow(0).createCell(3).setCellValue("Testing");
assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3)));
// Try to write-out and read again, should only work
// in read-write mode, not read-only mode
XSSFWorkbook wb2 = null;
try {
wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
if (access == PackageAccess.READ) {
fail("Shouln't be able to write from read-only mode");
}
// Check again
s = wb2.getSheetAt(0);
assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0)));
assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0)));
assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3)));
} catch (InvalidOperationException e) {
if (access == PackageAccess.READ_WRITE) {
// Shouldn't occur in write-mode
throw e;
}
} finally {
if (wb2 != null) {
wb2.getPackage().revert();
}
}
wb1.getPackage().revert();
} finally {
pkg.revert();
}
}
}
/**
* "Unknown error type: -60" fetching formula error value
*/
@Test
public void bug57535() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57535.xlsx")) {
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
evaluator.clearAllCachedResultValues();
Sheet sheet = wb.getSheet("Sheet1");
Cell cell = sheet.getRow(5).getCell(4);
assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals("E4+E5", cell.getCellFormula());
CellValue value = evaluator.evaluate(cell);
assertEquals(CellType.ERROR, value.getCellType());
assertEquals(-60, value.getErrorValue());
assertEquals("~CIRCULAR~REF~", FormulaError.forInt(value.getErrorValue()).getString());
assertEquals("CIRCULAR_REF", FormulaError.forInt(value.getErrorValue()).toString());
}
}
@Test
public void test57165() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx")) {
removeAllSheetsBut(3, wb);
wb.cloneSheet(0); // Throws exception here
wb.setSheetName(1, "New Sheet");
//saveWorkbook(wb, fileName);
XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
wbBack.close();
}
}
@Test
public void test57165_create() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx")) {
removeAllSheetsBut(3, wb);
wb.createSheet("newsheet"); // Throws exception here
wb.setSheetName(1, "New Sheet");
//saveWorkbook(wb, fileName);
XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
wbBack.close();
}
}
private static void removeAllSheetsBut(@SuppressWarnings("SameParameterValue") int sheetIndex, Workbook wb) {
int sheetNb = wb.getNumberOfSheets();
// Move this sheet at the first position
wb.setSheetOrder(wb.getSheetName(sheetIndex), 0);
for (int sn = sheetNb - 1; sn > 0; sn--) {
wb.removeSheetAt(sn);
}
}
/**
* Sums 2 plus the cell at the left, indirectly to avoid reference
* problems when deleting columns, conditionally to stop recursion
*/
private static final String FORMULA1 =
"IF( INDIRECT( ADDRESS( ROW(), COLUMN()-1 ) ) = 0, 0, "
+ "INDIRECT( ADDRESS( ROW(), COLUMN()-1 ) ) ) + 2";
/**
* Sums 2 plus the upper cell, indirectly to avoid reference
* problems when deleting rows, conditionally to stop recursion
*/
private static final String FORMULA2 =
"IF( INDIRECT( ADDRESS( ROW()-1, COLUMN() ) ) = 0, 0, "
+ "INDIRECT( ADDRESS( ROW()-1, COLUMN() ) ) ) + 2";
/**
* Expected:
* <p>
* [ 0][ 2][ 4]
*/
@Test
public void testBug56820_Formula1() throws IOException {
try (Workbook wb = new XSSFWorkbook()) {
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
Sheet sh = wb.createSheet();
sh.createRow(0).createCell(0).setCellValue(0.0d);
Cell formulaCell1 = sh.getRow(0).createCell(1);
Cell formulaCell2 = sh.getRow(0).createCell(2);
formulaCell1.setCellFormula(FORMULA1);
formulaCell2.setCellFormula(FORMULA1);
double A1 = evaluator.evaluate(formulaCell1).getNumberValue();
double A2 = evaluator.evaluate(formulaCell2).getNumberValue();
assertEquals(2, A1, 0);
assertEquals(4, A2, 0); //<-- FAILS EXPECTATIONS
}
}
/**
* Expected:
* <p>
* [ 0] <- number
* [ 2] <- formula
* [ 4] <- formula
*/
@Test
public void testBug56820_Formula2() throws IOException {
try (Workbook wb = new XSSFWorkbook()) {
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
Sheet sh = wb.createSheet();
sh.createRow(0).createCell(0).setCellValue(0.0d);
Cell formulaCell1 = sh.createRow(1).createCell(0);
Cell formulaCell2 = sh.createRow(2).createCell(0);
formulaCell1.setCellFormula(FORMULA2);
formulaCell2.setCellFormula(FORMULA2);
double A1 = evaluator.evaluate(formulaCell1).getNumberValue();
double A2 = evaluator.evaluate(formulaCell2).getNumberValue(); //<-- FAILS EVALUATION
assertEquals(2, A1, 0);
assertEquals(4, A2, 0);
}
}
@Test
public void test56467() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("picture.xlsx")) {
Sheet orig = wb.getSheetAt(0);
assertNotNull(orig);
Sheet sheet = wb.cloneSheet(0);
Drawing<?> drawing = sheet.createDrawingPatriarch();
for (XSSFShape shape : ((XSSFDrawing) drawing).getShapes()) {
if (shape instanceof XSSFPicture) {
XSSFPictureData pictureData = ((XSSFPicture) shape).getPictureData();
assertNotNull(pictureData);
}
}
}
}
/**
* OOXML-Strict files
* Not currently working - namespace mis-match from XMLBeans
*/
@Test
@Ignore("XMLBeans namespace mis-match on ooxml-strict files")
public void test57699() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.strict.xlsx")) {
assertEquals(3, wb.getNumberOfSheets());
// TODO Check sheet contents
// TODO Check formula evaluation
try (XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb)) {
assertEquals(3, wbBack.getNumberOfSheets());
// TODO Re-check sheet contents
// TODO Re-check formula evaluation
}
}
}
@Test
public void testBug56295_MergeXlslsWithStyles() throws IOException {
XSSFWorkbook xlsToAppendWorkbook = XSSFTestDataSamples.openSampleWorkbook("56295.xlsx");
XSSFSheet sheet = xlsToAppendWorkbook.getSheetAt(0);
XSSFRow srcRow = sheet.getRow(0);
XSSFCell oldCell = srcRow.getCell(0);
XSSFCellStyle cellStyle = oldCell.getCellStyle();
checkStyle(cellStyle);
// StylesTable table = xlsToAppendWorkbook.getStylesSource();
// List<XSSFCellFill> fills = table.getFills();
// System.out.println("Having " + fills.size() + " fills");
// for(XSSFCellFill fill : fills) {
// System.out.println("Fill: " + fill.getFillBackgroundColor() + "/" + fill.getFillForegroundColor());
// }
xlsToAppendWorkbook.close();
XSSFWorkbook targetWorkbook = new XSSFWorkbook();
XSSFSheet newSheet = targetWorkbook.createSheet(sheet.getSheetName());
XSSFRow destRow = newSheet.createRow(0);
XSSFCell newCell = destRow.createCell(0);
//newCell.getCellStyle().cloneStyleFrom(cellStyle);
CellStyle newCellStyle = targetWorkbook.createCellStyle();
newCellStyle.cloneStyleFrom(cellStyle);
newCell.setCellStyle(newCellStyle);
checkStyle(newCell.getCellStyle());
newCell.setCellValue(oldCell.getStringCellValue());
// OutputStream os = new FileOutputStream("output.xlsm");
// try {
// targetWorkbook.write(os);
// } finally {
// os.close();
// }
XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(targetWorkbook);
XSSFCellStyle styleBack = wbBack.getSheetAt(0).getRow(0).getCell(0).getCellStyle();
checkStyle(styleBack);
targetWorkbook.close();
wbBack.close();
}
/**
* Paragraph with property BuFont but none of the properties
* BuNone, BuChar, and BuAutoNum, used to trigger a NPE
* Excel treats this as not-bulleted, so now do we
*/
@Test
public void testBug57826() throws IOException {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("57826.xlsx");
assertTrue("no sheets in workbook", workbook.getNumberOfSheets() >= 1);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFDrawing drawing = sheet.getDrawingPatriarch();
assertNotNull(drawing);
List<XSSFShape> shapes = drawing.getShapes();
assertEquals(1, shapes.size());
assertTrue(shapes.get(0) instanceof XSSFSimpleShape);
XSSFSimpleShape shape = (XSSFSimpleShape) shapes.get(0);
// Used to throw a NPE
String text = shape.getText();
// No bulleting info included
assertEquals("test ok", text);
workbook.close();
}
private void checkStyle(XSSFCellStyle cellStyle) {
assertNotNull(cellStyle);
assertEquals(0, cellStyle.getFillForegroundColor());
assertNotNull(cellStyle.getFillForegroundXSSFColor());
XSSFColor fgColor = cellStyle.getFillForegroundColorColor();
assertNotNull(fgColor);
assertEquals("FF00FFFF", fgColor.getARGBHex());
assertEquals(0, cellStyle.getFillBackgroundColor());
assertNotNull(cellStyle.getFillBackgroundXSSFColor());
XSSFColor bgColor = cellStyle.getFillBackgroundColorColor();
assertNotNull(bgColor);
assertEquals("FF00FFFF", fgColor.getARGBHex());
}
@Test
public void bug57642() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet s = wb.createSheet("TestSheet");
XSSFCell c = s.createRow(0).createCell(0);
c.setCellFormula("ISERROR(TestSheet!A1)");
c = s.createRow(1).createCell(1);
c.setCellFormula("ISERROR(B2)");
wb.setSheetName(0, "CSN");
c = s.getRow(0).getCell(0);
assertEquals("ISERROR(CSN!A1)", c.getCellFormula());
c = s.getRow(1).getCell(1);
assertEquals("ISERROR(B2)", c.getCellFormula());
wb.close();
}
/**
* .xlsx supports 64000 cell styles, the style indexes after
* 32,767 must not be -32,768, then -32,767, -32,766
*/
@Test
public void bug57880() throws IOException {
int numStyles = 33000;
XSSFWorkbook wb = new XSSFWorkbook();
for (int i = 1; i < numStyles; i++) {
// Create a style and use it
XSSFCellStyle style = wb.createCellStyle();
assertEquals(i, style.getUIndex());
}
assertEquals(numStyles, wb.getNumCellStyles());
// avoid OOM in Gump run
File file = XSSFTestDataSamples.writeOutAndClose(wb, "bug57880");
//noinspection UnusedAssignment
wb = null;
// Garbage collection may happen here
// avoid zip bomb detection
double ratio = ZipSecureFile.getMinInflateRatio();
ZipSecureFile.setMinInflateRatio(0.00005);
wb = XSSFTestDataSamples.readBackAndDelete(file);
ZipSecureFile.setMinInflateRatio(ratio);
//Assume identical cell styles aren't consolidated
//If XSSFWorkbooks ever implicitly optimize/consolidate cell styles (such as when the workbook is written to disk)
//then this unit test should be updated
assertEquals(numStyles, wb.getNumCellStyles());
for (int i = 1; i < numStyles; i++) {
XSSFCellStyle style = wb.getCellStyleAt(i);
assertNotNull(style);
assertEquals(i, style.getUIndex());
}
wb.close();
}
@Test
public void test56574() throws IOException {
runTest56574(false);
runTest56574(true);
}
private void runTest56574(boolean createRow) throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56574.xlsx")) {
Sheet sheet = wb.getSheet("Func");
assertNotNull(sheet);
Map<String, Object[]> data;
data = new TreeMap<>();
data.put("1", new Object[]{"ID", "NAME", "LASTNAME"});
data.put("2", new Object[]{2, "Amit", "Shukla"});
data.put("3", new Object[]{1, "Lokesh", "Gupta"});
data.put("4", new Object[]{4, "John", "Adwards"});
data.put("5", new Object[]{2, "Brian", "Schultz"});
int rownum = 1;
for (Map.Entry<String, Object[]> me : data.entrySet()) {
final Row row;
if (createRow) {
row = sheet.createRow(rownum++);
} else {
row = sheet.getRow(rownum++);
}
assertNotNull(row);
int cellnum = 0;
for (Object obj : me.getValue()) {
Cell cell = row.getCell(cellnum);
if (cell == null) {
cell = row.createCell(cellnum);
} else {
if (cell.getCellType() == CellType.FORMULA) {
cell.setCellFormula(null);
cell.getCellStyle().setDataFormat((short) 0);
}
}
if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Integer) {
cell.setCellValue((Integer) obj);
}
cellnum++;
}
}
XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
CalculationChain chain = wb.getCalculationChain();
checkCellsAreGone(chain);
XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
Sheet sheetBack = wbBack.getSheet("Func");
assertNotNull(sheetBack);
chain = wbBack.getCalculationChain();
checkCellsAreGone(chain);
wbBack.close();
}
}
private void checkCellsAreGone(CalculationChain chain) {
for (CTCalcCell calc : chain.getCTCalcChain().getCList()) {
// A2 to A6 should be gone
assertNotEquals("A2", calc.getR());
assertNotEquals("A3", calc.getR());
assertNotEquals("A4", calc.getR());
assertNotEquals("A5", calc.getR());
assertNotEquals("A6", calc.getR());
}
}
/**
* Excel 2007 generated Macro-Enabled .xlsm file
*/
@Test
public void bug57181() throws IOException {
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57181.xlsm")) {
assertEquals(9, wb.getNumberOfSheets());
}
}
@Test
public void bug52111() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("Intersection-52111-xssf.xlsx")) {
Sheet s = wb.getSheetAt(0);
assertFormula(wb, s.getRow(2).getCell(0), "(C2:D3 D3:E4)", "4.0");
assertFormula(wb, s.getRow(6).getCell(0), "Tabelle2!E:E Tabelle2!11:11", "5.0");
assertFormula(wb, s.getRow(8).getCell(0), "Tabelle2!E:F Tabelle2!11:12", null);
}
}
@Test
public void test48962() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("48962.xlsx")) {
Sheet sh = wb.getSheetAt(0);
Row row = sh.getRow(1);
Cell cell = row.getCell(0);
CellStyle style = cell.getCellStyle();
assertNotNull(style);
// color index
assertEquals(64, style.getFillBackgroundColor());
XSSFColor color = ((XSSFCellStyle) style).getFillBackgroundXSSFColor();
assertNotNull(color);
// indexed color
assertEquals(64, color.getIndexed());
assertEquals(64, color.getIndex());
// not an RGB color
assertFalse(color.isRGB());
assertNull(color.getRGB());
}
}
@Test
public void test50755_workday_formula_example() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("50755_workday_formula_example.xlsx")) {
Sheet sheet = wb.getSheet("Sheet1");
for (Row aRow : sheet) {
Cell cell = aRow.getCell(1);
if (cell.getCellType() == CellType.FORMULA) {
String formula = cell.getCellFormula();
assertNotNull(formula);
assertTrue(formula.contains("WORKDAY"));
} else {
assertNotNull(cell.toString());
}
}
}
}
@Test
public void test51626() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("51626.xlsx");
assertNotNull(wb);
wb.close();
InputStream stream = HSSFTestDataSamples.openSampleFileStream("51626.xlsx");
wb = WorkbookFactory.create(stream);
stream.close();
wb.close();
wb = XSSFTestDataSamples.openSampleWorkbook("51626_contact.xlsx");
assertNotNull(wb);
wb.close();
stream = HSSFTestDataSamples.openSampleFileStream("51626_contact.xlsx");
wb = WorkbookFactory.create(stream);
stream.close();
wb.close();
}
@Test
public void test51451() throws IOException {
Workbook wb = new XSSFWorkbook();
Sheet sh = wb.createSheet();
Row row = sh.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(239827342);
CellStyle style = wb.createCellStyle();
//style.setHidden(false);
DataFormat excelFormat = wb.createDataFormat();
style.setDataFormat(excelFormat.getFormat("#,##0"));
sh.setDefaultColumnStyle(0, style);
// FileOutputStream out = new FileOutputStream("/tmp/51451.xlsx");
// wb.write(out);
// out.close();
wb.close();
}
@Test
public void test53105() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53105.xlsx")) {
assertNotNull(wb);
// Act
// evaluate SUM('Skye Lookup Input'!A4:XFD4), cells in range each contain "1"
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
double numericValue = evaluator.evaluate(wb.getSheetAt(0).getRow(1).getCell(0)).getNumberValue();
// Assert
assertEquals(16384.0, numericValue, 0.0);
}
}
@Test
public void test58315() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("58315.xlsx")) {
Cell cell = wb.getSheetAt(0).getRow(0).getCell(0);
assertNotNull(cell);
StringBuilder tmpCellContent = new StringBuilder(cell.getStringCellValue());
XSSFRichTextString richText = (XSSFRichTextString) cell.getRichStringCellValue();
for (int i = richText.length() - 1; i >= 0; i--) {
Font f = richText.getFontAtIndex(i);
if (f != null && f.getStrikeout()) {
tmpCellContent.deleteCharAt(i);
}
}
String result = tmpCellContent.toString();
assertEquals("320 350", result);
}
}
@Test
public void test55406() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("55406_Conditional_formatting_sample.xlsx")) {
Sheet sheet = wb.getSheetAt(0);
Cell cellA1 = sheet.getRow(0).getCell(0);
Cell cellA2 = sheet.getRow(1).getCell(0);
assertEquals(0, cellA1.getCellStyle().getFillForegroundColor());
assertEquals("FFFDFDFD", ((XSSFColor) cellA1.getCellStyle().getFillForegroundColorColor()).getARGBHex());
assertEquals(0, cellA2.getCellStyle().getFillForegroundColor());
assertEquals("FFFDFDFD", ((XSSFColor) cellA2.getCellStyle().getFillForegroundColorColor()).getARGBHex());
SheetConditionalFormatting cond = sheet.getSheetConditionalFormatting();
assertEquals(2, cond.getNumConditionalFormattings());
assertEquals(1, cond.getConditionalFormattingAt(0).getNumberOfRules());
assertEquals(64, cond.getConditionalFormattingAt(0).getRule(0).getPatternFormatting().getFillForegroundColor());
assertEquals("ISEVEN(ROW())", cond.getConditionalFormattingAt(0).getRule(0).getFormula1());
assertNull(((XSSFColor) cond.getConditionalFormattingAt(0).getRule(0).getPatternFormatting().getFillForegroundColorColor()).getARGBHex());
assertEquals(1, cond.getConditionalFormattingAt(1).getNumberOfRules());
assertEquals(64, cond.getConditionalFormattingAt(1).getRule(0).getPatternFormatting().getFillForegroundColor());
assertEquals("ISEVEN(ROW())", cond.getConditionalFormattingAt(1).getRule(0).getFormula1());
assertNull(((XSSFColor) cond.getConditionalFormattingAt(1).getRule(0).getPatternFormatting().getFillForegroundColorColor()).getARGBHex());
}
}
@Test
public void test51998() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("51998.xlsx");
Set<String> sheetNames = new HashSet<>();
for (int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
sheetNames.add(wb.getSheetName(sheetNum));
}
for (String sheetName : sheetNames) {
int sheetIndex = wb.getSheetIndex(sheetName);
wb.removeSheetAt(sheetIndex);
Sheet newSheet = wb.createSheet();
//Sheet newSheet = wb.createSheet(sheetName);
int newSheetIndex = wb.getSheetIndex(newSheet);
wb.setSheetName(newSheetIndex, sheetName);
wb.setSheetOrder(sheetName, sheetIndex);
}
Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
wb.close();
assertNotNull(wbBack);
wbBack.close();
}
@Test
public void test58731() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("58731.xlsx")) {
Sheet sheet = wb.createSheet("Java Books");
Object[][] bookData = {
{"Head First Java", "Kathy Serria", 79},
{"Effective Java", "Joshua Bloch", 36},
{"Clean Code", "Robert martin", 42},
{"Thinking in Java", "Bruce Eckel", 35},
};
int rowCount = 0;
for (Object[] aBook : bookData) {
Row row = sheet.createRow(rowCount++);
int columnCount = 0;
for (Object field : aBook) {
Cell cell = row.createCell(columnCount++);
if (field instanceof String) {
cell.setCellValue((String) field);
} else if (field instanceof Integer) {
cell.setCellValue((Integer) field);
}
}
}
try (Workbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb)) {
sheet = wb2.getSheet("Java Books");
assertNotNull(sheet.getRow(0));
assertNotNull(sheet.getRow(0).getCell(0));
assertEquals(bookData[0][0], sheet.getRow(0).getCell(0).getStringCellValue());
}
}
}
/**
* Regression between 3.10.1 and 3.13 -
* org.apache.poi.openxml4j.exceptions.InvalidFormatException:
* The part /xl/sharedStrings.xml does not have any content type
* ! Rule: Package require content types when retrieving a part from a package. [M.1.14]
*/
@Test
public void test58760() throws IOException {
Workbook wb1 = XSSFTestDataSamples.openSampleWorkbook("58760.xlsx");
assertEquals(1, wb1.getNumberOfSheets());
assertEquals("Sheet1", wb1.getSheetName(0));
Workbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
assertEquals(1, wb2.getNumberOfSheets());
assertEquals("Sheet1", wb2.getSheetName(0));
wb2.close();
wb1.close();
}
@Test
public void test57236() throws IOException {
// Having very small numbers leads to different formatting, Excel uses the scientific notation, but POI leads to "0"
/*
DecimalFormat format = new DecimalFormat("#.##########", new DecimalFormatSymbols(Locale.getDefault()));
double d = 3.0E-104;
assertEquals("3.0E-104", format.format(d));
*/
DataFormatter formatter = new DataFormatter(true);
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57236.xlsx")) {
for (int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
Sheet sheet = wb.getSheetAt(sheetNum);
for (int rowNum = sheet.getFirstRowNum(); rowNum < sheet.getLastRowNum(); rowNum++) {
Row row = sheet.getRow(rowNum);
for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
Cell cell = row.getCell(cellNum);
String fmtCellValue = formatter.formatCellValue(cell);
assertNotNull(fmtCellValue);
assertNotEquals("0", fmtCellValue);
}
}
}
}
}
/**
* helper function for {@link #test58043()}
* Side-effects: closes the provided workbook!
*
* @param workbook the workbook to save for manual checking
* @param outputFile the output file location to save the workbook to
*/
private void saveRotatedTextExample(Workbook workbook, File outputFile) throws IOException {
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow((short) 0);
Cell cell = row.createCell(0);
cell.setCellValue("Unsuccessful rotated text.");
CellStyle style = workbook.createCellStyle();
style.setRotation((short) -90);
cell.setCellStyle(style);
OutputStream fos = new FileOutputStream(outputFile);
workbook.write(fos);
fos.close();
workbook.close();
}
@Ignore("Creates files for checking results manually, actual values are tested in Test*CellStyle")
@Test
public void test58043() throws IOException {
saveRotatedTextExample(new HSSFWorkbook(), TempFile.createTempFile("rotated", ".xls"));
saveRotatedTextExample(new XSSFWorkbook(), TempFile.createTempFile("rotated", ".xlsx"));
}
@Test
public void test59132() throws IOException {
Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("59132.xlsx");
Sheet worksheet = workbook.getSheet("sheet1");
// B3
Row row = worksheet.getRow(2);
Cell cell = row.getCell(1);
cell.setCellValue((String) null);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
// B3
row = worksheet.getRow(2);
cell = row.getCell(1);
assertEquals(CellType.BLANK, cell.getCellType());
assertEquals(CellType._NONE, evaluator.evaluateFormulaCell(cell));
// A3
row = worksheet.getRow(2);
cell = row.getCell(0);
assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals("IF(ISBLANK(B3),\"\",B3)", cell.getCellFormula());
assertEquals(CellType.STRING, evaluator.evaluateFormulaCell(cell));
CellValue value = evaluator.evaluate(cell);
assertEquals("", value.getStringValue());
// A5
row = worksheet.getRow(4);
cell = row.getCell(0);
assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals("COUNTBLANK(A1:A4)", cell.getCellFormula());
assertEquals(CellType.NUMERIC, evaluator.evaluateFormulaCell(cell));
value = evaluator.evaluate(cell);
assertEquals(1.0, value.getNumberValue(), 0.1);
/*FileOutputStream output = new FileOutputStream("C:\\temp\\59132.xlsx");
try {
workbook.write(output);
} finally {
output.close();
}*/
workbook.close();
}
@Ignore("bug 59442")
@Test
public void testSetRGBBackgroundColor() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFCell cell = workbook.createSheet().createRow(0).createCell(0);
XSSFColor color = new XSSFColor(java.awt.Color.RED, workbook.getStylesSource().getIndexedColors());
XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(color);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(style);
// Everything is fine at this point, cell is red
XSSFColor actual = cell.getCellStyle().getFillBackgroundColorColor();
assertNull(actual);
actual = cell.getCellStyle().getFillForegroundColorColor();
assertNotNull(actual);
assertEquals(color.getARGBHex(), actual.getARGBHex());
Map<String, Object> properties = new HashMap<>();
properties.put(CellUtil.BORDER_BOTTOM, BorderStyle.THIN);
CellUtil.setCellStyleProperties(cell, properties);
// Now the cell is all black
actual = cell.getCellStyle().getFillBackgroundColorColor();
assertNotNull(actual);
assertNull(actual.getARGBHex());
actual = cell.getCellStyle().getFillForegroundColorColor();
assertNotNull(actual);
assertEquals(color.getARGBHex(), actual.getARGBHex());
XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(workbook);
workbook.close();
XSSFCell ncell = nwb.getSheetAt(0).getRow(0).getCell(0);
XSSFColor ncolor = new XSSFColor(java.awt.Color.RED, workbook.getStylesSource().getIndexedColors());
// Now the cell is all black
XSSFColor nactual = ncell.getCellStyle().getFillBackgroundColorColor();
assertNotNull(nactual);
assertEquals(ncolor.getARGBHex(), nactual.getARGBHex());
nwb.close();
}
@Ignore("currently fails on POI 3.15 beta 2")
@Test
public void test55273() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("ExcelTables.xlsx")) {
Sheet sheet = wb.getSheet("ExcelTable");
Name name = wb.getName("TableAsRangeName");
assertEquals("TableName[#All]", name.getRefersToFormula());
// POI 3.15-beta 2 (2016-06-15): getSheetName throws IllegalArgumentException: Invalid CellReference: TableName[#All]
assertEquals("TableName", name.getSheetName());
XSSFSheet xsheet = (XSSFSheet) sheet;
List<XSSFTable> tables = xsheet.getTables();
assertEquals(2, tables.size()); //FIXME: how many tables are there in this spreadsheet?
assertEquals("Table1", tables.get(0).getName()); //FIXME: what is the table name?
assertEquals("Table2", tables.get(1).getName()); //FIXME: what is the table name?
}
}
@Test
public void test57523() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57523.xlsx")) {
Sheet sheet = wb.getSheet("Attribute Master");
Row row = sheet.getRow(15);
int N = CellReference.convertColStringToIndex("N");
Cell N16 = row.getCell(N);
assertEquals(500.0, N16.getNumericCellValue(), 0.00001);
int P = CellReference.convertColStringToIndex("P");
Cell P16 = row.getCell(P);
assertEquals(10.0, P16.getNumericCellValue(), 0.00001);
}
}
/**
* Files produced by some scientific equipment neglect
* to include the row number on the row tags
*/
@Test
public void noRowNumbers59746() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("59746_NoRowNums.xlsx")) {
Sheet sheet = wb.getSheetAt(0);
assertTrue("Last row num: " + sheet.getLastRowNum(), sheet.getLastRowNum() > 20);
assertEquals("Checked", sheet.getRow(0).getCell(0).getStringCellValue());
assertEquals("Checked", sheet.getRow(9).getCell(2).getStringCellValue());
assertFalse(sheet.getRow(70).getCell(8).getBooleanCellValue());
assertEquals(71, sheet.getPhysicalNumberOfRows());
assertEquals(70, sheet.getLastRowNum());
assertEquals(70, sheet.getRow(sheet.getLastRowNum()).getRowNum());
}
}
@Test
public void testWorkdayFunction() throws IOException {
try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("59106.xlsx")) {
XSSFSheet sheet = workbook.getSheet("Test");
Row row = sheet.getRow(1);
Cell cell = row.getCell(0);
DataFormatter form = new DataFormatter();
FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
String result = form.formatCellValue(cell, evaluator);
assertEquals("09 Mar 2016", result);
}
}
// This bug is currently open. When this bug is fixed, it should not throw an AssertionError
@Test(expected = AssertionError.class)
public void test55076_collapseColumnGroups() throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
// this column collapsing bug only occurs when the grouped columns are different widths
sheet.setColumnWidth(1, 400);
sheet.setColumnWidth(2, 600);
sheet.setColumnWidth(3, 800);
assertEquals(400, sheet.getColumnWidth(1));
assertEquals(600, sheet.getColumnWidth(2));
assertEquals(800, sheet.getColumnWidth(3));
sheet.groupColumn(1, 3);
sheet.setColumnGroupCollapsed(1, true);
assertEquals(0, sheet.getColumnOutlineLevel(0));
assertEquals(1, sheet.getColumnOutlineLevel(1));
assertEquals(1, sheet.getColumnOutlineLevel(2));
assertEquals(1, sheet.getColumnOutlineLevel(3));
assertEquals(0, sheet.getColumnOutlineLevel(4));
// none of the columns should be hidden
// column group collapsing is a different concept
for (int c = 0; c < 5; c++) {
assertFalse("Column " + c, sheet.isColumnHidden(c));
}
assertEquals(400, sheet.getColumnWidth(1));
assertEquals(600, sheet.getColumnWidth(2));
assertEquals(800, sheet.getColumnWidth(3));
wb.close();
}
/**
* Other things, including charts, may end up taking drawing part
* numbers. (Uses a test file hand-crafted with an extra non-drawing
* part with a part number)
*/
@Test
public void drawingNumbersAlreadyTaken_60255() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("60255_extra_drawingparts.xlsx")) {
assertEquals(4, wb.getNumberOfSheets());
// Sheet 3 starts with a drawing
Sheet sheet = wb.getSheetAt(0);
assertNull(sheet.getDrawingPatriarch());
sheet = wb.getSheetAt(1);
assertNull(sheet.getDrawingPatriarch());
sheet = wb.getSheetAt(2);
assertNotNull(sheet.getDrawingPatriarch());
sheet = wb.getSheetAt(3);
assertNull(sheet.getDrawingPatriarch());
// Add another sheet, and give it a drawing
sheet = wb.createSheet();
assertNull(sheet.getDrawingPatriarch());
sheet.createDrawingPatriarch();
assertNotNull(sheet.getDrawingPatriarch());
// Save and check
Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
assertEquals(5, wbBack.getNumberOfSheets());
// Sheets 3 and 5 now
sheet = wbBack.getSheetAt(0);
assertNull(sheet.getDrawingPatriarch());
sheet = wbBack.getSheetAt(1);
assertNull(sheet.getDrawingPatriarch());
sheet = wbBack.getSheetAt(2);
assertNotNull(sheet.getDrawingPatriarch());
sheet = wbBack.getSheetAt(3);
assertNull(sheet.getDrawingPatriarch());
sheet = wbBack.getSheetAt(4);
assertNotNull(sheet.getDrawingPatriarch());
}
}
@Test
public void test53611() throws IOException {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("test");
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellValue("blabla");
//0 1 2 3 4 5 6 7
//A B C D E F G H
row = sheet.createRow(4);
cell = row.createCell(7);
cell.setCellValue("blabla");
// we currently only populate the dimension during writing out
// to avoid having to iterate all rows/cells in each add/remove of a row or cell
wb.write(new NullOutputStream());
assertEquals("B2:H5", ((XSSFSheet) sheet).getCTWorksheet().getDimension().getRef());
wb.close();
}
@Test
public void test61798() throws IOException {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("test");
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellValue("blabla");
row = sheet.createRow(4);
// Allowable column range for EXCEL2007 is (0..16383) or ('A'..'XDF')
cell = row.createCell(16383);
cell.setCellValue("blabla");
// we currently only populate the dimension during writing out
// to avoid having to iterate all rows/cells in each add/remove of a row or cell
wb.write(new NullOutputStream());
assertEquals("B2:XFD5", ((XSSFSheet)sheet).getCTWorksheet().getDimension().getRef());
wb.close();
}
@Test
public void bug61063() throws Exception {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("61063.xlsx")) {
FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
Sheet s = wb.getSheetAt(0);
Row r = s.getRow(3);
Cell c = r.getCell(0);
assertEquals(CellType.FORMULA, c.getCellType());
eval.setDebugEvaluationOutputForNextEval(true);
CellValue cv = eval.evaluate(c);
assertNotNull(cv);
assertEquals("Had: " + cv, 2.0, cv.getNumberValue(), 0.00001);
}
}
@Test
public void bug61516() throws IOException {
final String initialFormula = "A1";
final String expectedFormula = "#REF!"; // from ms excel
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("sheet1");
sheet.createRow(0).createCell(0).setCellValue(1); // A1 = 1
{
Cell c3 = sheet.createRow(2).createCell(2);
c3.setCellFormula(initialFormula); // C3 = =A1
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
CellValue cellValue = evaluator.evaluate(c3);
assertEquals(1, cellValue.getNumberValue(), 0.0001);
}
{
FormulaShifter formulaShifter = FormulaShifter.createForRowCopy(0, "sheet1", 2/*firstRowToShift*/, 2/*lastRowToShift*/
, -1/*step*/, SpreadsheetVersion.EXCEL2007); // parameters 2, 2, -1 should mean : move row range [2-2] one level up
XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
Ptg[] ptgs = FormulaParser.parse(initialFormula, fpb, FormulaType.CELL, 0); // [A1]
formulaShifter.adjustFormula(ptgs, 0); // adjusted to [A]
String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs); //A
//System.out.println(String.format("initial formula : A1; expected formula value after shifting up : #REF!; actual formula value : %s", shiftedFmla));
assertEquals("On copy we expect the formula to be adjusted, in this case it would point to row -1, which is an invalid REF",
expectedFormula, shiftedFmla);
}
{
FormulaShifter formulaShifter = FormulaShifter.createForRowShift(0, "sheet1", 2/*firstRowToShift*/, 2/*lastRowToShift*/
, -1/*step*/, SpreadsheetVersion.EXCEL2007); // parameters 2, 2, -1 should mean : move row range [2-2] one level up
XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
Ptg[] ptgs = FormulaParser.parse(initialFormula, fpb, FormulaType.CELL, 0); // [A1]
formulaShifter.adjustFormula(ptgs, 0); // adjusted to [A]
String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs); //A
//System.out.println(String.format("initial formula : A1; expected formula value after shifting up : #REF!; actual formula value : %s", shiftedFmla));
assertEquals("On move we expect the formula to stay the same, thus expecting the initial formula A1 here",
initialFormula, shiftedFmla);
}
sheet.shiftRows(2, 2, -1);
{
Cell c2 = sheet.getRow(1).getCell(2);
assertNotNull("cell C2 needs to exist now", c2);
assertEquals(CellType.FORMULA, c2.getCellType());
assertEquals(initialFormula, c2.getCellFormula());
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
CellValue cellValue = evaluator.evaluate(c2);
assertEquals(1, cellValue.getNumberValue(), 0.0001);
}
wb.close();
}
@Test
public void test61652() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("61652.xlsx")) {
Sheet sheet = wb.getSheet("IRPPCalc");
Row row = sheet.getRow(11);
Cell cell = row.getCell(18);
WorkbookEvaluatorProvider fe = (WorkbookEvaluatorProvider) wb.getCreationHelper().createFormulaEvaluator();
ConditionalFormattingEvaluator condfmt = new ConditionalFormattingEvaluator(wb, fe);
assertEquals("Conditional formatting is not triggered for this cell",
"[]", condfmt.getConditionalFormattingForCell(cell).toString());
// but we can read the conditional formatting itself
List<EvaluationConditionalFormatRule> rules = condfmt.getFormatRulesForSheet(sheet);
assertEquals(1, rules.size());
assertEquals("AND($A1>=EDATE($D$6,3),$B1>0)", rules.get(0).getFormula1());
}
}
@Test
public void test61543() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
XSSFTable table1 = sheet.createTable(null);
XSSFTable table2 = sheet.createTable(null);
XSSFTable table3 = sheet.createTable(null);
sheet.removeTable(table1);
sheet.createTable(null);
sheet.removeTable(table2);
sheet.removeTable(table3);
sheet.createTable(null);
}
}
/**
* Auto column sizing failed when there were loads of fonts with
* errors like ArrayIndexOutOfBoundsException: -32765
*/
@Test
public void test62108() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
// Create lots of fonts
XSSFDataFormat formats = wb.createDataFormat();
XSSFFont[] fonts = new XSSFFont[50000];
for (int i = 0; i < fonts.length; i++) {
XSSFFont font = wb.createFont();
font.setFontHeight(i);
fonts[i] = font;
}
// Create a moderate number of columns, which use
// fonts from the start and end of the font list
final int numCols = 125;
for (int i = 0; i < numCols; i++) {
XSSFCellStyle cs = wb.createCellStyle();
cs.setDataFormat(formats.getFormat("'Test " + i + "' #,###"));
XSSFFont font = fonts[i];
if (i % 2 == 1) {
font = fonts[fonts.length - i];
}
cs.setFont(font);
XSSFCell c = row.createCell(i);
c.setCellValue(i);
c.setCellStyle(cs);
}
// Do the auto-size
for (int i = 0; i < numCols; i++) {
sheet.autoSizeColumn(i);
}
}
}
@Test
public void test61905xlsx() throws IOException {
try (Workbook wb = new XSSFWorkbook()) {
checkActiveSheet(wb, XSSFITestDataProvider.instance);
}
}
@Test
public void test61905xls() throws IOException {
try (Workbook wb = new HSSFWorkbook()) {
checkActiveSheet(wb, HSSFITestDataProvider.instance);
}
}
private void checkActiveSheet(Workbook wb, ITestDataProvider instance) throws IOException {
Sheet sheet = wb.createSheet("new sheet");
sheet.setActiveCell(new CellAddress("E11"));
assertEquals("E11", sheet.getActiveCell().formatAsString());
Workbook wbBack = instance.writeOutAndReadBack(wb);
sheet = wbBack.getSheetAt(0);
assertEquals("E11", sheet.getActiveCell().formatAsString());
wbBack.close();
}
@Test
public void testBug54084Unicode() throws IOException {
// sample XLSX with the same text-contents as the text-file above
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("54084 - Greek - beyond BMP.xlsx")) {
verifyBug54084Unicode(wb);
//XSSFTestDataSamples.writeOut(wb, "bug 54084 for manual review");
// now write the file and read it back in
XSSFWorkbook wbWritten = XSSFTestDataSamples.writeOutAndReadBack(wb);
verifyBug54084Unicode(wbWritten);
// finally also write it out via the streaming interface and verify that we still can read it back in
SXSSFWorkbook swb = new SXSSFWorkbook(wb);
Workbook wbStreamingWritten = SXSSFITestDataProvider.instance.writeOutAndReadBack(swb);
verifyBug54084Unicode(wbStreamingWritten);
wbWritten.close();
swb.close();
wbStreamingWritten.close();
}
}
private void verifyBug54084Unicode(Workbook wb) {
// expected data is stored in UTF-8 in a text-file
byte[] data = HSSFTestDataSamples.getTestDataFileContent("54084 - Greek - beyond BMP.txt");
String testData = new String(data, StandardCharsets.UTF_8).trim();
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
String value = cell.getStringCellValue();
//System.out.println(value);
assertEquals("The data in the text-file should exactly match the data that we read from the workbook", testData, value);
}
@Test
public void bug63371() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
CellRangeAddress region = new CellRangeAddress(1, 1, 1, 2);
assertEquals(0, sheet.addMergedRegion(region));
//System.out.println(String.format("%s: index=%d", "testAddMergedRegion", index));
final List<CellRangeAddress> ranges = sheet.getMergedRegions();
final int numMergedRegions = sheet.getNumMergedRegions();
final CTWorksheet ctSheet = sheet.getCTWorksheet();
final CTMergeCells ctMergeCells = ctSheet.getMergeCells();
final List<CTMergeCell> ctMergeCellList = ctMergeCells.getMergeCellList();
final long ctMergeCellCount = ctMergeCells.getCount();
final int ctMergeCellListSize = ctMergeCellList.size();
/*System.out.println(String.format("\ntestMergeRegions(%s)", "After adding first region"));
System.out.println(String.format("ranges.size=%d", ranges.size()));
System.out.println(String.format("numMergedRegions=%d", numMergedRegions));
System.out.println(String.format("ctMergeCellCount=%d", ctMergeCellCount));
System.out.println(String.format("ctMergeCellListSize=%d", ctMergeCellListSize));*/
assertEquals(1, ranges.size());
assertEquals(1, numMergedRegions);
assertEquals(1, ctMergeCellCount);
assertEquals(1, ctMergeCellListSize);
}
}
@Test
public void testBug63509() throws IOException {
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet("sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("1000");
// This causes the error
sheet.addIgnoredErrors(new CellReference(cell), IgnoredErrorType.NUMBER_STORED_AS_TEXT);
// Workaround
// sheet.addIgnoredErrors(new CellReference(cell.getRowIndex(), cell.getColumnIndex(), false, false),
// IgnoredErrorType.NUMBER_STORED_AS_TEXT);
/*File file = new File("/tmp/63509.xlsx");
try(FileOutputStream outputStream = new FileOutputStream(file)) {
workbook.write(outputStream);
}*/
}
}
@Test(expected = POIXMLException.class)
public void test64045() throws IOException, InvalidFormatException {
File file = XSSFTestDataSamples.getSampleFile("xlsx-corrupted.xlsx");
try (XSSFWorkbook ignored = new XSSFWorkbook(file)) {
fail("Should catch exception as the file is corrupted");
}
}
@Test
public void test58896WithFile() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("58896.xlsx")) {
Sheet sheet = wb.getSheetAt(0);
Instant start = Instant.now();
LOG.log(POILogger.INFO, "Autosizing columns...");
for (int i = 0; i < 3; ++i) {
LOG.log(POILogger.INFO, "Autosize " + i + " - " + Duration.between(start, Instant.now()));
sheet.autoSizeColumn(i);
}
for (int i = 0; i < 69 - 35 + 1; ++i)
for (int j = 0; j < 8; ++j) {
int col = 3 + 2 + i * (8 + 2) + j;
LOG.log(POILogger.INFO, "Autosize " + col + " - " + Duration.between(start, Instant.now()));
sheet.autoSizeColumn(col);
}
LOG.log(POILogger.INFO, Duration.between(start, Instant.now()));
}
}
@Test
public void testBug63845() throws IOException {
try (Workbook wb = new XSSFWorkbook()) {
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0, CellType.FORMULA);
cell.setCellFormula("SUM(B1:E1)");
assertNull("Element 'v' should not be set for formulas unless the value was calculated",
((XSSFCell) cell).getCTCell().getV());
try (Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb)) {
Cell cellBack = wbBack.getSheetAt(0).getRow(0).getCell(0);
assertNull("Element 'v' should not be set for formulas unless the value was calculated",
((XSSFCell) cellBack).getCTCell().getV());
assertNotNull("Formula should be set internally now",
((XSSFCell) cellBack).getCTCell().getF());
wbBack.getCreationHelper().createFormulaEvaluator().evaluateInCell(cellBack);
assertEquals("Element 'v' should be set now as the formula was calculated manually",
"0.0", ((XSSFCell) cellBack).getCTCell().getV());
cellBack.setCellValue("123");
assertEquals("String value should be set now",
"123", cellBack.getStringCellValue());
assertNull("No formula should be set any more",
((XSSFCell) cellBack).getCTCell().getF());
}
}
}
@Test
public void testBug63845_2() throws IOException {
try (Workbook wb = new XSSFWorkbook()) {
Sheet sheet = wb.createSheet("test");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue(2);
row.createCell(1).setCellValue(5);
row.createCell(2).setCellFormula("A1+B1");
try (Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb)) {
Cell cellBack = wbBack.getSheetAt(0).getRow(0).getCell(2);
assertNull("Element 'v' should not be set for formulas unless the value was calculated",
((XSSFCell) cellBack).getCTCell().getV());
wbBack.getCreationHelper().createFormulaEvaluator().evaluateInCell(cellBack);
assertEquals("Element 'v' should be set now as the formula was calculated manually",
"7.0", ((XSSFCell) cellBack).getCTCell().getV());
}
}
}
@Test
public void testBug64508() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("64508.xlsx")) {
int activeSheet = wb.getActiveSheetIndex();
Sheet sheet1 = wb.getSheetAt(activeSheet);
Row row = sheet1.getRow(1);
CellReference aCellReference = new CellReference("E2");
Cell aCell = row.getCell(aCellReference.getCol());
Assert.assertEquals(CellType.STRING, aCell.getCellType());
Assert.assertEquals("", aCell.getStringCellValue());
}
}
@Test
public void testBug64667() throws IOException {
//test that an NPE isn't thrown on opening
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("64667.xlsx")) {
int activeSheet = wb.getActiveSheetIndex();
assertEquals(0, activeSheet);
assertNotNull(wb.getSheetAt(activeSheet));
}
}
@Test
public void testXLSXinPPT() throws Exception {
Assume.assumeFalse(Boolean.getBoolean("scratchpad.ignore"));
try (SlideShow<?,?> ppt = SlideShowFactory.create(
POIDataSamples.getSlideShowInstance().openResourceAsStream("testPPT_oleWorkbook.ppt"))) {
Slide<?, ?> slide = ppt.getSlides().get(1);
ObjectShape<?,?> oleShape = (ObjectShape<?,?>)slide.getShapes().get(2);
org.apache.poi.sl.usermodel.ObjectData data = oleShape.getObjectData();
assertNull(data.getFileName());
// Will be OOXML wrapped in OLE2, not directly SpreadSheet
POIFSFileSystem fs = new POIFSFileSystem(data.getInputStream());
assertTrue(fs.getRoot().hasEntry(OOXML_PACKAGE));
assertFalse(fs.getRoot().hasEntry("Workbook"));
// Can fetch Package to get OOXML
DirectoryNode root = fs.getRoot();
DocumentEntry docEntry = (DocumentEntry) root.getEntry(OOXML_PACKAGE);
try (DocumentInputStream dis = new DocumentInputStream(docEntry);
OPCPackage pkg = OPCPackage.open(dis);
XSSFWorkbook wb = new XSSFWorkbook(pkg)) {
assertEquals(1, wb.getNumberOfSheets());
}
// Via the XSSF Factory
XSSFWorkbookFactory xssfFactory = new XSSFWorkbookFactory();
try (XSSFWorkbook wb = xssfFactory.create(fs.getRoot(), null)) {
assertEquals(1, wb.getNumberOfSheets());
}
// Or can open via the normal Factory, as stream or OLE2
try (Workbook wb = WorkbookFactory.create(fs)) {
assertEquals(1, wb.getNumberOfSheets());
}
try (Workbook wb = WorkbookFactory.create(data.getInputStream())) {
assertEquals(1, wb.getNumberOfSheets());
}
}
}
}
|