File: xlsx-write.c

package info (click to toggle)
gnumeric 1.12.18-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 103,816 kB
  • ctags: 25,022
  • sloc: ansic: 278,696; xml: 54,477; sh: 11,677; perl: 4,216; makefile: 2,751; yacc: 1,324; python: 203
file content (3078 lines) | stat: -rw-r--r-- 100,207 bytes parent folder | download
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
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * xlsx-write.c : export MS Office Open xlsx files.
 *
 * Copyright (C) 2006-2007 Jody Goldberg (jody@gnome.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

/*****************************************************************************/

#include <gnumeric-config.h>
#include <goffice/goffice.h>

#include "ms-excel-write.h"
#include "xlsx-utils.h"

#include "parse-util.h"
#include "workbook.h"
#include "workbook-priv.h"
#include "workbook-view.h"
#include "sheet.h"
#include "sheet-style.h"
#include "sheet-view.h"
#include "sheet-filter.h"
#include "ranges.h"
#include "value.h"
#include "cell.h"
#include "expr.h"
#include "expr-impl.h"
#include "func.h"
#include "style-color.h"
#include "validation.h"
#include "hlink.h"
#include "input-msg.h"
#include "print-info.h"
#include "gutils.h"
#include "sheet-object.h"
#include "sheet-object-cell-comment.h"
#include "sheet-object-graph.h"
#include "graph.h"
#include "style-border.h"
#include "style-conditions.h"
#include "gutils.h"
#include "expr-name.h"

#include "go-val.h"

#include <gsf/gsf-output.h>
#include <gsf/gsf-outfile.h>
#include <gsf/gsf-outfile-zip.h>
#include <gsf/gsf-open-pkg-utils.h>
#include <gsf/gsf-utils.h>
#include <gsf/gsf-libxml.h>
#include <glib/gi18n-lib.h>
#include <gsf/gsf-meta-names.h>
#include <gsf/gsf-doc-meta-data.h>
#include <gsf/gsf-docprop-vector.h>
#include <gsf/gsf-timestamp.h>
#include <gmodule.h>
#include <string.h>

#define NUM_FORMAT_BASE 100

enum {
	ECMA_376_2006 = 1,
	ECMA_376_2008 = 2
};

static char const *ns_ss	 = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
static char const *ns_ss_drawing = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing";
static char const *ns_docprops_core_cp       = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
static char const *ns_docprops_core_dc       = "http://purl.org/dc/elements/1.1/";
static char const *ns_docprops_core_dcmitype = "http://purl.org/dc/dcmitype/";
static char const *ns_docprops_core_dcterms  = "http://purl.org/dc/terms/";
static char const *ns_docprops_core_xsi      = "http://www.w3.org/2001/XMLSchema-instance";
static char const *ns_docprops_extended      = "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties";
static char const *ns_docprops_extended_vt   = "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes";
static char const *ns_docprops_custom        = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties";
static char const *ns_drawing	 = "http://schemas.openxmlformats.org/drawingml/2006/main";
static char const *ns_chart	 = "http://schemas.openxmlformats.org/drawingml/2006/chart";
static char const *ns_rel	 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
static char const *ns_rel_hlink	 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";
static char const *ns_rel_draw	 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing";
static char const *ns_rel_chart	 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart";
static char const *ns_rel_com	 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments";
typedef struct {
	XLExportBase base;

	gint             version;

	Sheet const	*sheet;
	GHashTable	*shared_string_hash;
	GPtrArray	*shared_string_array;
	GHashTable	*styles_hash;
	GPtrArray	*styles_array;
	GHashTable	*dxfs_hash;
	GPtrArray	*dxfs_array;
	GnmConventions	*convs;
	GOIOContext	*io_context;

	GsfOutfile	*xl_dir;
	struct {
		unsigned int	count;
		GsfOutfile	*dir;
	} chart, drawing, pivotCache, pivotTable;
	unsigned comment;
	GOFormat *date_fmt;
} XLSXWriteState;

typedef struct {
	XLSXWriteState	*state;
	GsfXMLOut	*xml;
} XLSXClosure;

typedef struct {
	int code;
	int width_mm;
	gdouble width;
	gdouble height;
	GtkUnit unit;
} XLSXPaperDefs;

static void
xlsx_add_bool (GsfXMLOut *xml, char const *id, gboolean val)
{
	gsf_xml_out_add_cstr_unchecked (xml, id, val ? "1" : "0");
}
static void
xlsx_add_rgb (GsfXMLOut *xml, char const *id, GOColor c)
{
	char buf [3 * 4 * sizeof (unsigned int) + 1];
	sprintf (buf, "%02X%02X%02X%02X",
		 GO_COLOR_UINT_A (c), GO_COLOR_UINT_R (c),
		 GO_COLOR_UINT_G (c), GO_COLOR_UINT_B (c));
	gsf_xml_out_add_cstr_unchecked (xml, id, buf);
}
static void
xlsx_add_pos (GsfXMLOut *xml, char const *id, GnmCellPos const *pos)
{
	gsf_xml_out_add_cstr_unchecked (xml, id,
		cellpos_as_string (pos));
}
static void
xlsx_add_range (GsfXMLOut *xml, char const *id, GnmRange const *range)
{
	gsf_xml_out_add_cstr_unchecked (xml, id,
		range_as_string (range));
}
static void
xlsx_add_range_list (GsfXMLOut *xml, char const *id, GSList const *ranges)
{
	GString *accum = g_string_new (NULL);

	for (; NULL != ranges ; ranges = ranges->next) {
		g_string_append (accum, range_as_string (ranges->data));
		if (NULL != ranges->next)
			g_string_append_c (accum, ' ');
	}

	gsf_xml_out_add_cstr_unchecked (xml, id, accum->str);
	g_string_free (accum, TRUE);
}
static void
xlsx_add_pt (GsfXMLOut *xml, char const *id, double l)
{
	GString *str = g_string_new (NULL);

	g_string_append_printf (str, "%.2fpt", l);
	gsf_xml_out_add_cstr_unchecked (xml, id, str->str);
	g_string_free (str, TRUE);
}

/****************************************************************************/

#define N_PREDEFINED_FILLS (G_N_ELEMENTS (pre_def_fills) - 1)

static char const * const pats[] = {
	"solid",            /* 1 */
	"darkGray",         /* 2 */
	"mediumGray",       /* 3 */
	"lightGray",        /* 4 */
	"gray125",          /* 5 */
	"gray0625",         /* 6 */
	"darkHorizontal",   /* 7 */
	"darkVertical",     /* 8 */
	"darkUp",           /* 9 */
	"darkDown",         /* 10 */
	"darkGrid",         /* 11 */
	"darkTrellis",      /* 12 */
	"lightHorizontal",  /* 13 */
	"lightVertical",    /* 14 */
	"lightDown",        /* 15 */
	"lightUp",          /* 16 */
	"lightGrid",        /* 17 */
	"lightTrellis",     /* 18 */
	/* Not in Excel */
	"lightVertical",    /* 19 */ 
	"darkHorizontal",   /* 20 */
	"lightGray",        /* 21 */
	"lightGray",        /* 22 */
	"darkGray",         /* 23 */
	"solid",            /* 24 */
};

static char const * const pre_def_fills[] = {
	"none",
	"gray125",
	NULL
};

static void
xlsx_write_predefined_fills (GsfXMLOut *xml)
{
	char const * const *f = pre_def_fills;

	while (*f != NULL) {
		gsf_xml_out_start_element (xml, "fill");
		gsf_xml_out_start_element (xml, "patternFill");
		gsf_xml_out_add_cstr_unchecked (xml, "patternType", *f++);
		gsf_xml_out_end_element (xml);
		gsf_xml_out_end_element (xml);
	}
}

static gint
xlsx_find_predefined_fill (GnmStyle const *style)
{
	gboolean pattern_is_std =
		(!gnm_style_is_element_set (style, MSTYLE_COLOR_PATTERN) ||
		 gnm_style_get_pattern_color (style)->go_color == GO_COLOR_BLACK);
	gboolean back_is_std =
		(!gnm_style_is_element_set (style, MSTYLE_COLOR_BACK) ||
		 gnm_style_get_back_color (style)->go_color == GO_COLOR_WHITE);

	if (gnm_style_is_element_set (style, MSTYLE_PATTERN) &&
	    gnm_style_get_pattern (style) == 0 &&
	    pattern_is_std &&
	    back_is_std)
		return 0;
	if (gnm_style_is_element_set (style, MSTYLE_PATTERN) &&
	    gnm_style_get_pattern (style) == 5 &&
	    pattern_is_std &&
	    back_is_std
	    )
		return 1;

	return -1;
}

/****************************************************************************/

static void
xlsx_write_rich_text (GsfXMLOut *xml, char const *text, PangoAttrList *attrs,
		      gboolean allow_xml_space)
{
	PangoAttrIterator *iter;
	int start, end, max;

	if (attrs == NULL) {
		gsf_xml_out_start_element (xml, "t");
		gsf_xml_out_add_cstr (xml, NULL, text);
		gsf_xml_out_end_element (xml); /* </t> */
		return;
	}

	max = strlen (text);
	iter = pango_attr_list_get_iterator (attrs);
	do {
		PangoAttribute *attr;
		GOFontScript fs = GO_FONT_SCRIPT_STANDARD;

		gsf_xml_out_start_element (xml, "r");
		gsf_xml_out_start_element (xml, "rPr");

		attr = pango_attr_iterator_get (iter, PANGO_ATTR_FAMILY);
		if (attr) {
			gsf_xml_out_start_element (xml, "rFont");
			gsf_xml_out_add_cstr_unchecked (xml, "val", ((PangoAttrString *) attr)->value);
			gsf_xml_out_end_element (xml); /* </rFont> */
		}

		attr = pango_attr_iterator_get (iter, PANGO_ATTR_WEIGHT);
		if (attr) {
			gsf_xml_out_start_element (xml, "b");
			gsf_xml_out_add_cstr_unchecked (xml, "val", ((PangoAttrInt *) attr)->value > PANGO_WEIGHT_NORMAL ? "true": "false");
			gsf_xml_out_end_element (xml); /* </b> */
		}

		attr = pango_attr_iterator_get (iter, PANGO_ATTR_STYLE);
		if (attr) {
			gsf_xml_out_start_element (xml, "i");
			gsf_xml_out_add_cstr_unchecked (xml, "val", ((PangoAttrInt *) attr)->value != PANGO_STYLE_NORMAL ? "true": "false");
			gsf_xml_out_end_element (xml); /* </i> */
		}

		attr = pango_attr_iterator_get (iter, PANGO_ATTR_STRIKETHROUGH);
		if (attr) {
			gsf_xml_out_start_element (xml, "strike");
			gsf_xml_out_add_cstr_unchecked (xml, "val", ((PangoAttrInt *) attr)->value ? "true": "false");
			gsf_xml_out_end_element (xml); /* </strike> */
		}

		attr = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
		if (attr) {
			PangoColor *color = &((PangoAttrColor *) attr)->color;
			char *buf = g_strdup_printf("ff%02x%02x%02x", color->red >> 8, color->green >> 8, color->blue >> 8);
			gsf_xml_out_start_element (xml, "color");
			gsf_xml_out_add_cstr_unchecked (xml, "rgb", buf);
			gsf_xml_out_end_element (xml); /* </color> */
			g_free (buf);
		}

		attr = pango_attr_iterator_get (iter, PANGO_ATTR_SIZE);
		if (attr) {
			gsf_xml_out_start_element (xml, "sz");
			gsf_xml_out_add_uint (xml, "val", ((PangoAttrInt *) attr)->value / PANGO_SCALE);
			gsf_xml_out_end_element (xml); /* </sz> */
		}

		attr = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
		if (attr) {
			PangoUnderline u = ((PangoAttrInt *) attr)->value;
			gsf_xml_out_start_element (xml, "u");
			switch (u) {
			case PANGO_UNDERLINE_NONE:
			default:
				gsf_xml_out_add_cstr_unchecked (xml, "val", "none");
				break;
			case PANGO_UNDERLINE_ERROR:
				/* not supported by OpenXML */
				/* Fall through. */
			case PANGO_UNDERLINE_SINGLE:
				gsf_xml_out_add_cstr_unchecked (xml, "val", "single");
				break;
			case PANGO_UNDERLINE_DOUBLE:
				gsf_xml_out_add_cstr_unchecked (xml, "val", "double");
				break;
			case PANGO_UNDERLINE_LOW:
				gsf_xml_out_add_cstr_unchecked (xml, "val", "singleAccounting");
				break;
			}
			gsf_xml_out_end_element (xml); /* </u> */
		}

		attr = pango_attr_iterator_get (iter, go_pango_attr_superscript_get_attr_type ());
		if (attr && ((PangoAttrInt *) attr)->value)
			fs = GO_FONT_SCRIPT_SUPER;
		attr = pango_attr_iterator_get (iter, go_pango_attr_subscript_get_attr_type ());
		if (attr && ((PangoAttrInt *) attr)->value)
			fs = GO_FONT_SCRIPT_SUB;
		if (fs != GO_FONT_SCRIPT_STANDARD) {
			const char *va = (fs == GO_FONT_SCRIPT_SUB)
				? "subscript"
				: "superscript";
			gsf_xml_out_start_element (xml, "vertAlign");
			gsf_xml_out_add_cstr_unchecked (xml, "val", va);
			gsf_xml_out_end_element (xml); /* </vertAlign> */
		}

		gsf_xml_out_end_element (xml); /* </rPr> */
		gsf_xml_out_start_element (xml, "t");
		pango_attr_iterator_range (iter, &start, &end);
		if (end > max)
		    end = max;
		if (start < end) {
			char *buf = g_strndup (text + start, end - start);
			if (allow_xml_space) {
				const char *p;
				gboolean has_space = FALSE;
				for (p = buf; *p; p = g_utf8_next_char (p)) {
					if (g_unichar_isspace (g_utf8_get_char (p))) {
						has_space = TRUE;
						break;
					}
				}
				if (has_space)
					gsf_xml_out_add_cstr_unchecked
						(xml, "xml:space", "preserve");
			}
			gsf_xml_out_add_cstr (xml, NULL, buf);
			g_free (buf);
		}
		gsf_xml_out_end_element (xml); /* </t> */
		gsf_xml_out_end_element (xml); /* </r> */
	} while (pango_attr_iterator_next (iter));
	pango_attr_iterator_destroy (iter);
}

static int
xlsx_shared_string (XLSXWriteState *state, GnmValue const *v)
{
	gpointer tmp;
	int i;

	g_return_val_if_fail (VALUE_IS_STRING (v), -1);

	if (g_hash_table_lookup_extended (state->shared_string_hash,
					  v, NULL, &tmp))
		i = GPOINTER_TO_INT (tmp);
	else {
		GnmValue *v2 = value_dup (v);

		if (VALUE_FMT (v2) && !go_format_is_markup (VALUE_FMT (v2)))
			value_set_fmt (v2, NULL);

		i = state->shared_string_array->len;
		g_ptr_array_add (state->shared_string_array, v2);
		g_hash_table_insert (state->shared_string_hash,
				     v2, GINT_TO_POINTER (i));
	}

	return i;
}

static void
xlsx_write_shared_strings (XLSXWriteState *state, GsfOutfile *wb_part)
{
	const unsigned N = state->shared_string_array->len;
	unsigned i;
	GsfOutput *part;
	GsfXMLOut *xml;

	if (N == 0)
		return;

	part = gsf_outfile_open_pkg_add_rel
		(state->xl_dir, "sharedStrings.xml",
		 "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
		 wb_part,
		 "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings");
	xml = gsf_xml_out_new (part);

	gsf_xml_out_start_element (xml, "sst");
	gsf_xml_out_add_cstr_unchecked (xml, "xmlns", ns_ss);
	gsf_xml_out_add_int (xml, "uniqueCount", N);
	gsf_xml_out_add_int (xml, "count", N);

	for (i = 0; i < N; i++) {
		GnmValue const *val =
			g_ptr_array_index (state->shared_string_array, i);
		PangoAttrList *attrs = VALUE_FMT (val)
			? (PangoAttrList *)go_format_get_markup (VALUE_FMT (val))
			: NULL;
		gsf_xml_out_start_element (xml, "si");
		xlsx_write_rich_text (xml,
				      value_peek_string (val),
				      attrs,
				      FALSE);
		gsf_xml_out_end_element (xml); /* </si> */
	}

	gsf_xml_out_end_element (xml); /* </sst> */

	g_object_unref (xml);
	gsf_output_close (part);
	g_object_unref (part);
}

static void
xlsx_load_buildin_num_formats (GHashTable *hash)
{
	static char const * const std_builtins[] = {
		/* 0 */	 "General",
		/* 1 */	 "0",
		/* 2 */	 "0.00",
		/* 3 */	 "#,##0",
		/* 4 */	 "#,##0.00",
		/* 5 */	 NULL,
		/* 6 */	 NULL,
		/* 7 */	 NULL,
		/* 8 */	 NULL,
		/* 9 */  "0%",
		/* 10 */ "0.00%",
		/* 11 */ "0.00E+00",
		/* 12 */ "# ?/?",
		/* 13 */ "# ?""?/?""?",	/* silly trick to avoid using a trigraph */
		/* 14 */ "mm-dd-yy",
		/* 15 */ "d-mmm-yy",
		/* 16 */ "d-mmm",
		/* 17 */ "mmm-yy",
		/* 18 */ "h:mm AM/PM",
		/* 19 */ "h:mm:ss AM/PM",
		/* 20 */ "h:mm",
		/* 21 */ "h:mm:ss",
		/* 22 */ "m/d/yy h:mm",
		/* 23 */ NULL,
		/* 24 */ NULL,
		/* 25 */ NULL,
		/* 26 */ NULL,
		/* 27 */ NULL,
		/* 28 */ NULL,
		/* 29 */ NULL,
		/* 30 */ NULL,
		/* 31 */ NULL,
		/* 32 */ NULL,
		/* 33 */ NULL,
		/* 34 */ NULL,
		/* 35 */ NULL,
		/* 36 */ NULL,
		/* 37 */ "#,##0 ;(#,##0)",
		/* 38 */ "#,##0 ;[Red](#,##0)",
		/* 39 */ "#,##0.00;(#,##0.00)",
		/* 40 */ "#,##0.00;[Red](#,##0.00)",
		/* 41 */ NULL,
		/* 42 */ NULL,
		/* 43 */ NULL,
		/* 44 */ NULL,
		/* 45 */ "mm:ss",
		/* 46 */ "[h]:mm:ss",
		/* 47 */ "mmss.0",
		/* 48 */ "##0.0E+0",
		/* 49 */ "@"
	};
	unsigned int i;

	g_return_if_fail (NUM_FORMAT_BASE > (int) G_N_ELEMENTS (std_builtins));

	for (i = 1; i < G_N_ELEMENTS (std_builtins); i++)
		if (std_builtins[i] != NULL)
			g_hash_table_insert (hash, g_strdup (std_builtins[i]), GUINT_TO_POINTER (i));
}

static void
xlsx_write_num_format (XLSXWriteState *state, GsfXMLOut *xml,
		       GOFormat const *format, int id)
{
	gsf_xml_out_start_element (xml, "numFmt");
	gsf_xml_out_add_cstr (xml, "formatCode", go_format_as_XL (format));
	gsf_xml_out_add_int (xml, "numFmtId", id);
	gsf_xml_out_end_element (xml);
}

static GHashTable *
xlsx_write_num_formats (XLSXWriteState *state, GsfXMLOut *xml)
{
	GHashTable *hash = g_hash_table_new_full
		(g_str_hash,  g_str_equal, g_free, NULL);
	GHashTable *num_format_hash = g_hash_table_new
		(g_direct_hash, g_direct_equal);
	unsigned int i, count;
	GPtrArray *num_format_array = g_ptr_array_new ();

	xlsx_load_buildin_num_formats (hash);

	for (i = 0 ; i < state->styles_array->len ; i++) {
		GnmStyle const *style = g_ptr_array_index (state->styles_array, i);
		GOFormat const *format;
		char const *xl;
		gpointer tmp;

		if (!gnm_style_is_element_set (style, MSTYLE_FORMAT))
			continue;
		format = gnm_style_get_format (style);
		if (go_format_is_general (format))
			continue;
		xl = go_format_as_XL (format);
		tmp = g_hash_table_lookup (hash, xl);
		if (tmp == NULL) {
			tmp = GUINT_TO_POINTER (num_format_array->len + NUM_FORMAT_BASE);
			g_hash_table_insert (hash, g_strdup (xl), tmp);
			g_ptr_array_add (num_format_array, (gpointer)format);
		}
		g_hash_table_insert (num_format_hash, (gpointer) style, tmp);
	}

	count = num_format_array->len;
	if (count != 0) {
		gsf_xml_out_start_element (xml, "numFmts");
		gsf_xml_out_add_int (xml, "count", count);
		for (i = 0; i < count; i++) {
			GOFormat const *format =
				g_ptr_array_index (num_format_array, i);
			xlsx_write_num_format (state, xml, format,
					       i + NUM_FORMAT_BASE);
		}
		gsf_xml_out_end_element (xml);
	}
	g_ptr_array_free (num_format_array, TRUE);
	g_hash_table_destroy (hash);
	return num_format_hash;
}

static void
xlsx_write_color_element (GsfXMLOut *xml, char const *id, GOColor color)
{
	gsf_xml_out_start_element (xml, id);
	xlsx_add_rgb (xml, "rgb", color);
	gsf_xml_out_end_element (xml);
}

static gint
xlsx_find_font (GnmStyle const *style, GPtrArray  *styles)
{
	unsigned i;
	for (i = 0 ; i < styles->len ; i++) {
		GnmStyle const *old_style = g_ptr_array_index (styles, i);
		if (style == old_style ||
		    (gnm_style_equal_elem (style, old_style, MSTYLE_FONT_BOLD) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_FONT_ITALIC) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_FONT_UNDERLINE) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_FONT_COLOR) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_FONT_SIZE) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_FONT_SCRIPT) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_FONT_STRIKETHROUGH) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_FONT_NAME)))
			return (gint) i;
	}
	return -1;
}

static gboolean
xlsx_has_font_style (GnmStyle const *style)
{
	return (gnm_style_is_element_set (style, MSTYLE_FONT_BOLD) ||
		gnm_style_is_element_set (style, MSTYLE_FONT_ITALIC) ||
		gnm_style_is_element_set (style, MSTYLE_FONT_UNDERLINE) ||
		gnm_style_is_element_set (style, MSTYLE_FONT_COLOR) ||
		gnm_style_is_element_set (style, MSTYLE_FONT_NAME) ||
		gnm_style_is_element_set (style, MSTYLE_FONT_SCRIPT) ||
		gnm_style_is_element_set (style, MSTYLE_FONT_SIZE) ||
		gnm_style_is_element_set (style, MSTYLE_FONT_STRIKETHROUGH));
}

static void
xlsx_write_font (XLSXWriteState *state, GsfXMLOut *xml, GnmStyle const *style)
{
	gsf_xml_out_start_element (xml, "font");
	if (gnm_style_is_element_set (style, MSTYLE_FONT_BOLD)) {
		gsf_xml_out_start_element (xml, "b");
		xlsx_add_bool (xml, "val", gnm_style_get_font_bold (style));
		gsf_xml_out_end_element (xml);
	}
	if (gnm_style_is_element_set (style, MSTYLE_FONT_ITALIC)) {
		gsf_xml_out_start_element (xml, "i");
		xlsx_add_bool (xml, "val", gnm_style_get_font_italic (style));
		gsf_xml_out_end_element (xml);
	}
	if (gnm_style_is_element_set (style, MSTYLE_FONT_UNDERLINE)) {
		static const char * const types[] = {
			"none",
			"single",
			"double",
			"singleAccounting",
			"doubleAccounting"
		};

		gsf_xml_out_start_element (xml, "u");
		gsf_xml_out_add_cstr
			(xml, "val", types[gnm_style_get_font_uline (style)]);
		gsf_xml_out_end_element (xml);
	}
	if (gnm_style_is_element_set (style, MSTYLE_FONT_COLOR))
		xlsx_write_color_element
			(xml, "color",
			 gnm_style_get_font_color (style)->go_color);
	if (gnm_style_is_element_set (style, MSTYLE_FONT_NAME)) {
		gsf_xml_out_start_element (xml, "name");
		gsf_xml_out_add_cstr
			(xml, "val", gnm_style_get_font_name (style));
		gsf_xml_out_end_element (xml);
	}
	if (gnm_style_is_element_set (style, MSTYLE_FONT_SCRIPT)) {
		GOFontScript scr = gnm_style_get_font_script (style);
		const char *val;

		switch (scr) {
		case GO_FONT_SCRIPT_SUB: val = "subscript"; break;
		case GO_FONT_SCRIPT_SUPER: val = "superscript"; break;
		default:
		case GO_FONT_SCRIPT_STANDARD: val = "baseline"; break;
		}
		gsf_xml_out_start_element (xml, "vertAlign");
		gsf_xml_out_add_cstr (xml, "val", val);
		gsf_xml_out_end_element (xml);
	}
	if (gnm_style_is_element_set (style, MSTYLE_FONT_SIZE)) {
		gsf_xml_out_start_element (xml, "sz");
		gsf_xml_out_add_float
			(xml, "val", gnm_style_get_font_size (style),
			 2);
		gsf_xml_out_end_element (xml);

	}
	if (gnm_style_is_element_set (style, MSTYLE_FONT_STRIKETHROUGH)) {
		gsf_xml_out_start_element (xml, "strike");
		xlsx_add_bool (xml, "val", gnm_style_get_font_strike (style));
		gsf_xml_out_end_element (xml);
	}
	gsf_xml_out_end_element (xml); /* font */
}

static GHashTable *
xlsx_write_fonts (XLSXWriteState *state, GsfXMLOut *xml)
{
	GHashTable *fonts_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
	GPtrArray  *styles_w_fonts = g_ptr_array_new ();
	unsigned int i;

	for (i = 0 ; i < state->styles_array->len ; i++) {
		GnmStyle const *style = g_ptr_array_index (state->styles_array, i);
		if (xlsx_has_font_style (style)) {
			gint font_n = xlsx_find_font (style, styles_w_fonts);
			if (font_n < 0) {
				g_ptr_array_add (styles_w_fonts, (gpointer)style);
				g_hash_table_insert (fonts_hash, (gpointer)style,
						     GINT_TO_POINTER (styles_w_fonts->len));
			} else
				g_hash_table_insert (fonts_hash, (gpointer)style,
						     GINT_TO_POINTER (font_n + 1));
		}
	}

	if (styles_w_fonts->len > 0) {
		gsf_xml_out_start_element (xml, "fonts");
		gsf_xml_out_add_int (xml, "count", styles_w_fonts->len);
		for (i = 0 ; i < styles_w_fonts->len ; i++) {
			GnmStyle const *style = g_ptr_array_index (styles_w_fonts, i);
			xlsx_write_font (state, xml, style);
		}
		gsf_xml_out_end_element (xml);
	}

	g_ptr_array_free (styles_w_fonts, TRUE);
	return fonts_hash;
}

static gint
xlsx_find_fill (GnmStyle const *style, GPtrArray *styles)
{
	unsigned i;
	int j;

	j = xlsx_find_predefined_fill (style);
	if (j >= 0)
		return j;
	for (i = 0 ; i < styles->len ; i++) {
		GnmStyle const *old_style = g_ptr_array_index (styles, i);
		if (style == old_style ||
		    (gnm_style_equal_elem (style, old_style, MSTYLE_COLOR_BACK) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_COLOR_PATTERN) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_PATTERN)))
			return (gint) i + N_PREDEFINED_FILLS;
	}
	return -1;
}

static gboolean
xlsx_has_background_style (GnmStyle const *style)
{
	return (gnm_style_is_element_set (style, MSTYLE_COLOR_BACK) ||
		gnm_style_is_element_set (style, MSTYLE_COLOR_PATTERN) ||
		gnm_style_is_element_set (style, MSTYLE_PATTERN));
}

static void
xlsx_write_background (XLSXWriteState *state, GsfXMLOut *xml,
		       GnmStyle const *style, gboolean invert_solid)
{
	/*
	 * MAGIC:
	 * Looks like pattern background and forground colours are inverted
	 * for dxfs with solid fills for no apparent reason.
	 */
	gboolean invert = FALSE;
	GnmColor *fg;
	GnmColor *bg;

	gsf_xml_out_start_element (xml, "fill");
	gsf_xml_out_start_element (xml, "patternFill");
	if (gnm_style_is_element_set (style, MSTYLE_PATTERN)) {
		gint pattern = gnm_style_get_pattern (style);
		const char *type;
		if (pattern <= 0 || pattern > (gint)G_N_ELEMENTS (pats)) {
			type = "none";
		} else {
			invert = (pattern == 1 && invert_solid);
			type = pats[pattern - 1];
		}
		gsf_xml_out_add_cstr_unchecked (xml, "patternType", type);
	}

	fg = gnm_style_is_element_set (style, MSTYLE_COLOR_BACK)
		? gnm_style_get_back_color (style)
		: NULL;
	bg = gnm_style_is_element_set (style, MSTYLE_COLOR_PATTERN)
		? gnm_style_get_pattern_color (style)
		: NULL;
	if (invert) {
		GnmColor *tmp = fg;
		fg = bg;
		bg = tmp;
	}

	if (fg)
		xlsx_write_color_element (xml, "fgColor", fg->go_color);
	if (bg)
		xlsx_write_color_element (xml, "bgColor", bg->go_color);

	gsf_xml_out_end_element (xml);
	gsf_xml_out_end_element (xml);
}

static GHashTable *
xlsx_write_fills (XLSXWriteState *state, GsfXMLOut *xml)
{
	GHashTable *fills_hash =  g_hash_table_new (g_direct_hash, g_direct_equal);
	GPtrArray  *styles_w_fills  = g_ptr_array_new ();
	unsigned i;

	for (i = 0 ; i < state->styles_array->len ; i++) {
		GnmStyle const *style = g_ptr_array_index (state->styles_array, i);
		if (xlsx_has_background_style (style)) {
			gint fill_n = xlsx_find_fill (style, styles_w_fills);
			if (fill_n < 0) {
				g_ptr_array_add (styles_w_fills, (gpointer)style);
				g_hash_table_insert
					(fills_hash, (gpointer)style,
					 GINT_TO_POINTER (styles_w_fills->len
							  + N_PREDEFINED_FILLS));
			} else
				g_hash_table_insert
					(fills_hash, (gpointer)style,
					 GINT_TO_POINTER
					 (fill_n + 1));
		}
	}

	gsf_xml_out_start_element (xml, "fills");
	gsf_xml_out_add_int (xml, "count", styles_w_fills->len
			     + N_PREDEFINED_FILLS);
	/* Excel considers the first few fills special (not according to */
	/* ECMA)                            */
	xlsx_write_predefined_fills (xml);
	for (i = 0 ; i < styles_w_fills->len ; i++) {
		GnmStyle const *style = g_ptr_array_index (styles_w_fills, i);
		xlsx_write_background (state, xml, style, FALSE);
	}
	gsf_xml_out_end_element (xml);

	g_ptr_array_free (styles_w_fills, TRUE);
	return fills_hash;
}

static gint
xlsx_find_border (GnmStyle const *style, GPtrArray  *styles)
{
	unsigned i;
	for (i = 0 ; i < styles->len ; i++) {
		GnmStyle const *old_style = g_ptr_array_index (styles, i);
		if (style == old_style ||
		    (gnm_style_equal_elem (style, old_style, MSTYLE_BORDER_TOP) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_BORDER_BOTTOM) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_BORDER_LEFT) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_BORDER_RIGHT) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_BORDER_DIAGONAL) &&
		     gnm_style_equal_elem (style, old_style, MSTYLE_BORDER_REV_DIAGONAL)))
			return (gint) i;
	}
	return -1;
}

static void
xlsx_write_border (XLSXWriteState *state, GsfXMLOut *xml, GnmBorder *border, GnmStyleElement elem)
{
	if (border == NULL)
		return;
	switch (elem) {
	case MSTYLE_BORDER_TOP:
		gsf_xml_out_start_element (xml, "top");
		break;
	case MSTYLE_BORDER_BOTTOM:
		gsf_xml_out_start_element (xml, "bottom");
		break;
	case MSTYLE_BORDER_LEFT:
		gsf_xml_out_start_element
			(xml, (state->version == ECMA_376_2006) ? "left" : "start");
		break;
	case MSTYLE_BORDER_DIAGONAL:
	case MSTYLE_BORDER_REV_DIAGONAL:
		gsf_xml_out_start_element (xml, "diagonal");
		break;
	default:
	case MSTYLE_BORDER_RIGHT:
		gsf_xml_out_start_element
			(xml, (state->version == ECMA_376_2006) ? "right" : "end");
		break;
	}
	switch (border->line_type) {
	case GNM_STYLE_BORDER_THIN:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "thin");
		break;
	case GNM_STYLE_BORDER_MEDIUM:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "medium");
		break;
	case GNM_STYLE_BORDER_DASHED:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "dashed");
		break;
	case GNM_STYLE_BORDER_DOTTED:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "dotted");
		break;
	case GNM_STYLE_BORDER_THICK:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "thick");
		break;
	case GNM_STYLE_BORDER_DOUBLE:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "double");
		break;
	case GNM_STYLE_BORDER_HAIR:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "hair");
		break;
	case GNM_STYLE_BORDER_MEDIUM_DASH:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "mediumDashed");
		break;
	case GNM_STYLE_BORDER_DASH_DOT:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "dashDot");
		break;
	case GNM_STYLE_BORDER_MEDIUM_DASH_DOT:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "mediumDashDot");
		break;
	case GNM_STYLE_BORDER_DASH_DOT_DOT:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "dashDotDot");
		break;
	case GNM_STYLE_BORDER_MEDIUM_DASH_DOT_DOT:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "mediumDashDotDot");
		break;
	case GNM_STYLE_BORDER_SLANTED_DASH_DOT:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "slantDashDot");
		break;
	default:
	case GNM_STYLE_BORDER_NONE:
		gsf_xml_out_add_cstr_unchecked (xml, "style", "none");
		break;
	}

	if (border->color != NULL)
		xlsx_write_color_element (xml, "color",  border->color->go_color);

	gsf_xml_out_end_element (xml);
}

static gboolean
xlsx_has_border_style (GnmStyle const *style)
{
	return (gnm_style_is_element_set (style, MSTYLE_BORDER_TOP) ||
		gnm_style_is_element_set (style, MSTYLE_BORDER_BOTTOM) ||
		gnm_style_is_element_set (style, MSTYLE_BORDER_LEFT) ||
		gnm_style_is_element_set (style, MSTYLE_BORDER_RIGHT) ||
		gnm_style_is_element_set (style, MSTYLE_BORDER_REV_DIAGONAL) ||
		gnm_style_is_element_set (style, MSTYLE_BORDER_DIAGONAL));
}

static void
xlsx_write_full_border (XLSXWriteState *state, GsfXMLOut *xml,
			GnmStyle const *style)
{
	gboolean diagonal_border_written = FALSE;
	gsf_xml_out_start_element (xml, "border");
	if (gnm_style_is_element_set (style, MSTYLE_BORDER_DIAGONAL)) {
		GnmBorder *border = gnm_style_get_border
			(style, MSTYLE_BORDER_DIAGONAL);
		gsf_xml_out_add_bool
			(xml, "diagonalUp",
			 (border && border->line_type != GNM_STYLE_BORDER_NONE));
	}
	if (gnm_style_is_element_set (style, MSTYLE_BORDER_REV_DIAGONAL)) {
		GnmBorder *border = gnm_style_get_border
			(style, MSTYLE_BORDER_REV_DIAGONAL);
		gsf_xml_out_add_bool
			(xml, "diagonalDown",
			 (border && border->line_type != GNM_STYLE_BORDER_NONE));
	}
	if (gnm_style_is_element_set (style, MSTYLE_BORDER_LEFT))
		xlsx_write_border (state, xml,
				   gnm_style_get_border
				   (style, MSTYLE_BORDER_LEFT),
				   MSTYLE_BORDER_LEFT);
	if (gnm_style_is_element_set (style, MSTYLE_BORDER_RIGHT))
		xlsx_write_border (state, xml,
				   gnm_style_get_border
				   (style, MSTYLE_BORDER_RIGHT),
				   MSTYLE_BORDER_RIGHT);
	if (gnm_style_is_element_set (style, MSTYLE_BORDER_TOP))
		xlsx_write_border (state, xml,
				   gnm_style_get_border
				   (style, MSTYLE_BORDER_TOP),
				   MSTYLE_BORDER_TOP);
	if (gnm_style_is_element_set (style, MSTYLE_BORDER_BOTTOM))
		xlsx_write_border (state, xml,
				   gnm_style_get_border
				   (style, MSTYLE_BORDER_BOTTOM),
				   MSTYLE_BORDER_BOTTOM);
	if (gnm_style_is_element_set (style, MSTYLE_BORDER_DIAGONAL)) {
		GnmBorder *border = gnm_style_get_border
			(style, MSTYLE_BORDER_DIAGONAL);
		if (border && border->line_type != GNM_STYLE_BORDER_NONE) {
			diagonal_border_written = TRUE;
			xlsx_write_border (state, xml,
					   border,
					   MSTYLE_BORDER_DIAGONAL);
		}
	}
	if (!diagonal_border_written &&
	    gnm_style_is_element_set (style, MSTYLE_BORDER_REV_DIAGONAL)) {
		GnmBorder *border = gnm_style_get_border
			(style, MSTYLE_BORDER_REV_DIAGONAL);
		if (border && border->line_type != GNM_STYLE_BORDER_NONE) {
			xlsx_write_border (state, xml,
					   border,
					   MSTYLE_BORDER_REV_DIAGONAL);
		}
	}
	gsf_xml_out_end_element (xml); /* border */
}


static GHashTable *
xlsx_write_borders (XLSXWriteState *state, GsfXMLOut *xml)
{
	GHashTable *border_hash =  g_hash_table_new (g_direct_hash, g_direct_equal);
	GPtrArray  *styles_w_border  = g_ptr_array_new ();
	unsigned int i;

	for (i = 0 ; i < state->styles_array->len ; i++) {
		GnmStyle const *style = g_ptr_array_index (state->styles_array, i);
		if (xlsx_has_border_style (style)) {
			gint border_n = xlsx_find_border (style, styles_w_border);
			if (border_n < 0) {
				g_ptr_array_add (styles_w_border, (gpointer)style);
				g_hash_table_insert (border_hash, (gpointer)style,
						     GINT_TO_POINTER (styles_w_border->len));
			} else
				g_hash_table_insert (border_hash, (gpointer)style,
						     GINT_TO_POINTER (border_n + 1));
		}
	}

	if (styles_w_border->len > 0) {
		gsf_xml_out_start_element (xml, "borders");
		gsf_xml_out_add_int (xml, "count", styles_w_border->len);
		for (i = 0; i < styles_w_border->len; i++) {
			GnmStyle const *style = g_ptr_array_index (styles_w_border, i);
			xlsx_write_full_border (state, xml, style);
		}
		gsf_xml_out_end_element (xml);
	}

	g_ptr_array_free (styles_w_border, TRUE);
	return border_hash;
}

static gboolean
xlsx_has_alignment_style (GnmStyle const *style)
{
	return gnm_style_is_element_set (style, MSTYLE_ALIGN_H)
		|| gnm_style_is_element_set (style, MSTYLE_ALIGN_V)
		|| gnm_style_is_element_set (style, MSTYLE_WRAP_TEXT)
		|| gnm_style_is_element_set (style, MSTYLE_SHRINK_TO_FIT)
		|| gnm_style_is_element_set (style, MSTYLE_ROTATION)
		|| gnm_style_is_element_set (style, MSTYLE_INDENT);
}

static void
xlsx_write_style_write_alignment (G_GNUC_UNUSED XLSXWriteState *state, GsfXMLOut *xml,
		  GnmStyle const *style)
{
	gsf_xml_out_start_element (xml, "alignment");
	if (gnm_style_is_element_set (style, MSTYLE_ALIGN_H)) {
		switch (gnm_style_get_align_h (style)) {
		case GNM_HALIGN_LEFT:
			gsf_xml_out_add_cstr_unchecked (xml, "horizontal",
							"left");
			break;
		case GNM_HALIGN_RIGHT:
			gsf_xml_out_add_cstr_unchecked (xml, "horizontal",
							"right");
			break;
		case GNM_HALIGN_CENTER:
			gsf_xml_out_add_cstr_unchecked (xml, "horizontal",
							"center");
			break;
		case GNM_HALIGN_FILL:
			gsf_xml_out_add_cstr_unchecked (xml, "horizontal",
							"fill");
			break;
		case GNM_HALIGN_JUSTIFY:
			gsf_xml_out_add_cstr_unchecked (xml, "horizontal",
							"justify");
			break;
		case GNM_HALIGN_CENTER_ACROSS_SELECTION:
			gsf_xml_out_add_cstr_unchecked (xml, "horizontal",
							"centerContinuous");
			break;
		case GNM_HALIGN_DISTRIBUTED:
			gsf_xml_out_add_cstr_unchecked (xml, "horizontal",
							"distributed");
			break;
		case GNM_HALIGN_GENERAL:
		default:
			gsf_xml_out_add_cstr_unchecked (xml, "horizontal",
							"general");
			break;
		}
	}
	if (gnm_style_is_element_set (style, MSTYLE_ALIGN_V)) {
		switch (gnm_style_get_align_v (style)) {
		case GNM_VALIGN_TOP:
			gsf_xml_out_add_cstr_unchecked (xml, "vertical",
							"top");
			break;
		case GNM_VALIGN_BOTTOM:
			gsf_xml_out_add_cstr_unchecked (xml, "vertical",
							"bottom");
			break;
		case GNM_VALIGN_CENTER:
			gsf_xml_out_add_cstr_unchecked (xml, "vertical",
							"center");
			break;
		case GNM_VALIGN_JUSTIFY:
			gsf_xml_out_add_cstr_unchecked (xml, "vertical",
							"justify");
			break;
		default:
		case GNM_VALIGN_DISTRIBUTED:
			gsf_xml_out_add_cstr_unchecked (xml, "vertical",
							"distributed");
			break;
		}
	}
	if (gnm_style_is_element_set (style, MSTYLE_WRAP_TEXT)) {
		gsf_xml_out_add_bool (xml, "wrapText", gnm_style_get_wrap_text (style));
	}
	if (gnm_style_is_element_set (style, MSTYLE_SHRINK_TO_FIT)) {
		gsf_xml_out_add_bool (xml, "shrinkToFit", gnm_style_get_shrink_to_fit (style));
	}
	if (gnm_style_is_element_set (style, MSTYLE_ROTATION)) {
		int r = gnm_style_get_rotation (style);
		if (r == -1)
			r = 0xff;
		else if (r >= 270)
			r = (360 - r) + 90;
		gsf_xml_out_add_int (xml, "textRotation", r);
	}
	if (gnm_style_is_element_set (style, MSTYLE_INDENT)) {
		gsf_xml_out_add_int (xml, "indent", gnm_style_get_indent (style));
	}
	gsf_xml_out_end_element (xml);
}

static void
xlsx_write_style (XLSXWriteState *state, GsfXMLOut *xml,
		  GnmStyle const *style, GHashTable *fills_hash,
		  GHashTable *num_format_hash, GHashTable *fonts_hash,
		  GHashTable *border_hash, gint id)
{
	gboolean alignment = xlsx_has_alignment_style (style);
	gpointer tmp_fill, tmp_font, tmp_border;
	gboolean fill = (NULL != (tmp_fill = g_hash_table_lookup (fills_hash, style)));
	gboolean font = (NULL != (tmp_font = g_hash_table_lookup (fonts_hash, style)));
	gboolean border = (NULL != (tmp_border = g_hash_table_lookup (border_hash, style)));
	gboolean num_fmt = gnm_style_is_element_set (style, MSTYLE_FORMAT);

	if (id >= 0) {
		xlsx_add_bool (xml, "applyAlignment", alignment);
		xlsx_add_bool (xml, "applyBorder", border);
		xlsx_add_bool (xml, "applyFont", font);
		xlsx_add_bool (xml, "applyFill", fill);
		xlsx_add_bool (xml, "applyNumberFormat", num_fmt);
	}
	if (font)
		gsf_xml_out_add_int (xml, "fontId", GPOINTER_TO_INT (tmp_font) - 1);
	if (fill)
		gsf_xml_out_add_int (xml, "fillId", GPOINTER_TO_INT (tmp_fill) - 1);
	if (border)
		gsf_xml_out_add_int (xml, "borderId", GPOINTER_TO_INT (tmp_border) - 1);
	if (num_fmt)
		gsf_xml_out_add_int
			(xml, "numFmtId",
			 GPOINTER_TO_INT (g_hash_table_lookup (num_format_hash, style)));
	if (id >= 0)
		gsf_xml_out_add_int (xml, "xfId", id);

	if (alignment)
		xlsx_write_style_write_alignment (state, xml, style);
}

static void
xlsx_write_cellStyleXfs (XLSXWriteState *state, GsfXMLOut *xml,
		    GHashTable *fills_hash, GHashTable *num_format_hash,
		    GHashTable *fonts_hash, GHashTable *border_hash)
{
	gsf_xml_out_start_element (xml, "cellStyleXfs");
	gsf_xml_out_add_int (xml, "count", 1);
	gsf_xml_out_start_element (xml, "xf");
	xlsx_write_style
		(state, xml,
		 g_ptr_array_index (state->styles_array, 0),
		 fills_hash, num_format_hash, fonts_hash,
		 border_hash, -1);
	gsf_xml_out_end_element (xml);
	gsf_xml_out_end_element (xml);
}

static void
xlsx_write_cellXfs (XLSXWriteState *state, GsfXMLOut *xml,
		    GHashTable *fills_hash, GHashTable *num_format_hash,
		    GHashTable *fonts_hash, GHashTable *border_hash)
{
	unsigned i, N = state->styles_array->len;

	if (N == 0)
		return;

	gsf_xml_out_start_element (xml, "cellXfs");
	gsf_xml_out_add_int (xml, "count", N);
	for (i = 0; i < N; i++) {
		GnmStyle const *style =
			g_ptr_array_index (state->styles_array, i);
		gsf_xml_out_start_element (xml, "xf");
		xlsx_write_style
			(state, xml, style,
			 fills_hash, num_format_hash, fonts_hash,
			 border_hash, 0);
		gsf_xml_out_end_element (xml);
	}
	gsf_xml_out_end_element (xml);
}

static void
xlsx_write_dxfs (XLSXWriteState *state, GsfXMLOut *xml,
		 GHashTable *num_format_hash)
{
	unsigned i, N = state->dxfs_array->len;
	int id = NUM_FORMAT_BASE + g_hash_table_size (num_format_hash);

	if (N == 0)
		return;

	gsf_xml_out_start_element (xml, "dxfs");
	gsf_xml_out_add_int (xml, "count", N);
	for (i = 0; i < N; i++) {
		GnmStyle *style =
			g_ptr_array_index (state->dxfs_array, i);
		gsf_xml_out_start_element (xml, "dxf");
		if (xlsx_has_font_style (style))
			xlsx_write_font (state, xml, style);
		if (xlsx_has_alignment_style (style))
			xlsx_write_style_write_alignment (state, xml, style);
		if (xlsx_has_background_style (style))
			xlsx_write_background (state, xml, style, TRUE);
		if (gnm_style_is_element_set (style, MSTYLE_FORMAT)) {
			GOFormat const *format = gnm_style_get_format (style);
			xlsx_write_num_format (state, xml, format, id++);
		}
		if (xlsx_has_border_style (style))
			xlsx_write_full_border (state, xml, style);

		gsf_xml_out_end_element (xml);
	}
	gsf_xml_out_end_element (xml);
}

static void
xlsx_write_styles (XLSXWriteState *state, GsfOutfile *wb_part)
{
	GsfOutput *part = gsf_outfile_open_pkg_add_rel (state->xl_dir, "styles.xml",
		"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
		wb_part,
		"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles");
	GsfXMLOut *xml = gsf_xml_out_new (part);
	GHashTable *fills_hash, *num_format_hash, *fonts_hash, *border_hash;

	gsf_xml_out_start_element (xml, "styleSheet");
	gsf_xml_out_add_cstr_unchecked (xml, "xmlns", ns_ss);
	/* Note the schema does not allow the attribute xml:space */

	/* The order of elements is fixed in the schema (xsd:sequence) */
	num_format_hash = xlsx_write_num_formats (state, xml);
	fonts_hash = xlsx_write_fonts (state, xml);
	fills_hash = xlsx_write_fills (state, xml);
	border_hash = xlsx_write_borders (state, xml);
	xlsx_write_cellStyleXfs (state, xml, fills_hash, num_format_hash, fonts_hash, border_hash);
	xlsx_write_cellXfs (state, xml, fills_hash, num_format_hash, fonts_hash, border_hash);
	/* <xsd:element name="cellStyles" type="CT_CellStyles" minOccurs="0" maxOccurs="1"/> */
	xlsx_write_dxfs (state, xml, num_format_hash);
	/* <xsd:element name="tableStyles" type="CT_TableStyles" minOccurs="0" maxOccurs="1"/> */
	/* <xsd:element name="colors" type="CT_Colors" minOccurs="0" maxOccurs="1"/> */
	/* <xsd:element name="extLst" type="CT_ExtensionList" minOccurs="0" maxOccurs="1"/> */

	g_hash_table_destroy (fills_hash);
	g_hash_table_destroy (fonts_hash);
	g_hash_table_destroy (num_format_hash);
	g_hash_table_destroy (border_hash);
	gsf_xml_out_end_element (xml); /* </styleSheet> */
	g_object_unref (xml);
	gsf_output_close (part);
	g_object_unref (part);
}

#define XLSX_MAX_COLS	gnm_sheet_get_max_cols (state->sheet)	/* default is (2^14) */
#define XLSX_MAX_ROWS	gnm_sheet_get_max_rows (state->sheet)	/* default is (2^20) */

static void
xlsx_write_sheet_view (GsfXMLOut *xml, SheetView const *sv)
{
	Sheet const *sheet = sv_sheet (sv);
	GnmColor *sheet_auto   = sheet_style_get_auto_pattern_color (sheet);
	GnmColor *default_auto = style_color_auto_pattern ();
	GnmCellPos topLeft, frozen_topLeft;
	char const *activePane = NULL;
	int const frozen_height = sv->unfrozen_top_left.row -
		sv->frozen_top_left.row;
	int const frozen_width = sv->unfrozen_top_left.col -
		sv->frozen_top_left.col;
	int tmp;

	if (frozen_width > 0) {
		topLeft.col		= sv->frozen_top_left.col;
		frozen_topLeft.col	= sv->initial_top_left.col;
	} else {
		topLeft.col		= sv->initial_top_left.col;
		frozen_topLeft.col	= sv->frozen_top_left.col;
	}
	if (frozen_height > 0) {
		topLeft.row		= sv->frozen_top_left.row;
		frozen_topLeft.row	= sv->initial_top_left.row;
	} else {
		topLeft.row		= sv->initial_top_left.row;
		frozen_topLeft.row	= sv->frozen_top_left.row;
	}

	gsf_xml_out_start_element (xml, "sheetView");
	if (topLeft.col > 0 || topLeft.row > 0) /* A1 is the default */
		xlsx_add_pos (xml, "topLeftCell", &topLeft);
	gsf_xml_out_add_int (xml, "workbookViewId",
		wb_view_get_index_in_wb (sv_wbv (sv)));

	tmp = (int) (100.* sheet->last_zoom_factor_used + .5);
	if (tmp != 100)
		gsf_xml_out_add_int (xml, "zoomScale", tmp);

	switch (sv->view_mode) {
	case GNM_SHEET_VIEW_NORMAL_MODE : break;
	case GNM_SHEET_VIEW_PAGE_BREAK_MODE :
		gsf_xml_out_add_cstr_unchecked (xml, "view", "pageBreakPreview"); break;
	case GNM_SHEET_VIEW_LAYOUT_MODE :
		gsf_xml_out_add_cstr_unchecked (xml, "view", "pageLayout"); break;
	}

	if (sheet->hide_grid)
		gsf_xml_out_add_cstr_unchecked (xml, "showGridLines", "0");
	if (sheet->display_formulas)
		gsf_xml_out_add_cstr_unchecked (xml, "showFormulas", "1");
	if (sheet->hide_col_header || sheet->hide_row_header)
		gsf_xml_out_add_cstr_unchecked (xml, "showRowColHeaders", "0");
	if (sheet->hide_zero)
		gsf_xml_out_add_cstr_unchecked (xml, "showZeros", "0");
	if (!sheet->display_outlines)
		gsf_xml_out_add_cstr_unchecked (xml, "showOutlineSymbols", "0");
	if (sheet->text_is_rtl)
		gsf_xml_out_add_cstr_unchecked (xml, "rightToLeft", "1");
	if (sheet == wb_view_cur_sheet (sv_wbv (sv)))
		gsf_xml_out_add_cstr_unchecked (xml, "tabSelected", "1");

	if (!style_color_equal (sheet_auto, default_auto)) {
		gsf_xml_out_add_cstr_unchecked (xml, "defaultGridColor", "1");
#if 0
		gsf_xml_out_add_int (xml, "colorId", grid_color_index);
#endif
	}
	style_color_unref (sheet_auto);
	style_color_unref (default_auto);

	if (sv_is_frozen (sv)) {
		activePane = "bottomRight"; /* h&v freeze */

		gsf_xml_out_start_element (xml, "pane");
		if (frozen_width > 0)
			gsf_xml_out_add_int (xml, "xSplit", frozen_width);
		else
			activePane = "bottomLeft"; /* v freeze */
		if (frozen_height > 0)
			gsf_xml_out_add_int (xml, "ySplit", frozen_height);
		else
			activePane = "topRight"; /* h freeze */
		xlsx_add_pos (xml, "topLeftCell", &frozen_topLeft);
		gsf_xml_out_add_cstr_unchecked (xml, "activePane", activePane);
		gsf_xml_out_add_cstr_unchecked (xml, "state", "frozen");
		gsf_xml_out_end_element (xml); /* </pane> */
	}

	gsf_xml_out_start_element (xml, "selection");
	if (NULL != activePane)
		gsf_xml_out_add_cstr_unchecked (xml, "pane", activePane);
	/* activeCellId is always 0 for gnumeric */
	xlsx_add_pos (xml, "activeCell", &sv->edit_pos);
	xlsx_add_range_list (xml, "sqref", sv->selections);
	gsf_xml_out_end_element (xml); /* </selection> */

	gsf_xml_out_end_element (xml); /* </sheetView> */
}

static void
xlsx_write_init_row (gboolean *needs_row, GsfXMLOut *xml, int r, char const *span)
{
	if (*needs_row) {
		gsf_xml_out_start_element (xml, "row");
		gsf_xml_out_add_int (xml, "r", r+1);
		gsf_xml_out_add_cstr_unchecked (xml, "spans", span);
		*needs_row = FALSE;
	}
}

static gint
xlsx_get_style_id (XLSXWriteState *state, GnmStyle const *style)
{
	gpointer tmp;

	g_return_val_if_fail (style != NULL, 0);

	if (NULL == (tmp = g_hash_table_lookup (state->styles_hash, style))) {
		g_ptr_array_add (state->styles_array, (gpointer) style);
		tmp = GINT_TO_POINTER (state->styles_array->len);
		gnm_style_ref (style);
		g_hash_table_insert (state->styles_hash, (gpointer) style, tmp);
	}
	return GPOINTER_TO_INT (tmp) - 1;
}

static gint
xlsx_get_cond_style_id (XLSXWriteState *state, GnmStyle const *style)
{
	gpointer tmp;

	g_return_val_if_fail (style != NULL, 0);

	if (NULL == (tmp = g_hash_table_lookup (state->dxfs_hash, style))) {
		g_ptr_array_add (state->dxfs_array, (gpointer)style);
		tmp = GINT_TO_POINTER (state->dxfs_array->len);
		g_hash_table_insert (state->dxfs_hash, (gpointer)style, tmp);
	}
	return GPOINTER_TO_INT (tmp) - 1;
}

static gboolean
row_boring (Sheet *sheet, int r)
{
	ColRowInfo const *ri = sheet_row_get (sheet, r);
	if (!ri)
		return TRUE;

	return (!ri->hard_size &&
		fabs (ri->size_pts - sheet->rows.default_style.size_pts) < 1e-6 &&
		!ri->is_collapsed &&
		ri->visible &&
		ri->outline_level == 0);
}

static void
xlsx_write_cells (XLSXWriteState *state, GsfXMLOut *xml,
		  GnmRange const *extent, GnmStyle **col_styles)
{
	int r, c;
	char *content;
	GnmParsePos pp;
	GnmExprTop const *texpr;
	GnmExprArrayCorner const *array;
	char *cheesy_span = g_strdup_printf ("%d:%d", extent->start.col+1, extent->end.col+1);
	Sheet *sheet = (Sheet *)state->sheet;
	GPtrArray *all_cells = sheet_cells (sheet, extent);
	guint cno = 0;
	int *boring_count;
	guint8 *non_defaults_rows = sheet_style_get_nondefault_rows (sheet, col_styles);

	boring_count = g_new0 (int, extent->end.row + 1);
	r = extent->end.row;
	boring_count[r] = row_boring (sheet, r);
	while (r-- > extent->start.row)
		boring_count[r] = row_boring (sheet, r)
			? 1 + boring_count[r + 1]
			: 0;

	/* Add a NULL to simplify code.  */
	g_ptr_array_add (all_cells, NULL);

	gsf_xml_out_start_element (xml, "sheetData");
       	for (r = extent->start.row ; r <= extent->end.row ; r++) {
		gboolean needs_row = TRUE;

		if (boring_count[r] == 0) {
			ColRowInfo const *ri = sheet_row_get (sheet, r);

			/* The code here needs to match row_boring.  */

			if (ri->hard_size) {
				xlsx_write_init_row (&needs_row, xml, r, cheesy_span);
				gsf_xml_out_add_cstr_unchecked (xml, "customHeight", "1");
			}
			if (ri->hard_size || fabs (ri->size_pts - sheet->cols.default_style.size_pts) > 1e-6) {
				xlsx_write_init_row (&needs_row, xml, r, cheesy_span);
				gsf_xml_out_add_float (xml, "ht", ri->size_pts, 4);
			}
			if (ri->is_collapsed) {
				xlsx_write_init_row (&needs_row, xml, r, cheesy_span);
				gsf_xml_out_add_cstr_unchecked (xml, "collapsed", "1");
			}
			if (!ri->visible) {
				xlsx_write_init_row (&needs_row, xml, r, cheesy_span);
				gsf_xml_out_add_cstr_unchecked (xml, "hidden", "1");
			}
			if (ri->outline_level > 0) {
				xlsx_write_init_row (&needs_row, xml, r, cheesy_span);
				gsf_xml_out_add_int (xml, "outlineLevel", ri->outline_level);
			}
		}

		/*
		 * If we didn't have to write anything yet and if the whole
		 * row -- and possibly the ones after it -- are all
		 * using default style, skip them.
		 */
		if (needs_row) {
			GnmCell *cell = g_ptr_array_index (all_cells, cno);
			int dr, rows = (cell ? cell->pos.row : extent->end.row + 1) - r;
			rows = MIN (rows, boring_count[r]);
			for (dr = 0; dr < rows; dr++)
				if (non_defaults_rows[r + dr])
					break;
			rows = MIN (rows, dr);
			if (rows > 0) {
				r += (rows - 1);
				continue;
			}
		}

		for (c = extent->start.col ; c <= extent->end.col ; c++) {
			GnmCell *cell;
			GnmValue const *val;
			GnmStyle const *style;
			GnmStyle *style1 = NULL;
			gint style_id;
			GOFormat const *fmt1, *fmt2;

			cell = g_ptr_array_index (all_cells, cno);
			if (cell && cell->pos.row == r && cell->pos.col == c) {
				cno++;
				val = cell->value;
			} else {
				cell = NULL;
				val = NULL;
			}

			/* FIXME: Use sheet_style_get_row once per row */
			style = sheet_style_get (sheet, c, r);
			fmt1 = gnm_style_get_format (style);
			fmt2 = cell ? gnm_cell_get_format_given_style (cell, style) : fmt1;
			if (fmt1 != fmt2 && !go_format_is_markup (fmt2)) {
				style = style1 = gnm_style_dup (style);
				gnm_style_set_format (style1, fmt2);
			}
			style_id = style && style != col_styles[c]
				? xlsx_get_style_id (state, style)
				: -1;
			if (style1)
				gnm_style_unref (style1);

			if (cell) {
				char const *type;
				gboolean inlineStr = FALSE;
				int str_id = -1;

				xlsx_write_init_row (&needs_row, xml, r, cheesy_span);
				gsf_xml_out_start_element (xml, "c");
				gsf_xml_out_add_cstr_unchecked (xml, "r",
					cell_coord_name (c, r));
				if (style_id > -1)
					gsf_xml_out_add_int (xml, "s", style_id);

				switch (val->type) {
				default :
				case VALUE_EMPTY:
					type = NULL; /* FIXME : what to do ? */
					break;
				case VALUE_BOOLEAN:
					type = "b";
					break;
				case VALUE_FLOAT:
					type = ""; /* "n" is the default */
					break;
				case VALUE_ERROR:
					type = "e";
					break;
				case VALUE_STRING:
					/* A reasonable approximation of * 'is_shared'.  It can get spoofed by
					 * rich text references to a base * string */
					if (go_string_get_ref_count (val->v_str.val) > 1) {
						str_id = xlsx_shared_string (state, val);
						type = "s";
					} else if (gnm_cell_has_expr (cell))
						type = "str";
					else {
						type = "inlineStr";
						inlineStr = TRUE;
					}
					break;
				case VALUE_CELLRANGE:
				case VALUE_ARRAY:
					type = NULL; /* FIXME */
					break;
				}

				if (NULL != type && *type)
					gsf_xml_out_add_cstr_unchecked (xml, "t", type);

				if (gnm_cell_has_expr (cell)) {
					texpr = cell->base.texpr;
					if (!gnm_expr_top_is_array_elem (texpr, NULL, NULL)) {
						gsf_xml_out_start_element (xml, "f");

						array = gnm_expr_top_get_array_corner (texpr);
						if (NULL != array) {
							GnmRange r;
							range_init_cellpos_size (&r, &cell->pos,
								array->cols, array->rows);
							gsf_xml_out_add_cstr_unchecked (xml, "t", "array");
							xlsx_add_range (xml, "ref", &r);
						}
						content = gnm_expr_top_as_string (cell->base.texpr,
							parse_pos_init_cell (&pp, cell), state->convs);
						gsf_xml_out_add_cstr (xml, NULL, content);
						g_free (content);

						gsf_xml_out_end_element (xml); /* </f> */
					}
				}

				if (inlineStr) {
					PangoAttrList *attrs = VALUE_FMT (val)
						? (PangoAttrList *)go_format_get_markup (VALUE_FMT (val))
						: NULL;

					gsf_xml_out_start_element (xml, "is");
					xlsx_write_rich_text (xml,
							      value_peek_string (val),
							      attrs,
							      FALSE);
					gsf_xml_out_end_element (xml); /* </is> */
				} else if (type) {
					gsf_xml_out_start_element (xml, "v");
					if (str_id >= 0)
						gsf_xml_out_add_int (xml, NULL, str_id);
					else if (VALUE_IS_BOOLEAN (val))
						xlsx_add_bool (xml, NULL, value_get_as_int (val));
					else {
						GString *str = g_string_new (NULL);
						value_get_as_gstring (cell->value, str, state->convs);
						gsf_xml_out_add_cstr (xml, NULL, str->str);
						g_string_free (str, TRUE);
					}
					gsf_xml_out_end_element (xml); /* </v> */
				}

				gsf_xml_out_end_element (xml); /* </c> */
			} else if (style_id > -1) {
				xlsx_write_init_row (&needs_row, xml, r, cheesy_span);
				gsf_xml_out_start_element (xml, "c");
				gsf_xml_out_add_cstr_unchecked (xml, "r",
								cell_coord_name (c, r));
				gsf_xml_out_add_int (xml, "s", style_id);
				gsf_xml_out_end_element (xml); /* </c> */
			}
		}
		if (!needs_row)
			gsf_xml_out_end_element (xml); /* </row> */
	}
	gsf_xml_out_end_element (xml); /* </sheetData> */
	g_free (non_defaults_rows);
	g_free (boring_count);
	g_ptr_array_free (all_cells, TRUE);
	g_free (cheesy_span);
}

static void
xlsx_write_merges (XLSXWriteState *state, GsfXMLOut *xml)
{
	GSList *ptr;

	if (NULL != (ptr = state->sheet->list_merged)) {
		gsf_xml_out_start_element (xml, "mergeCells");
		for (; ptr != NULL ; ptr = ptr->next) {
			gsf_xml_out_start_element (xml, "mergeCell");
			xlsx_add_range (xml, "ref", ptr->data);
			gsf_xml_out_end_element (xml); /* </mergeCell> */
		}
		gsf_xml_out_end_element (xml); /* </mergeCells> */
	}
}

static void
xlsx_write_cond_rule (XLSXWriteState *state, GsfXMLOut *xml,
		      GnmStyleCond *cond, GnmParsePos const *pp)
{
	int i, n;
	const char *type = NULL;
	const char *operator = NULL;
	GnmExprTop const *alt_texpr;
	gboolean needs_alt_texpr = FALSE;

	gsf_xml_out_start_element (xml, "cfRule");
	switch (cond->op) {
	case GNM_STYLE_COND_BETWEEN:
		n = 2; operator = "between";
		break;
	case GNM_STYLE_COND_NOT_BETWEEN:
		n = 2; operator = "notBetween";
		break;
	case GNM_STYLE_COND_EQUAL:
		n = 1; operator = "equal";
		break;
	case GNM_STYLE_COND_NOT_EQUAL:
		n = 1; operator = "notEqual";
		break;
	case GNM_STYLE_COND_GT:
		n = 1; operator = "greaterThan";
		break;
	case GNM_STYLE_COND_LT:
		n = 1; operator = "lessThan";
		break;
	case GNM_STYLE_COND_GTE:
		n = 1; operator = "greaterThanOrEqual";
		break;
	case GNM_STYLE_COND_LTE:
		n = 1; operator = "lessThanOrEqual";
		break;
	case GNM_STYLE_COND_CONTAINS_BLANKS:
		needs_alt_texpr = TRUE;
		n = 1; type = "containsBlanks";
		break;
	case GNM_STYLE_COND_NOT_CONTAINS_BLANKS:
		needs_alt_texpr = TRUE;
		n = 1; type = "notContainsBlanks";
		break;
	case GNM_STYLE_COND_CONTAINS_ERR:
		needs_alt_texpr = TRUE;
		n = 1; type = "containsErrors";
		break;
	case GNM_STYLE_COND_NOT_CONTAINS_ERR:
		needs_alt_texpr = TRUE;
		n = 1; type = "notContainsErrors";
		break;
	case GNM_STYLE_COND_CUSTOM:
		n = 1; type = "expression";
		break;
	case GNM_STYLE_COND_CONTAINS_STR:
		needs_alt_texpr = TRUE;
		n = 1; type = "containsText";
		break;
	case GNM_STYLE_COND_NOT_CONTAINS_STR:
		needs_alt_texpr = TRUE;
		n = 1; type = "notContainsText";
		break;
	case GNM_STYLE_COND_BEGINS_WITH_STR:
		needs_alt_texpr = TRUE;
		n = 1; type = "beginsWith";
		break;
	case GNM_STYLE_COND_ENDS_WITH_STR:
		needs_alt_texpr = TRUE;
		n = 1; type = "endsWith";
		break;

	case GNM_STYLE_COND_NOT_BEGINS_WITH_STR:
	case GNM_STYLE_COND_NOT_ENDS_WITH_STR:
		needs_alt_texpr = TRUE;
		n = 1; type = "expression";
		break;

	default:
		g_assert_not_reached ();
	}

	alt_texpr = needs_alt_texpr
		? gnm_style_cond_get_alternate_expr (cond)
		: NULL;

	gsf_xml_out_add_cstr_unchecked (xml, "type", type ? type : "cellIs");
	gsf_xml_out_add_int (xml, "dxfId",
			     xlsx_get_cond_style_id (state, cond->overlay));
	gsf_xml_out_add_int (xml, "priority", 1) ;
	gsf_xml_out_add_int (xml, "stopIfTrue", 1) ;
	if (operator)
		gsf_xml_out_add_cstr_unchecked (xml, "operator", operator);
	for (i = 0; i < n; i++) {
		GnmExprTop const *texpr = alt_texpr
			? alt_texpr
			: gnm_style_cond_get_expr (cond, i);
		char *str = gnm_expr_top_as_string (texpr, pp, state->convs);
		gsf_xml_out_simple_element (xml, "formula", str);
		g_free (str);
	}
	if (alt_texpr)
		gnm_expr_top_unref (alt_texpr);
	gsf_xml_out_end_element (xml); /* </cfRule> */
}


static void
xlsx_write_conditional_formatting (XLSXWriteState *state, GsfXMLOut *xml)
{
	GnmStyleList *cond_styles =
		sheet_style_collect_conditions (state->sheet, NULL);
	GnmStyleList *l;
	if (!cond_styles)
		return;

	for (l = cond_styles; l; l = l->next) {
		GnmStyleRegion const *sr = l->data;
		GnmStyleConditions *sc = gnm_style_get_conditions (sr->style);
		GPtrArray const *conds = gnm_style_conditions_details (sc);
		unsigned ui;
		GnmParsePos pp;

		if (!conds)
			continue;

		parse_pos_init (&pp, NULL, state->sheet,
				sr->range.start.col, sr->range.start.row);
		gsf_xml_out_start_element (xml, "conditionalFormatting");
		xlsx_add_range (xml, "sqref", &sr->range);
		for (ui = 0; ui < conds->len; ui++) {
			GnmStyleCond *cond = g_ptr_array_index (conds, ui);
			xlsx_write_cond_rule (state, xml, cond, &pp);
		}
		gsf_xml_out_end_element (xml); /* </conditionalFormatting> */
	}

	style_list_free (cond_styles);
}


static void
xlsx_write_validation_expr (XLSXClosure *info, GnmCellPos const *pos,
			    char const *elem, GnmExprTop const *texpr)
{
	if (NULL != texpr) {
		GnmParsePos pp;
		char *str = gnm_expr_top_as_string (texpr,
			parse_pos_init (&pp, NULL, (Sheet *)info->state->sheet,
					pos->col, pos->row),
			info->state->convs);
		gsf_xml_out_simple_element (info->xml, elem, str);
		g_free (str);
	}
}

static void
xlsx_write_validation (XLValInputPair const *vip, G_GNUC_UNUSED gpointer dummy, XLSXClosure *info)
{
#if 0
	/* Get docs on this */
	"imeMode" default="noControl"
		"noControl"
		"off"
		"on"
		"disabled"
		"hiragana"
		"fullKatakana"
		"halfKatakana"
		"fullAlpha"
		"halfAlpha"
		"fullHangul"
		"halfHangul"
#endif
	char const *tmp;

	gsf_xml_out_start_element (info->xml, "dataValidation");

	if (NULL != vip->v) {
		tmp = NULL;
		switch (vip->v->type) {
		default : /* fall back to the default */
		case GNM_VALIDATION_TYPE_ANY : /* the default "none" */  break;
		case GNM_VALIDATION_TYPE_AS_INT :		tmp = "whole"; break;
		case GNM_VALIDATION_TYPE_AS_NUMBER :	tmp = "decimal"; break;
		case GNM_VALIDATION_TYPE_IN_LIST :		tmp = "list"; break;
		case GNM_VALIDATION_TYPE_AS_DATE :		tmp = "date"; break;
		case GNM_VALIDATION_TYPE_AS_TIME :		tmp = "time"; break;
		case GNM_VALIDATION_TYPE_TEXT_LENGTH :	tmp = "textLength"; break;
		case GNM_VALIDATION_TYPE_CUSTOM :		tmp = "custom"; break;
		}
		if (NULL != tmp)
			gsf_xml_out_add_cstr_unchecked (info->xml, "type", tmp);

		tmp = NULL;
		switch (vip->v->op) {
		default : /* fall back to the default */
		case GNM_VALIDATION_OP_BETWEEN :	/* the default "between" */ break;
		case GNM_VALIDATION_OP_NOT_BETWEEN: tmp = "notBetween"; break;
		case GNM_VALIDATION_OP_EQUAL :	tmp = "equal"; break;
		case GNM_VALIDATION_OP_NOT_EQUAL :	tmp = "notEqual"; break;
		case GNM_VALIDATION_OP_LT :		tmp = "lessThan"; break;
		case GNM_VALIDATION_OP_GT :		tmp = "greaterThan"; break;
		case GNM_VALIDATION_OP_LTE :	tmp = "lessThanOrEqual"; break;
		case GNM_VALIDATION_OP_GTE :	tmp = "greaterThanOrEqual"; break;
		}
		if (NULL != tmp)
			gsf_xml_out_add_cstr_unchecked (info->xml, "operator", tmp);

		tmp = NULL;
		switch (vip->v->style) {
		default : /* fall back to the default */
		case GNM_VALIDATION_STYLE_STOP : /* "stop" the default */ break;
		case GNM_VALIDATION_STYLE_WARNING : tmp = "warning"; break;
		case GNM_VALIDATION_STYLE_INFO : tmp = "information"; break;
		}
		if (NULL != tmp)
			gsf_xml_out_add_cstr_unchecked (info->xml, "errorStyle", tmp);

		if (vip->v->allow_blank)
			xlsx_add_bool (info->xml, "allowBlank", TRUE);
		if (vip->v->use_dropdown)
			xlsx_add_bool (info->xml, "showDropDown", TRUE);

		if (NULL != vip->v->title)
			gsf_xml_out_add_cstr (info->xml, "errorTitle", vip->v->title->str);
		if (NULL != vip->v->msg)
			gsf_xml_out_add_cstr (info->xml, "error", vip->v->msg->str);
	}

	/* ?? Always TRUE but not the default ?? */
	xlsx_add_bool (info->xml, "showInputMessage", TRUE);
	xlsx_add_bool (info->xml, "showErrorMessage", TRUE);

	if (NULL != vip->msg) {
		char const *str;
		if (NULL != (str = gnm_input_msg_get_title (vip->msg)))
			gsf_xml_out_add_cstr (info->xml, "promptTitle", str);
		if (NULL != (str = gnm_input_msg_get_msg (vip->msg)))
			gsf_xml_out_add_cstr (info->xml, "prompt", str);
	}

	xlsx_add_range_list (info->xml, "sqref", vip->ranges);

	if (NULL != vip->v) {
		GnmRange const *first = vip->ranges->data;
		xlsx_write_validation_expr (info, &first->start,
			"formula1", vip->v->deps[0].texpr);
		xlsx_write_validation_expr (info, &first->start,
			"formula2", vip->v->deps[1].texpr);
	}

	gsf_xml_out_end_element (info->xml); /*  </dataValidation> */
}

static void
xlsx_write_validations (XLSXWriteState *state, GsfXMLOut *xml, G_GNUC_UNUSED GnmRange const *extent)
{
	GnmStyleList *validations = sheet_style_collect_validations (state->sheet, NULL);

	if (NULL != validations) {
		XLSXClosure info = { state, xml };
		/* filter on logical max, not extent.  XL allows validations
		 * past the stated dimension */
		GHashTable *group = excel_collect_validations (validations,
			XLSX_MAX_COLS, XLSX_MAX_ROWS);

		gsf_xml_out_start_element (xml, "dataValidations");
		gsf_xml_out_add_int (xml, "count", g_hash_table_size (group)) ;
		g_hash_table_foreach (group, (GHFunc) xlsx_write_validation, &info);
		gsf_xml_out_end_element (xml); /*  </dataValidations> */

		g_hash_table_destroy (group);
		style_list_free (validations);
	}
}

static void
xlsx_write_hlink (GnmHLink const *link, GSList *ranges, XLSXClosure *info)
{
	gchar const *target = gnm_hlink_get_target (link);
	gchar const *location = NULL;
	gchar const *rid = NULL;
	gchar const *tip;
	GType const t = G_OBJECT_TYPE (link);

	if (t == gnm_hlink_url_get_type () ||
	    t == gnm_hlink_email_get_type ()) {
		rid = gsf_outfile_open_pkg_add_extern_rel (
			GSF_OUTFILE_OPEN_PKG (gsf_xml_out_get_output (info->xml)),
			target, ns_rel_hlink);
	} else if (t != gnm_hlink_cur_wb_get_type ())
		return;

	for (; ranges  != NULL ; ranges = ranges->next) {
		gsf_xml_out_start_element (info->xml, "hyperlink");
		xlsx_add_range (info->xml, "ref", ranges->data);

		if (t == gnm_hlink_cur_wb_get_type ())
			gsf_xml_out_add_cstr (info->xml, "location", target);
		else if (NULL != rid)
			gsf_xml_out_add_cstr (info->xml, "r:id", rid);
		if (NULL != location)
			gsf_xml_out_add_cstr (info->xml, "tooltip", location);
		if (NULL != (tip = gnm_hlink_get_tip (link)))
			gsf_xml_out_add_cstr (info->xml, "tooltip", tip);
		gsf_xml_out_end_element (info->xml); /*  </hyperlink> */
	}
}

static void
xlsx_write_hlinks (XLSXWriteState *state, GsfXMLOut *xml, G_GNUC_UNUSED GnmRange const *extent)
{
	GnmStyleList *hlinks = sheet_style_collect_hlinks (state->sheet, NULL);

	if (NULL != hlinks) {
		XLSXClosure info = { state, xml };
		/* filter on logical max, not extent.  XL allows validations
		 * past the stated dimension */
		GHashTable *group = excel_collect_hlinks (hlinks,
			XLSX_MAX_COLS, XLSX_MAX_ROWS);

		gsf_xml_out_start_element (xml, "hyperlinks");
		g_hash_table_foreach (group, (GHFunc) xlsx_write_hlink, &info);
		gsf_xml_out_end_element (xml); /*  </hyperlinks> */

		g_hash_table_destroy (group);
		style_list_free (hlinks);
	}
}

static void
xlsx_write_col (XLSXWriteState *state, GsfXMLOut *xml,
		ColRowInfo const *ci, int first, int last,
		GnmStyle *style)
{
	double const def_width = state->sheet->cols.default_style.size_pts;
	gint style_id = xlsx_get_style_id (state, style);

	gsf_xml_out_start_element (xml, "col");
	gsf_xml_out_add_int (xml, "min", first + 1);
	gsf_xml_out_add_int (xml, "max", last + 1);
	gsf_xml_out_add_int (xml, "style", style_id);

	if (ci) {
		gsf_xml_out_add_float (xml, "width",
				       ci->size_pts / ((130. / 18.5703125) * (72./96.)), 7);
		if (!ci->visible)
			gsf_xml_out_add_cstr_unchecked (xml, "hidden", "1");
		if (ci->hard_size)
			gsf_xml_out_add_cstr_unchecked (xml, "customWidth", "1");
		else if (fabs (def_width - ci->size_pts) > .1) {
			gsf_xml_out_add_cstr_unchecked (xml, "bestFit", "1");
			gsf_xml_out_add_cstr_unchecked (xml, "customWidth", "1");
		}

		if (ci->outline_level > 0)
			gsf_xml_out_add_int (xml, "outlineLevel",
					     ci->outline_level);
		if (ci->is_collapsed)
			gsf_xml_out_add_cstr_unchecked (xml, "collapsed", "1");
	}

	gsf_xml_out_end_element (xml); /* </col> */
}

static void
xlsx_write_cols (XLSXWriteState *state, GsfXMLOut *xml, GnmStyle **styles)
{
	int first_col = 0, i;
	int last_col = gnm_sheet_get_last_col (state->sheet);
	ColRowInfo const *info = sheet_col_get (state->sheet, first_col);

	gsf_xml_out_start_element (xml, "cols");

	for (i = first_col + 1; i <= last_col; i++) {
		ColRowInfo const *ci = sheet_col_get (state->sheet, i);
		if (!colrow_equal (info, ci) || styles[i] != styles[i - 1]) {
			xlsx_write_col (state, xml, info,
					first_col, i - 1,
					styles[i - 1]);
			info	  = ci;
			first_col = i;
		}
	}
	xlsx_write_col (state, xml, info,
			first_col, i - 1,
			styles[i - 1]);

	gsf_xml_out_end_element (xml); /* </cols> */
}

static void
xlsx_write_autofilters (XLSXWriteState *state, GsfXMLOut *xml)
{
	GnmFilter const *filter;
	unsigned i;

	if (NULL == state->sheet->filters)
		return;

	/* We write only the first filter per sheet.  */
	filter = state->sheet->filters->data;

	gsf_xml_out_start_element (xml, "autoFilter");
	xlsx_add_range (xml, "ref", &filter->r);

	for (i = 0; i < filter->fields->len ; i++) {
		GnmFilterCondition const *cond =
			gnm_filter_get_condition (filter, i);

		/* filter unused or bucket filters in excel5 */
		if (!cond || cond->op[0] == GNM_FILTER_UNUSED)
			continue;

		gsf_xml_out_start_element (xml, "filterColumn");
		gsf_xml_out_add_int (xml, "colId", i);

		switch (cond->op[0]) {
		case GNM_FILTER_OP_EQUAL:
		case GNM_FILTER_OP_GT:
		case GNM_FILTER_OP_LT:
		case GNM_FILTER_OP_GTE:
		case GNM_FILTER_OP_LTE:
		case GNM_FILTER_OP_NOT_EQUAL: {
			static const char *const opname[6] = {
				"equal", "greaterThan", "lessThan",
				"greaterThanOrEqual", "lessThanOrEqual",
				"notEqual"
			};
			unsigned oi, N;

			for (oi = N = 1; oi < G_N_ELEMENTS (cond->op); oi++) {
				if (cond->op[oi] == GNM_FILTER_UNUSED)
					continue;
				N++;
			}

			gsf_xml_out_start_element (xml, "customFilters");
			if (N > 1)
				gsf_xml_out_add_cstr_unchecked (xml, "and", "true");
			for (oi = 0; oi < G_N_ELEMENTS (cond->op); oi++) {
				GnmFilterOp op = cond->op[oi];
				GString *str;

				if (op == GNM_FILTER_UNUSED)
					continue;
				gsf_xml_out_start_element (xml, "customFilter");
				if (op >= GNM_FILTER_OP_EQUAL && op <= GNM_FILTER_OP_NOT_EQUAL)
					gsf_xml_out_add_cstr_unchecked (xml, "operator", opname[op]);

				str = g_string_new (NULL);
				value_get_as_gstring (cond->value[oi], str, state->convs);
				gsf_xml_out_add_cstr (xml, "val", str->str);
				g_string_free (str, TRUE);
				gsf_xml_out_end_element (xml); /* </customFilter> */
			}
			gsf_xml_out_end_element (xml); /* </customFilters> */
			break;
		}

		case GNM_FILTER_OP_BLANKS :
			gsf_xml_out_start_element (xml, "filters");
			xlsx_add_bool (xml, "blank", TRUE);
			gsf_xml_out_end_element (xml); /* </filters> */
			break;
		case GNM_FILTER_OP_NON_BLANKS :
			gsf_xml_out_start_element (xml, "customFilters");
			gsf_xml_out_start_element (xml, "customFilter");
			gsf_xml_out_add_cstr_unchecked (xml, "operator", "notEqual");
			gsf_xml_out_add_cstr (xml, "val", " ");
			gsf_xml_out_end_element (xml); /* </customFilter> */
			gsf_xml_out_end_element (xml); /* </customFilters> */
			break;

		case GNM_FILTER_OP_TOP_N:
		case GNM_FILTER_OP_BOTTOM_N:
		case GNM_FILTER_OP_TOP_N_PERCENT:
		case GNM_FILTER_OP_BOTTOM_N_PERCENT:
			gsf_xml_out_start_element (xml, "top10");
			gsf_xml_out_add_float (xml, "val", cond->count, -1);
			if (cond->op[0] & GNM_FILTER_OP_BOTTOM_MASK)
				gsf_xml_out_add_cstr_unchecked (xml, "top", "0");
			if (cond->op[0] & GNM_FILTER_OP_PERCENT_MASK)
				gsf_xml_out_add_cstr_unchecked (xml, "percent", "1");
			gsf_xml_out_end_element (xml); /* </top10> */
			break;

		default:
			continue;
		}

		gsf_xml_out_end_element (xml); /* </filterColumn> */
	}
	gsf_xml_out_end_element (xml); /* </autoFilter> */
}

static void
xlsx_write_protection (XLSXWriteState *state, GsfXMLOut *xml)
{
	gboolean sheet;
	gboolean objects;
	gboolean scenarios;
	gboolean formatCells;
	gboolean formatColumns;
	gboolean formatRows;
	gboolean insertColumns;
	gboolean insertRows;
	gboolean insertHyperlinks;
	gboolean deleteColumns;
	gboolean deleteRows;
	gboolean selectLockedCells;
	gboolean sort;
	gboolean autoFilter;
	gboolean pivotTables;
	gboolean selectUnlockedCells;

	g_object_get (G_OBJECT (state->sheet),
		"protected",				 &sheet,
		"protected-allow-edit-objects",		 &objects,
		"protected-allow-edit-scenarios",	 &scenarios,
		"protected-allow-cell-formatting",	 &formatCells,
		"protected-allow-column-formatting",	 &formatColumns,
		"protected-allow-row-formatting",	 &formatRows,
		"protected-allow-insert-columns",	 &insertColumns,
		"protected-allow-insert-rows",		 &insertRows,
		"protected-allow-insert-hyperlinks",	 &insertHyperlinks,
		"protected-allow-delete-columns",	 &deleteColumns,
		"protected-allow-delete-rows",		 &deleteRows,
		"protected-allow-select-locked-cells",	 &selectLockedCells,
		"protected-allow-sort-ranges",		 &sort,
		"protected-allow-edit-auto-filters",	 &autoFilter,
		"protected-allow-edit-pivottable",	 &pivotTables,
		"protected-allow-select-unlocked-cells", &selectUnlockedCells,
		NULL);

	gsf_xml_out_start_element (xml, "sheetProtection");
	if ( sheet)		  xlsx_add_bool (xml, "sheet",			TRUE);
	if ( objects)		  xlsx_add_bool (xml, "objects",		TRUE);
	if ( scenarios)		  xlsx_add_bool (xml, "scenarios",		TRUE);
	if (!formatCells)	  xlsx_add_bool (xml, "formatCells",		FALSE);
	if (!formatColumns)	  xlsx_add_bool (xml, "formatColumns",		FALSE);
	if (!formatRows)	  xlsx_add_bool (xml, "formatRows",		FALSE);
	if (!insertColumns)	  xlsx_add_bool (xml, "insertColumns",		FALSE);
	if (!insertRows)	  xlsx_add_bool (xml, "insertRows",		FALSE);
	if (!insertHyperlinks)	  xlsx_add_bool (xml, "insertHyperlinks",	FALSE);
	if (!deleteColumns)	  xlsx_add_bool (xml, "deleteColumns",		FALSE);
	if (!deleteRows)	  xlsx_add_bool (xml, "deleteRows",		FALSE);
	if ( selectLockedCells)	  xlsx_add_bool (xml, "selectLockedCells",	TRUE);
	if (!sort)		  xlsx_add_bool (xml, "sort",			FALSE);
	if (!autoFilter)	  xlsx_add_bool (xml, "autoFilter",		FALSE);
	if (!pivotTables)	  xlsx_add_bool (xml, "pivotTables",		FALSE);
	if ( selectUnlockedCells) xlsx_add_bool (xml, "selectUnlockedCells",	TRUE);

	gsf_xml_out_end_element (xml); /* sheetProtection */
}

static void
xlsx_write_breaks (G_GNUC_UNUSED XLSXWriteState *state, GsfXMLOut *xml, GnmPageBreaks *breaks)
{
	unsigned const maxima = (breaks->is_vert ? XLSX_MaxCol : XLSX_MaxRow) - 1;
	GArray const *details = breaks->details;
	GnmPageBreak const *binfo;
	unsigned i;

	gsf_xml_out_start_element (xml,
		(breaks->is_vert) ? "rowBreaks" : "colBreaks");
	gsf_xml_out_add_int (xml, "count", details->len);

	for (i = 0 ; i < details->len ; i++) {
		binfo = &g_array_index (details, GnmPageBreak, i);
		gsf_xml_out_start_element (xml, "brk");
		gsf_xml_out_add_int (xml, "id", binfo->pos);

		/* hard code min=0 max=dir */
		gsf_xml_out_add_int (xml, "max", maxima);

		switch (binfo->type) {
		case GNM_PAGE_BREAK_MANUAL :	gsf_xml_out_add_bool (xml, "man", TRUE); break;
		case GNM_PAGE_BREAK_AUTO :	break;
		case GNM_PAGE_BREAK_NONE :	break;
		case GNM_PAGE_BREAK_DATA_SLICE :gsf_xml_out_add_bool (xml, "pt", TRUE); break;
		}
		gsf_xml_out_end_element (xml); /* </brk> */
	}
	gsf_xml_out_end_element (xml);
}

static int
xlsx_find_paper_code (GtkPaperSize *psize)
{
	XLSXPaperDefs *paper_defs;
	XLSXPaperDefs paper[] =
		{{ 74 , 90 , 90 , 205 , GTK_UNIT_MM },
		 { 38 , 92 , 3.625 , 6.5 , GTK_UNIT_INCH },
		 { 94 , 97 , 97 , 151 , GTK_UNIT_MM },
		 /* { 95 , 97 , 97 , 151 , GTK_UNIT_MM }, */
		 { 37 , 98 , 3.875 , 7.5 , GTK_UNIT_INCH },
		 { 19 , 98 , 3.875 , 8.875 , GTK_UNIT_INCH },
		 { 96 , 102 , 102 , 165 , GTK_UNIT_MM },
		 { 97 , 102 , 102 , 176 , GTK_UNIT_MM },
		 { 20 , 104 , 4.125 , 9.5 , GTK_UNIT_INCH },
		 { 70 , 105 , 105 , 148 , GTK_UNIT_MM },
		 { 92 , 105 , 105 , 235 , GTK_UNIT_MM },
		 { 99 , 110 , 110 , 208 , GTK_UNIT_MM },
		 { 27 , 110 , 110 , 220 , GTK_UNIT_MM },
		 /* { 100 , 110 , 110 , 220 , GTK_UNIT_MM }, */
		 { 36 , 110 , 110 , 230 , GTK_UNIT_MM },
		 { 21 , 114 , 4.5 , 10.375 , GTK_UNIT_INCH },
		 { 31 , 114 , 114 , 162 , GTK_UNIT_MM },
		 { 32 , 114 , 114 , 229 , GTK_UNIT_MM },
		 { 22 , 120 , 4.75 , 11 , GTK_UNIT_INCH },
		 { 101 , 120 , 120 , 230 , GTK_UNIT_MM },
		 { 73 , 120 , 120 , 235 , GTK_UNIT_MM },
		 { 103 , 120 , 120 , 309 , GTK_UNIT_MM },
		 { 98 , 125 , 125 , 176 , GTK_UNIT_MM },
		 { 23 , 127 , 5 , 11.5 , GTK_UNIT_INCH },
		 { 88 , 128 , 128 , 182 , GTK_UNIT_MM },
		 { 6 , 139 , 5.5 , 8.5 , GTK_UNIT_INCH },
		 { 93 , 146 , 146 , 215 , GTK_UNIT_MM },
		 { 81 , 148 , 148 , 100 , GTK_UNIT_MM },
		 { 83 , 148 , 148 , 105 , GTK_UNIT_MM },
		 { 82 , 148 , 148 , 200 , GTK_UNIT_MM },
		 { 11 , 148 , 148 , 210 , GTK_UNIT_MM },
		 /* { 61 , 148 , 148 , 210 , GTK_UNIT_MM }, */
		 { 107 , 151 , 151 , 97 , GTK_UNIT_MM },
		 /* { 108 , 151 , 151 , 97 , GTK_UNIT_MM }, */
		 { 102 , 160 , 160 , 230 , GTK_UNIT_MM },
		 { 28 , 162 , 162 , 229 , GTK_UNIT_MM },
		 { 109 , 165 , 165 , 102 , GTK_UNIT_MM },
		 { 64 , 174 , 174 , 235 , GTK_UNIT_MM },
		 { 110 , 176 , 176 , 102 , GTK_UNIT_MM },
		 { 35 , 176 , 176 , 125 , GTK_UNIT_MM },
		 /* { 111 , 176 , 176 , 125 , GTK_UNIT_MM }, */
		 { 13 , 176 , 176 , 250 , GTK_UNIT_MM },
		 /* { 34 , 176 , 176 , 250 , GTK_UNIT_MM }, */
		 { 89 , 182 , 182 , 128 , GTK_UNIT_MM },
		 { 62 , 182 , 182 , 257 , GTK_UNIT_MM },
		 { 7 , 184 , 7.25 , 10.5 , GTK_UNIT_INCH },
		 { 43 , 200 , 200 , 148 , GTK_UNIT_MM },
		 /* { 69 , 200 , 200 , 148 , GTK_UNIT_MM }, */
		 { 65 , 201 , 201 , 276 , GTK_UNIT_MM },
		 { 87 , 205 , 205 , 90 , GTK_UNIT_MM },
		 { 112 , 208 , 208 , 110 , GTK_UNIT_MM },
		 { 54 , 210 , 8.275 , 11 , GTK_UNIT_INCH },
		 { 78 , 210 , 210 , 148 , GTK_UNIT_MM },
		 { 9 , 210 , 210 , 297 , GTK_UNIT_MM },
		 /* { 10 , 210 , 210 , 297 , GTK_UNIT_MM }, */
		 /* { 55 , 210 , 210 , 297 , GTK_UNIT_MM }, */
		 { 60 , 210 , 210 , 330 , GTK_UNIT_MM },
		 { 1 , 215 , 8.5 , 11 , GTK_UNIT_INCH },
		 /* { 2 , 215 , 8.5 , 11 , GTK_UNIT_INCH }, */
		 /* { 18 , 215 , 8.5 , 11 , GTK_UNIT_INCH }, */
		 { 40 , 215 , 8.5 , 12 , GTK_UNIT_INCH },
		 { 59 , 215 , 8.5 , 12.69 , GTK_UNIT_INCH },
		 { 14 , 215 , 8.5 , 13 , GTK_UNIT_INCH },
		 /* { 41 , 215 , 8.5 , 13 , GTK_UNIT_INCH }, */
		 { 5 , 215 , 8.5 , 14 , GTK_UNIT_INCH },
		 { 106 , 215 , 215 , 146 , GTK_UNIT_MM },
		 { 15 , 215 , 215 , 275 , GTK_UNIT_MM },
		 { 72 , 216 , 216 , 277 , GTK_UNIT_MM },
		 { 113 , 220 , 220 , 110 , GTK_UNIT_MM },
		 { 47 , 220 , 220 , 220 , GTK_UNIT_MM },
		 { 57 , 227 , 227 , 356 , GTK_UNIT_MM },
		 { 44 , 228 , 9 , 11 , GTK_UNIT_INCH },
		 { 30 , 229 , 229 , 324 , GTK_UNIT_MM },
		 /* { 104 , 229 , 229 , 324 , GTK_UNIT_MM }, */
		 { 114 , 230 , 230 , 120 , GTK_UNIT_MM },
		 { 115 , 230 , 230 , 160 , GTK_UNIT_MM },
		 { 50 , 235 , 9.275 , 12 , GTK_UNIT_INCH },
		 /* { 56 , 235 , 9.275 , 12 , GTK_UNIT_INCH }, */
		 { 51 , 235 , 9.275 , 15 , GTK_UNIT_INCH },
		 { 91 , 235 , 235 , 105 , GTK_UNIT_MM },
		 { 86 , 235 , 235 , 120 , GTK_UNIT_MM },
		 { 53 , 236 , 236 , 322 , GTK_UNIT_MM },
		 { 71 , 240 , 240 , 332 , GTK_UNIT_MM },
		 { 12 , 250 , 250 , 353 , GTK_UNIT_MM },
		 /* { 33 , 250 , 250 , 353 , GTK_UNIT_MM }, */
		 { 42 , 250 , 250 , 353 , GTK_UNIT_MM },
		 { 45 , 254 , 10 , 11 , GTK_UNIT_INCH },
		 { 16 , 254 , 10 , 14 , GTK_UNIT_INCH },
		 { 80 , 257 , 257 , 182 , GTK_UNIT_MM },
		 { 85 , 277 , 277 , 216 , GTK_UNIT_MM },
		 { 75 , 279 , 11 , 8.5 , GTK_UNIT_INCH },
		 { 3 , 279 , 11 , 17 , GTK_UNIT_INCH },
		 /* { 17 , 279 , 11 , 17 , GTK_UNIT_INCH }, */
		 { 52 , 296 , 11.69 , 18 , GTK_UNIT_INCH },
		 { 77 , 297 , 297 , 210 , GTK_UNIT_MM },
		 { 8 , 297 , 297 , 420 , GTK_UNIT_MM },
		 /* { 67 , 297 , 297 , 420 , GTK_UNIT_MM }, */
		 { 90 , 304 , 12 , 11 , GTK_UNIT_INCH },
		 { 58 , 305 , 305 , 487 , GTK_UNIT_MM },
		 { 116 , 309 , 309 , 120 , GTK_UNIT_MM },
		 { 63 , 322 , 322 , 445 , GTK_UNIT_MM },
		 /* { 68 , 322 , 322 , 445 , GTK_UNIT_MM }, */
		 { 117 , 324 , 324 , 229 , GTK_UNIT_MM },
		 { 29 , 324 , 324 , 458 , GTK_UNIT_MM },
		 /* { 105 , 324 , 324 , 458 , GTK_UNIT_MM }, */
		 { 84 , 332 , 332 , 240 , GTK_UNIT_MM },
		 { 79 , 364 , 364 , 257 , GTK_UNIT_MM },
		 { 39 , 377 , 14.875 , 11 , GTK_UNIT_INCH },
		 { 46 , 381 , 15 , 11 , GTK_UNIT_INCH },
		 { 76 , 420 , 420 , 297 , GTK_UNIT_MM },
		 { 66 , 420 , 420 , 594 , GTK_UNIT_MM },
		 { 4 , 431 , 17 , 11 , GTK_UNIT_INCH },
		 { 24 , 431 , 17 , 22 , GTK_UNIT_INCH },
		 { 118 , 458 , 458 , 324 , GTK_UNIT_MM },
		 { 25 , 558 , 22 , 34 , GTK_UNIT_INCH },
		 { 26 , 863 , 34 , 44 , GTK_UNIT_INCH },
		 {0,0,0,0,0 }};
	gchar const *psize_name;
	int width_mm;

	psize_name = gtk_paper_size_get_name (psize);
	if (0 == strcmp (psize_name, GTK_PAPER_NAME_LETTER))
		return 1;
	if (0 == strcmp (psize_name, GTK_PAPER_NAME_A4))
		return 9;
	if (0 == strcmp (psize_name, GTK_PAPER_NAME_A3))
		return 8;
	if (0 == strcmp (psize_name, GTK_PAPER_NAME_A5))
		return 11;
	if (0 == strcmp (psize_name, GTK_PAPER_NAME_B5))
		return 13;
	if (0 == strcmp (psize_name, GTK_PAPER_NAME_EXECUTIVE))
		return 7;
	if (0 == strcmp (psize_name, GTK_PAPER_NAME_LEGAL))
		return 5;

	width_mm = (int) gtk_paper_size_get_width (psize, GTK_UNIT_MM);
	
	for (paper_defs = paper; paper_defs->code > 0; paper_defs++) {
		if (width_mm < paper_defs->width_mm)
			return 0;
		if (width_mm == paper_defs->width_mm) {
			gdouble width = gtk_paper_size_get_width (psize,  paper_defs->unit);
			gdouble height = gtk_paper_size_get_height (psize,  paper_defs->unit);
			
			if (width == paper_defs->width && height == paper_defs->height)
				return paper_defs->code;
		}
	}
	return 0;
}

static void
xlsx_write_print_info_hf (XLSXWriteState *state, GsfXMLOut *xml,
			  const PrintHF *hf, const char *hftext)
{
	char *s = xls_header_footer_export (hf);

	gsf_xml_out_start_element (xml, hftext);
	gsf_xml_out_add_cstr (xml, NULL, s);
	gsf_xml_out_end_element (xml); /* hftext */

	g_free (s);
}

static void
xlsx_write_print_info (XLSXWriteState *state, GsfXMLOut *xml)
{
	PrintInformation *pi = state->sheet->print_info;
	double h_margin, f_margin;
	double left;
	double right;
	double t_margin, b_margin;

	g_return_if_fail (pi != NULL);

	gsf_xml_out_start_element (xml, "printOptions");
	gsf_xml_out_end_element (xml); /* </printOptions> */

	gsf_xml_out_start_element (xml, "pageMargins");
	print_info_get_margins (pi, &h_margin, &f_margin, &left, &right,
				&t_margin, &b_margin);
	gsf_xml_out_add_float (xml, "left",	left / 72., 4);
	gsf_xml_out_add_float (xml, "right",	right / 72., 4);
	gsf_xml_out_add_float (xml, "top",	t_margin / 72., 4);
	gsf_xml_out_add_float (xml, "bottom",	b_margin / 72., 4);
	gsf_xml_out_add_float (xml, "header",	h_margin / 72., 4);
	gsf_xml_out_add_float (xml, "footer",	f_margin / 72., 4);
	gsf_xml_out_end_element (xml); /* </pageMargins> */

	gsf_xml_out_start_element (xml, "pageSetup");

	xlsx_add_bool (xml, "blackAndWhite", pi->print_black_and_white);
	switch (pi->comment_placement) {
	case GNM_PRINT_COMMENTS_IN_PLACE:
		gsf_xml_out_add_cstr_unchecked (xml, "cellComments", "asDisplayed");
		break;
	case GNM_PRINT_COMMENTS_AT_END:
		gsf_xml_out_add_cstr_unchecked (xml, "cellComments", "atEnd");
		break;
	case GNM_PRINT_COMMENTS_NONE:
	default:
		gsf_xml_out_add_cstr_unchecked (xml, "cellComments", "none");
		break;
	}
	if (pi->n_copies > 0)
		gsf_xml_out_add_int (xml, "copies", pi->n_copies);
	xlsx_add_bool (xml, "draft", pi->print_as_draft);
	switch (pi->error_display) {
	case GNM_PRINT_ERRORS_AS_BLANK:
		gsf_xml_out_add_cstr_unchecked (xml, "errors", "blank");
		break;
	case GNM_PRINT_ERRORS_AS_DASHES:
		gsf_xml_out_add_cstr_unchecked (xml, "errors", "dash");
		break;
	case GNM_PRINT_ERRORS_AS_NA:
		gsf_xml_out_add_cstr_unchecked (xml, "errors", "NA");
		break;
	case GNM_PRINT_ERRORS_AS_DISPLAYED:
	default:
		gsf_xml_out_add_cstr_unchecked (xml, "errors", "displayed");
		break;
	}
	if (pi->start_page >= 0)
		gsf_xml_out_add_int (xml, "firstPageNumber", pi->start_page);
	if (pi->scaling.dim.rows > 0)
		gsf_xml_out_add_int (xml, "fitToHeight", pi->scaling.dim.rows);
	if (pi->scaling.dim.cols > 0)
		gsf_xml_out_add_int (xml, "fitToWidth", pi->scaling.dim.cols);
	/* horizontalDpi skipped */
	/* id skipped */

	if (pi->page_setup) {
		GtkPageOrientation orient;

		orient = gtk_page_setup_get_orientation (pi->page_setup);
		switch (orient) {
		case GTK_PAGE_ORIENTATION_PORTRAIT:
		case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
			gsf_xml_out_add_cstr_unchecked (xml, "orientation", "portrait");
			break;
		case GTK_PAGE_ORIENTATION_LANDSCAPE:
		case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
			gsf_xml_out_add_cstr_unchecked (xml, "orientation", "landscape");
			break;
		default:
			gsf_xml_out_add_cstr_unchecked (xml, "orientation", "default");
			break;
		}
	} else
		gsf_xml_out_add_cstr_unchecked (xml, "orientation", "default");

	gsf_xml_out_add_cstr_unchecked 
		(xml, "pageOrder", 
		 pi->print_across_then_down ? "overThenDown" : "downThenOver");
	
	if (pi->page_setup) {
		GtkPaperSize *psize;
		int paper_code;

		psize = gtk_page_setup_get_paper_size (pi->page_setup);
		paper_code = xlsx_find_paper_code (psize);

		if (paper_code > 0)
			gsf_xml_out_add_int (xml, "paperSize", paper_code);
		else {
			gdouble width = gtk_paper_size_get_width (psize, GTK_UNIT_POINTS);
			gdouble height = gtk_paper_size_get_height (psize, GTK_UNIT_POINTS);
			
			xlsx_add_pt (xml, "paperHeight", height);
			xlsx_add_pt (xml, "paperWidth", width);
		}
	}

	if (pi->scaling.percentage.x > 0) 
		gsf_xml_out_add_int (xml, "scale", 
				     (int)CLAMP (pi->scaling.percentage.x, 10, 400));
	xlsx_add_bool (xml, "useFirstPageNumber", (pi->start_page >= 0));
	/* usePrinterDefaults skipped */
	/* verticalDpi skipped */

	gsf_xml_out_end_element (xml); /* </pageSetup> */

	gsf_xml_out_start_element (xml, "headerFooter");
	xlsx_write_print_info_hf (state, xml, pi->header, "oddHeader");
	xlsx_write_print_info_hf (state, xml, pi->footer, "oddFooter");
	gsf_xml_out_end_element (xml); /* </headerFooter> */

	if (NULL != pi->page_breaks.v)
		xlsx_write_breaks (state, xml, pi->page_breaks.v);
	if (NULL != pi->page_breaks.h)
		xlsx_write_breaks (state, xml, pi->page_breaks.h);
}

/**********************************************************************/

static void
write_comment_author (gpointer key, G_GNUC_UNUSED gpointer value, GsfXMLOut *xml)
{
	gsf_xml_out_start_element (xml, "author");
	gsf_xml_out_add_cstr_unchecked (xml, NULL, (char const *) key);
	gsf_xml_out_end_element (xml);
}

static void
xlsx_write_comments (XLSXWriteState *state, GsfOutput *sheet_part, GSList *objects)
{
	GsfXMLOut *xml;
	GHashTable *authors;
	unsigned author = 0;
	char const *authorname;
	GSList *ptr;
	SheetObjectAnchor const *anchor;
	PangoAttrList *attrs;
	char *name = g_strdup_printf ("comments%u.xml", ++state->comment);
	GsfOutput *comments_part = gsf_outfile_new_child_full (state->xl_dir, name, FALSE,
		"content-type", "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
		NULL);
	g_free (name);
	gsf_outfile_open_pkg_relate (GSF_OUTFILE_OPEN_PKG (comments_part),
		GSF_OUTFILE_OPEN_PKG (sheet_part), ns_rel_com);
	xml = gsf_xml_out_new (comments_part);
	gsf_xml_out_start_element (xml, "comments");
	gsf_xml_out_add_cstr_unchecked (xml, "xmlns", ns_ss);
	/* search for comments authors */
	authors = g_hash_table_new (g_str_hash, g_str_equal);
	for (ptr = objects; ptr; ptr = ptr->next) {
		authorname = cell_comment_author_get (CELL_COMMENT (ptr->data));
		if (authorname != NULL && !g_hash_table_lookup_extended (authors, authorname, NULL, NULL))
			g_hash_table_insert (authors, (gpointer) authorname, GUINT_TO_POINTER (author++));
	}
	/* save authors */
	gsf_xml_out_start_element (xml, "authors");
	g_hash_table_foreach (authors, (GHFunc) write_comment_author, xml);
	gsf_xml_out_end_element (xml); /* </authors> */
	/* save comments */
	gsf_xml_out_start_element (xml, "commentList");
	for (ptr = objects; ptr; ptr = ptr->next) {
		gsf_xml_out_start_element (xml, "comment");
		anchor = sheet_object_get_anchor (ptr->data);
		gsf_xml_out_add_cstr_unchecked (xml, "ref", range_as_string (&anchor->cell_bound));
		authorname = cell_comment_author_get (CELL_COMMENT (ptr->data));
		if (authorname != NULL)
			gsf_xml_out_add_uint (xml, "authorId",
					      GPOINTER_TO_UINT (g_hash_table_lookup (authors, authorname)));
		gsf_xml_out_start_element (xml, "text");
		/* Save text as rich text */
		g_object_get (ptr->data, "text", &name, "markup", &attrs, NULL);
		if (name && *name)
			xlsx_write_rich_text (xml, name, attrs, TRUE);
		g_free (name);
		pango_attr_list_unref (attrs);
		gsf_xml_out_end_element (xml); /* </text> */
		gsf_xml_out_end_element (xml); /* </comment> */
	}
	gsf_xml_out_end_element (xml); /* </commentList> */
	g_hash_table_destroy (authors);
	gsf_xml_out_end_element (xml); /* </comments> */
	g_object_unref (xml);
	gsf_output_close (comments_part);
	g_object_unref (comments_part);
}

#include "xlsx-write-drawing.c"

static char const *
xlsx_write_sheet (XLSXWriteState *state, GsfOutfile *dir, GsfOutfile *wb_part, unsigned i)
{
	char *name = g_strdup_printf ("sheet%u.xml", i+1);
	GsfOutput *sheet_part = gsf_outfile_new_child_full (dir, name, FALSE,
		"content-type", "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
		NULL);
	char const *rId = gsf_outfile_open_pkg_relate (GSF_OUTFILE_OPEN_PKG (sheet_part),
		GSF_OUTFILE_OPEN_PKG (wb_part),
		"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet");
	GsfXMLOut *xml;
	GnmRange  extent, cell_extent;
	GSList   *charts;
	char const *chart_drawing_rel_id = NULL;
	GnmStyle **col_styles;
	PrintInformation *pi = NULL;

	state->sheet = workbook_sheet_by_index (state->base.wb, i);
	col_styles = sheet_style_most_common (state->sheet, TRUE);
	excel_sheet_extent (state->sheet, &extent, col_styles,
			    XLSX_MAX_COLS, XLSX_MAX_ROWS, state->io_context);
	cell_extent = sheet_get_cells_extent (state->sheet);
	extent = range_union (&extent, &cell_extent);

/*   comments   */
	charts = sheet_objects_get (state->sheet, NULL, CELL_COMMENT_TYPE);
	if (NULL != charts) {
		xlsx_write_comments (state, sheet_part, charts);
		g_slist_free (charts);
	}

/*   charts   */
	charts = sheet_objects_get (state->sheet, NULL, SHEET_OBJECT_GRAPH_TYPE);
	if (NULL != charts) {
		chart_drawing_rel_id = xlsx_write_objects (state, sheet_part, charts);
		g_slist_free (charts);
	}

	xml = gsf_xml_out_new (sheet_part);
/* CT_Worksheet =                                          */
	gsf_xml_out_start_element (xml, "worksheet");
	gsf_xml_out_add_cstr_unchecked (xml, "xmlns", ns_ss);
	gsf_xml_out_add_cstr_unchecked (xml, "xmlns:r", ns_rel);

	/*   element sheetPr { CT_SheetPr }?,     */
	gsf_xml_out_start_element (xml, "sheetPr");
	
	pi = state->sheet->print_info;
	if (pi != NULL) {
		gsf_xml_out_start_element (xml, "pageSetUpPr");
		xlsx_add_bool (xml, "fitToPage", pi->scaling.type == PRINT_SCALE_FIT_PAGES);
		gsf_xml_out_end_element (xml); /* </pageSetUpPr> */
	}

	if (NULL != state->sheet->tab_color) {
		gsf_xml_out_start_element (xml, "tabColor");
		xlsx_add_rgb (xml, "rgb", state->sheet->tab_color->go_color);
		gsf_xml_out_end_element (xml); /* </tabColor> */
	}
	gsf_xml_out_end_element (xml); /* </sheetPr> */
	
/*   element dimension { CT_SheetDimension }?,     */
	gsf_xml_out_start_element (xml, "dimension");
	xlsx_add_range (xml, "ref", &extent);
	gsf_xml_out_end_element (xml); /* </dimension> */
/*   element sheetViews { CT_SheetViews }?,     */
	gsf_xml_out_start_element (xml, "sheetViews");
	SHEET_FOREACH_VIEW (state->sheet, sv, xlsx_write_sheet_view (xml, sv););
	gsf_xml_out_end_element (xml); /* </sheetViews> */
/*   element sheetFormatPr { CT_SheetFormatPr }?,     */
	gsf_xml_out_start_element (xml, "sheetFormatPr");
	gsf_xml_out_add_float (xml, "defaultRowHeight",
		sheet_row_get_default_size_pts (state->sheet), 4);
	if (state->sheet->rows.max_outline_level > 0)
		gsf_xml_out_add_int (xml, "outlineLevelRow",
			state->sheet->rows.max_outline_level);
	if (state->sheet->cols.max_outline_level > 0)
		gsf_xml_out_add_int (xml, "outlineLevelCol",
			state->sheet->cols.max_outline_level);
	gsf_xml_out_end_element (xml); /* </sheetFormatPr> */
/*   element cols { CT_Cols }*,     */
	xlsx_write_cols (state, xml, col_styles);
/*   element sheetData { CT_SheetData },     */
	xlsx_write_cells (state, xml, &extent, col_styles);
/*   element sheetCalcPr { CT_SheetCalcPr }?,     */
/*   element sheetProtection { CT_SheetProtection }?,     */
	xlsx_write_protection (state, xml);
/*   element protectedRanges { CT_ProtectedRanges }?,     */
/*   element scenarios { CT_Scenarios }?,     */
/*   element autoFilter { CT_AutoFilter }?,     */
	xlsx_write_autofilters (state, xml);
/*   element sortState { CT_SortState }?,     */
/*   element dataConsolidate { CT_DataConsolidate }?,     */
/*   element customSheetViews { CT_CustomSheetViews }?,     */
/*   element mergeCells { CT_MergeCells }?,     */
	xlsx_write_merges (state, xml);
/*   element phoneticPr { CT_PhoneticPr }?,     */
/*   element conditionalFormatting { CT_ConditionalFormatting }*,     */
	xlsx_write_conditional_formatting (state, xml);
/*   element dataValidations { CT_DataValidations }?,     */
	xlsx_write_validations (state, xml, &extent);
/*   element hyperlinks { CT_Hyperlinks }?,     */
	xlsx_write_hlinks (state, xml, &extent);
/*   element printOptions { CT_PrintOptions }?, included in xlsx_write_print_info */
/*   element pageMargins { CT_PageMargins }?,   included in xlsx_write_print_info */
/*   element pageSetup { CT_PageSetup }?,       included in xlsx_write_print_info */
/*   element headerFooter { CT_HeaderFooter }?, included in xlsx_write_print_info */
	xlsx_write_print_info (state, xml);
/*   element rowBreaks { CT_PageBreak }?,     */
/*   element colBreaks { CT_PageBreak }?,     */
/*   element customProperties { CT_CustomProperties }?,     */
/*   element cellWatches { CT_CellWatches }?,     */
/*   element ignoredErrors { CT_IgnoredErrors }?,     */
/*   element smartTags { CT_SmartTags }?,     */
/*   element drawing { CT_Drawing }?,     */
	if (NULL != chart_drawing_rel_id) {
		gsf_xml_out_start_element (xml, "drawing");
		gsf_xml_out_add_cstr_unchecked (xml, "r:id", chart_drawing_rel_id);
		gsf_xml_out_end_element (xml);  /* </drawing> */
	}
/*   element legacyDrawing { CT_LegacyDrawing }?,  Deleted in edition 2   */
/*   element legacyDrawingHF { CT_LegacyDrawing }?,  Deleted in edition 2   */
/*   element picture { CT_SheetBackgroundPicture }?,     */
/*   element oleObjects { CT_OleObjects }?,     */
/*   element controls { CT_Controls }?,     */
/*   element webPublishItems { CT_WebPublishItems }?,     */
/*   element tableParts { CT_TableParts }?,     */
/*   element extLst { CT_ExtensionList }?     */
	gsf_xml_out_end_element (xml); /* </worksheet> */

	g_object_unref (xml);
	gsf_output_close (sheet_part);
	g_object_unref (sheet_part);
	g_free (name);
	g_free (col_styles);

	state->sheet = NULL;

	return rId;
}

static void
xlsx_write_named_expression (G_GNUC_UNUSED gpointer key, GnmNamedExpr *nexpr, XLSXClosure *closure)
{
	char *formula;

	g_return_if_fail (nexpr != NULL);
	if (!expr_name_is_active (nexpr))
		return;

	gsf_xml_out_start_element (closure->xml, "definedName");

	if (nexpr->is_permanent) {
		char const *expr_name = expr_name_name (nexpr);
		if (0 == strcmp (expr_name, "Print_Area"))
			gsf_xml_out_add_cstr (closure->xml, "name", "_xlnm.Print_Area");
		else if (0 == strcmp (expr_name, "Sheet_Title"))
			gsf_xml_out_add_cstr (closure->xml, "name", "_xlnm.Sheet_Title");
		else
			gsf_xml_out_add_cstr (closure->xml, "name", expr_name);
	} else {
		gsf_xml_out_add_cstr (closure->xml, "name", expr_name_name (nexpr));
	}
	if (nexpr->pos.sheet != NULL)
		gsf_xml_out_add_int (closure->xml, "localSheetId",
				     nexpr->pos.sheet->index_in_wb);

	formula = expr_name_as_string (nexpr, NULL, closure->state->convs);
	gsf_xml_out_add_cstr (closure->xml, NULL, formula);
	g_free (formula);

	gsf_xml_out_end_element (closure->xml);
}

static void
xlsx_write_definedNames (XLSXWriteState *state, GsfXMLOut *xml)
{
	XLSXClosure closure = {state, xml};

	gsf_xml_out_start_element (xml, "definedNames");
	workbook_foreach_name
		(state->base.wb, FALSE,
		 (GHFunc)&xlsx_write_named_expression, &closure);
	gsf_xml_out_end_element (xml);
}

static void
xlsx_write_calcPR (XLSXWriteState *state, GsfXMLOut *xml)
{
	Workbook const *wb = state->base.wb;

#warning Filter by defaults
	gsf_xml_out_start_element (xml, "calcPr");

	gsf_xml_out_add_cstr_unchecked (xml, "calcMode",
		wb->recalc_auto ? "auto" : "manual");

	xlsx_add_bool (xml, "iterate", wb->iteration.enabled);
	gsf_xml_out_add_int (xml, "iterateCount",
		wb->iteration.max_number);
	gsf_xml_out_add_float (xml, "iterateDelta",
		wb->iteration.tolerance, -1);

	gsf_xml_out_end_element (xml);
}

#include "xlsx-write-pivot.c"

#include "xlsx-write-docprops.c"

static gboolean
rich_value_equal (GnmValue const *a, GnmValue const *b)
{
	return value_equal (a, b) &&
		go_format_eq (VALUE_FMT (a), VALUE_FMT (b));
}

static guint
rich_value_hash (GnmValue const *v)
{
	return value_hash (v) ^ GPOINTER_TO_UINT (VALUE_FMT (v));
}

static void
xlsx_write_workbook (XLSXWriteState *state, GsfOutfile *root_part)
{
	int i;
	GsfXMLOut  *xml;
	GSList	   *cacheRefs;
	GPtrArray  *sheetIds  = g_ptr_array_new ();
	GsfOutfile *xl_dir    = (GsfOutfile *)gsf_outfile_new_child (root_part, "xl", TRUE);
	GsfOutfile *sheet_dir = (GsfOutfile *)gsf_outfile_new_child (xl_dir, "worksheets", TRUE);
	GsfOutfile *wb_part   = (GsfOutfile *)gsf_outfile_open_pkg_add_rel (xl_dir, "workbook.xml",
		"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
		root_part,
		"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument");
	GnmStyle *style = gnm_style_new_default ();

	state->xl_dir = xl_dir;
	state->shared_string_hash = g_hash_table_new_full
		((GHashFunc)rich_value_hash, (GEqualFunc)rich_value_equal,
		 (GDestroyNotify)value_release, NULL);
	state->shared_string_array = g_ptr_array_new ();
	state->styles_hash = g_hash_table_new_full
		(gnm_style_hash, (GEqualFunc)gnm_style_equal,
		 (GDestroyNotify)gnm_style_unref, NULL);
	state->styles_array = g_ptr_array_new ();
	state->dxfs_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
	state->dxfs_array = g_ptr_array_new ();

	xlsx_get_style_id (state, style);
	gnm_style_unref (style);

	state->convs	 = xlsx_conventions_new (TRUE);
	state->chart.dir   = state->drawing.dir   = NULL;
	state->chart.count = state->drawing.count = 0;

	g_ptr_array_set_size (sheetIds, workbook_sheet_count (state->base.wb));
	for (i = 0 ; i < workbook_sheet_count (state->base.wb); i++)
		g_ptr_array_index (sheetIds, i) =
			(gpointer) xlsx_write_sheet (state, sheet_dir, wb_part, i);

	xlsx_write_shared_strings (state, wb_part);
	xlsx_write_styles (state, wb_part);
	xlsx_write_docprops (state, root_part);
	cacheRefs = xlsx_write_pivots (state, wb_part);

	xml = gsf_xml_out_new (GSF_OUTPUT (wb_part));
	gsf_xml_out_start_element (xml, "workbook");
	gsf_xml_out_add_cstr_unchecked (xml, "xmlns", ns_ss);
	gsf_xml_out_add_cstr_unchecked (xml, "xmlns:r", ns_rel);
	/* Note the schema does not allow the attribute xml:space */

	gsf_xml_out_start_element (xml, "fileVersion");
	gsf_xml_out_add_int (xml, "lastEdited", 4);
	gsf_xml_out_add_int (xml, "lowestEdited", 4);
	gsf_xml_out_add_int (xml, "rupBuild", 3820);
	gsf_xml_out_end_element (xml);

	gsf_xml_out_start_element (xml, "workbookPr");
	gsf_xml_out_add_int (xml, "date1904",
			     workbook_date_conv (state->base.wb)->use_1904
			     ? 1 : 0);
	gsf_xml_out_end_element (xml);

	gsf_xml_out_start_element (xml, "bookViews");
	WORKBOOK_FOREACH_VIEW (state->base.wb, view, {
		int scale = 10;  /* Guess */
		gsf_xml_out_start_element (xml, "workbookView");
		gsf_xml_out_add_int (xml, "activeTab",
			view->current_sheet->index_in_wb);
		if (view->preferred_width > 0)
			gsf_xml_out_add_int (xml, "windowWidth", view->preferred_width * scale);
		if (view->preferred_height > 0)
			gsf_xml_out_add_int (xml, "windowHeight", view->preferred_height * scale);
		gsf_xml_out_end_element (xml);
	});
	gsf_xml_out_end_element (xml);

	gsf_xml_out_start_element (xml, "sheets");
	for (i = 0 ; i < workbook_sheet_count (state->base.wb); i++) {
		Sheet const *sheet = workbook_sheet_by_index (state->base.wb, i);
		gsf_xml_out_start_element (xml, "sheet");
		gsf_xml_out_add_cstr (xml, "name", sheet->name_unquoted);
		gsf_xml_out_add_int (xml, "sheetId", i+1);	/* FIXME What is this ?? */
		gsf_xml_out_add_cstr_unchecked (xml, "r:id",
			g_ptr_array_index (sheetIds, i));
		gsf_xml_out_end_element (xml); /* </sheet> */
	}
	gsf_xml_out_end_element (xml); /* </sheets> */

	xlsx_write_definedNames (state, xml);

	xlsx_write_calcPR (state, xml);

	if (NULL != cacheRefs) {
		GSList *ptr;
		unsigned int i = 0;
		gsf_xml_out_start_element (xml, "pivotCaches");
		for (ptr = cacheRefs ; ptr != NULL ; ptr = ptr->next) {
			gsf_xml_out_start_element (xml, "pivotCache");
			gsf_xml_out_add_int (xml, "cacheId", i++);
			gsf_xml_out_add_cstr_unchecked (xml, "r:id", ptr->data);
			gsf_xml_out_end_element (xml); /* </pivotCache> */
		}
		gsf_xml_out_end_element (xml); /* </pivotCaches> */
	}
	gsf_xml_out_start_element (xml, "webPublishing");
	xlsx_add_bool (xml, "allowPng", TRUE);
	xlsx_add_bool (xml, "css", FALSE);
	if (state->version == ECMA_376_2006)
		gsf_xml_out_add_int (xml, "codePage", 1252); /* FIXME : Use utf-8 ? */
	else
		gsf_xml_out_add_cstr_unchecked (xml, "characterSet", "UTF-8");
	gsf_xml_out_end_element (xml);

	gsf_xml_out_end_element (xml); /* </workbook> */
	g_object_unref (xml);

	xlsx_conventions_free (state->convs);
	g_hash_table_destroy (state->shared_string_hash);
	g_ptr_array_free (state->shared_string_array, TRUE);
	g_hash_table_destroy (state->styles_hash);
	g_ptr_array_free (state->styles_array, TRUE);
	g_hash_table_destroy (state->dxfs_hash);
	g_ptr_array_free (state->dxfs_array, TRUE);

	if (NULL != state->chart.dir)
		gsf_output_close (GSF_OUTPUT (state->chart.dir));
	if (NULL != state->drawing.dir)
		gsf_output_close (GSF_OUTPUT (state->drawing.dir));
	gsf_output_close (GSF_OUTPUT (wb_part));
	g_ptr_array_free (sheetIds, TRUE);
	gsf_output_close (GSF_OUTPUT (sheet_dir));
	gsf_output_close (GSF_OUTPUT (xl_dir));
}

G_MODULE_EXPORT void
xlsx_file_save (GOFileSaver const *fs, GOIOContext *io_context,
		gconstpointer wb_view, GsfOutput *output);
void
xlsx_file_save (G_GNUC_UNUSED GOFileSaver const *fs, GOIOContext *io_context,
		gconstpointer wb_view, GsfOutput *output)
{
	XLSXWriteState state;
	GsfOutfile *root_part;
	GnmLocale  *locale;

	locale = gnm_push_C_locale ();

	state.version           = ECMA_376_2006;
	state.io_context	= io_context;
	state.base.wb		= wb_view_get_workbook (wb_view);
	state.comment		= 0;
	root_part = gsf_outfile_open_pkg_new (
		gsf_outfile_zip_new (output, NULL));

	xlsx_write_workbook (&state, root_part);
	gsf_output_close (GSF_OUTPUT (root_part));
	g_object_unref (root_part);

	gnm_pop_C_locale (locale);
}

G_MODULE_EXPORT void
xlsx2_file_save (GOFileSaver const *fs, GOIOContext *io_context,
		gconstpointer wb_view, GsfOutput *output);
void
xlsx2_file_save (G_GNUC_UNUSED GOFileSaver const *fs, GOIOContext *io_context,
		gconstpointer wb_view, GsfOutput *output)
{
	XLSXWriteState state;
	GsfOutfile *root_part;
	GnmLocale  *locale;

	locale = gnm_push_C_locale ();
	state.version           = ECMA_376_2008;
	state.io_context	= io_context;
	state.base.wb		= wb_view_get_workbook (wb_view);
	state.comment		= 0;
	root_part = gsf_outfile_open_pkg_new (
		gsf_outfile_zip_new (output, NULL));

	xlsx_write_workbook (&state, root_part);
	gsf_output_close (GSF_OUTPUT (root_part));
	g_object_unref (root_part);

	gnm_pop_C_locale (locale);
}