jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
/*******************************************************************************
 * You may amend and distribute as you like, but don't remove this header!
 *
 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
 * See http://www.codeplex.com/EPPlus for details.
 *
 * Copyright (C) 2011  Jan Källman
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Lesser General Public License for more details.
 *
 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
 *
 * All code and executables are provided "as is" with no warranty either express or implied. 
 * The author accepts no liability for any damage or loss of business that this product may cause.
 *
 * Code change notes:
 * 
 * Author                            Change                        Date
 * ******************************************************************************
 * Jan Källman            Initial Release                2010-01-28
 * Jan Källman            License changed GPL-->LGPL  2011-12-27
 * Eyal Seagull            Conditional Formatting      2012-04-03
 *******************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using HH.WMS.Utils.EPPlus.Style;
using System.Xml;
using System.Drawing;
using System.Globalization;
using System.Collections;
using HH.WMS.Utils.EPPlus.Table;
using System.Text.RegularExpressions;
using System.IO;
using System.Linq;
using HH.WMS.Utils.EPPlus.DataValidation;
using HH.WMS.Utils.EPPlus.DataValidation.Contracts;
using System.Reflection;
using HH.WMS.Utils.EPPlus.Style.XmlAccess;
using System.Security;
using HH.WMS.Utils.EPPlus.ConditionalFormatting;
using HH.WMS.Utils.EPPlus.ConditionalFormatting.Contracts;
 
namespace HH.WMS.Utils.EPPlus
{
    /// <summary>
    /// A range of cells 
    /// </summary>
    public class ExcelRangeBase : ExcelAddress, IExcelCell, IDisposable, IEnumerable<ExcelRangeBase>, IEnumerator<ExcelRangeBase>
    {
        /// <summary>
        /// Reference to the worksheet
        /// </summary>
        protected ExcelWorksheet _worksheet;
        private ExcelWorkbook _workbook = null;
        private delegate void _changeProp(_setValue method, object value);
        private delegate void _setValue(object value, int row, int col);
        private _changeProp _changePropMethod;
        private int _styleID;
        #region Constructors
        internal ExcelRangeBase(ExcelWorksheet xlWorksheet)
        {
            _worksheet = xlWorksheet;
            _ws = _worksheet.Name;
            this.AddressChange += new EventHandler(ExcelRangeBase_AddressChange);
            SetDelegate();
        }
 
        void ExcelRangeBase_AddressChange(object sender, EventArgs e)
        {
            SetDelegate();
        }
        internal ExcelRangeBase(ExcelWorksheet xlWorksheet, string address) :
            base(xlWorksheet == null ? "" : xlWorksheet.Name, address)
        {
            _worksheet = xlWorksheet;
            if (string.IsNullOrEmpty(_ws)) _ws = _worksheet == null ? "" : _worksheet.Name;
            this.AddressChange += new EventHandler(ExcelRangeBase_AddressChange);
            SetDelegate();
        }
        internal ExcelRangeBase(ExcelWorkbook wb, ExcelWorksheet xlWorksheet, string address, bool isName) :
            base(xlWorksheet == null ? "" : xlWorksheet.Name, address, isName)
        {
            _worksheet = xlWorksheet;
            _workbook = wb;
            if (string.IsNullOrEmpty(_ws)) _ws = (xlWorksheet == null ? null : xlWorksheet.Name);
            this.AddressChange += new EventHandler(ExcelRangeBase_AddressChange);
            SetDelegate();
        }
        ~ExcelRangeBase()
        {
            this.AddressChange -= new EventHandler(ExcelRangeBase_AddressChange);
        }
        #endregion
        #region Set Value Delegates        
        private void SetDelegate()
        {
            if (_fromRow == -1)
            {
                _changePropMethod = SetUnknown;
            }
            //Single cell
            else if (_fromRow == _toRow && _fromCol == _toCol && Addresses == null)
            {
                _changePropMethod = SetSingle;
            }
            //Range (ex A1:A2)
            else if (Addresses == null)
            {
                _changePropMethod = SetRange;
            }
            //Multi Range (ex A1:A2,C1:C2)
            else
            {
                _changePropMethod = SetMultiRange;
            }
        }
        /// <summary>
        /// We dont know the address yet. Set the delegate first time a property is set.
        /// </summary>
        /// <param name="valueMethod"></param>
        /// <param name="value"></param>
        private void SetUnknown(_setValue valueMethod, object value)
        {
            //Address is not set use, selected range
            if (_fromRow == -1)
            {
                SetToSelectedRange();
            }
            SetDelegate();
            _changePropMethod(valueMethod, value);
        }
        /// <summary>
        /// Set a single cell
        /// </summary>
        /// <param name="valueMethod"></param>
        /// <param name="value"></param>
        private void SetSingle(_setValue valueMethod, object value)
        {
            valueMethod(value, _fromRow, _fromCol);
        }
        /// <summary>
        /// Set a range
        /// </summary>
        /// <param name="valueMethod"></param>
        /// <param name="value"></param>
        private void SetRange(_setValue valueMethod, object value)
        {
            SetValueAddress(this, valueMethod, value);
        }
        /// <summary>
        /// Set a multirange (A1:A2,C1:C2)
        /// </summary>
        /// <param name="valueMethod"></param>
        /// <param name="value"></param>
        private void SetMultiRange(_setValue valueMethod, object value)
        {
            SetValueAddress(this, valueMethod, value);
            foreach (var address in Addresses)
            {
                SetValueAddress(address, valueMethod, value);
            }
        }
        /// <summary>
        /// Set the property for an address
        /// </summary>
        /// <param name="address"></param>
        /// <param name="valueMethod"></param>
        /// <param name="value"></param>
        private void SetValueAddress(ExcelAddress address, _setValue valueMethod, object value)
        {
            IsRangeValid("");
            if (_fromRow == 1 && _fromCol == 1 && _toRow == ExcelPackage.MaxRows && _toCol == ExcelPackage.MaxColumns)  //Full sheet (ex ws.Cells.Value=0). Set value for A1 only to avoid hanging 
            {
                throw (new ArgumentException("Can't reference all cells. Please use the indexer to set the range"));
            }
            else
            {
                for (int col = address.Start.Column; col <= address.End.Column; col++)
                {
                    for (int row = address.Start.Row; row <= address.End.Row; row++)
                    {
                        valueMethod(value, row, col);
                    }
                }
            }
        }
        #endregion
        #region Set property methods
        private void Set_StyleID(object value, int row, int col)
        {
            _worksheet.Cell(row, col).StyleID = (int)value;
        }
        private void Set_StyleName(object value, int row, int col)
        {
            _worksheet.Cell(row, col).SetNewStyleName(value.ToString(), _styleID);
        }
        private void Set_Value(object value, int row, int col)
        {
            ExcelCell c = _worksheet.Cell(row, col);
            if (c._sharedFormulaID > 0) SplitFormulas();
            _worksheet.Cell(row, col).Value = value;
        }
        private void Set_Formula(object value, int row, int col)
        {
            ExcelCell c = _worksheet.Cell(row, col);
            if (c._sharedFormulaID > 0) SplitFormulas();
 
            string formula = (value == null ? string.Empty : value.ToString());
            if (formula == string.Empty)
            {
                c.Formula = string.Empty;
            }
            else
            {
                if (formula[0] == '=') value = formula.Substring(1, formula.Length - 1); // remove any starting equalsign.
                c.Formula = formula;
            }
        }
        /// <summary>
        /// Handles shared formulas
        /// </summary>
        /// <param name="value">The  formula</param>
        /// <param name="address">The address of the formula</param>
        /// <param name="IsArray">If the forumla is an array formula.</param>
        private void Set_SharedFormula(string value, ExcelAddress address, bool IsArray)
        {
            if (_fromRow == 1 && _fromCol == 1 && _toRow == ExcelPackage.MaxRows && _toCol == ExcelPackage.MaxColumns)  //Full sheet (ex ws.Cells.Value=0). Set value for A1 only to avoid hanging 
            {
                throw (new InvalidOperationException("Can't set a formula for the entire worksheet"));
            }
            else if (address.Start.Row == address.End.Row && address.Start.Column == address.End.Column && !IsArray)             //is it really a shared formula? Arrayformulas can be one cell only
            {
                //Nope, single cell. Set the formula
                Set_Formula(value, address.Start.Row, address.Start.Column);
                return;
            }
            //RemoveFormuls(address);
            CheckAndSplitSharedFormula();
            ExcelWorksheet.Formulas f = new ExcelWorksheet.Formulas();
            f.Formula = value;
            f.Index = _worksheet.GetMaxShareFunctionIndex(IsArray);
            f.Address = address.FirstAddress;
            f.StartCol = address.Start.Column;
            f.StartRow = address.Start.Row;
            f.IsArray = IsArray;
 
            _worksheet._sharedFormulas.Add(f.Index, f);
            _worksheet.Cell(address.Start.Row, address.Start.Column).SharedFormulaID = f.Index;
            _worksheet.Cell(address.Start.Row, address.Start.Column).Formula = value;
 
            for (int col = address.Start.Column; col <= address.End.Column; col++)
            {
                for (int row = address.Start.Row; row <= address.End.Row; row++)
                {
                    _worksheet.Cell(row, col).SharedFormulaID = f.Index;
                }
            }
        }
        private void Set_HyperLink(object value, int row, int col)
        {
            _worksheet.Cell(row, col).Hyperlink = value as Uri;
        }
        private void Set_IsRichText(object value, int row, int col)
        {
            _worksheet.Cell(row, col).IsRichText = (bool)value;
        }
        private void Exists_Comment(object value, int row, int col)
        {
            ulong cellID = GetCellID(_worksheet.SheetID, row, col);
            if (_worksheet.Comments._comments.ContainsKey(cellID))
            {
                throw (new InvalidOperationException(string.Format("Cell {0} already contain a comment.", new ExcelCellAddress(row, col).Address)));
            }
 
        }
        private void Set_Comment(object value, int row, int col)
        {
            string[] v = (string[])value;
            Worksheet.Comments.Add(new ExcelRangeBase(_worksheet, GetAddress(_fromRow, _fromCol)), v[0], v[1]);
            //   _worksheet.Cell(row, col).Comment = comment;
        }
        #endregion
        private void SetToSelectedRange()
        {
            if (_worksheet.View.SelectedRange == "")
            {
                Address = "A1";
            }
            else
            {
                Address = _worksheet.View.SelectedRange;
            }
        }
        private void IsRangeValid(string type)
        {
            if (_fromRow <= 0)
            {
                if (_address == "")
                {
                    SetToSelectedRange();
                }
                else
                {
                    if (type == "")
                    {
                        throw (new InvalidOperationException(string.Format("Range is not valid for this operation: {0}", _address)));
                    }
                    else
                    {
                        throw (new InvalidOperationException(string.Format("Range is not valid for {0} : {1}", type, _address)));
                    }
                }
            }
        }
        #region Public Properties
        /// <summary>
        /// The styleobject for the range.
        /// </summary>
        public ExcelStyle Style
        {
            get
            {
                IsRangeValid("styling");
                return _worksheet.Workbook.Styles.GetStyleObject(_worksheet.Cell(_fromRow, _fromCol).StyleID, _worksheet.PositionID, Address);
            }
        }
        /// <summary>
        /// The named style
        /// </summary>
        public string StyleName
        {
            get
            {
                IsRangeValid("styling");
                return _worksheet.Cell(_fromRow, _fromCol).StyleName;
            }
            set
            {
                _styleID = _worksheet.Workbook.Styles.GetStyleIdFromName(value);
                if (_fromRow == 1 && _toRow == ExcelPackage.MaxRows)    //Full column
                {
                    ExcelColumn column;
                    //Get the startcolumn
                    ulong colID = ExcelColumn.GetColumnID(_worksheet.SheetID, _fromCol);
                    if (!_worksheet._columns.ContainsKey(colID))
                    {
                        column = _worksheet.Column(_fromCol);
                    }
                    else
                    {
                        column = _worksheet._columns[colID] as ExcelColumn;
                    }
 
                    var index = _worksheet._columns.IndexOf(colID);
                    while (column.ColumnMin <= _toCol)
                    {
                        if (column.ColumnMax > _toCol)
                        {
                            var newCol = _worksheet.CopyColumn(column, _toCol + 1, column.ColumnMax);
                            column.ColumnMax = _toCol;
                        }
 
                        column._styleName = value;
                        column._styleID = _styleID;
 
                        index++;
                        if (index >= _worksheet._columns.Count)
                        {
                            break;
                        }
                        else
                        {
                            column = (_worksheet._columns[index] as ExcelColumn);
                        }
                    }
 
                    if (column._columnMax < _toCol)
                    {
                        var newCol = _worksheet.Column(column._columnMax + 1) as ExcelColumn;
                        newCol._columnMax = _toCol;
 
                        newCol._styleID = _styleID;
                        newCol._styleName = value;
                    }
                    if (_fromCol == 1 && _toCol == ExcelPackage.MaxColumns) //FullRow
                    {
                        foreach(ExcelRow row in _worksheet._rows)
                        {
                            row._styleName = value;
                            row._styleId = _styleID;
                        }
                    }
                }
                else if (_fromCol == 1 && _toCol == ExcelPackage.MaxColumns) //FullRow
                {
                    for (int row = _fromRow; row <= _toRow; row++)
                    {
                        _worksheet.Row(row)._styleName = value;
                        _worksheet.Row(row)._styleId = _styleID;
                    }
                }
 
                if (!((_fromRow == 1 && _toRow == ExcelPackage.MaxRows) || (_fromCol == 1 && _toCol == ExcelPackage.MaxColumns))) //Cell specific
                {
                    for (int col = _fromCol; col <= _toCol; col++)
                    {
                        for (int row = _fromRow; row <= _toRow; row++)
                        {
                            _worksheet.Cell(row, col).StyleName = value;
                        }
                    }
                }
                else //Only set name on created cells. (uncreated cells is set on full row or full column).
                {
                    int tempIndex = _index;
                    var e = this as IEnumerator;
                    e.Reset();
                    while (e.MoveNext())
                    {
                        ((ExcelCell)_worksheet._cells[_index]).SetNewStyleName(value, _styleID);
                    }
                    _index = tempIndex;
                }
                //_changePropMethod(Set_StyleName, value);
            }
        }
        /// <summary>
        /// The style ID. 
        /// It is not recomended to use this one. Use Named styles as an alternative.
        /// If you do, make sure that you use the Style.UpdateXml() method to update any new styles added to the workbook.
        /// </summary>
        public int StyleID
        {
            get
            {
                return _worksheet.Cell(_fromRow, _fromCol).StyleID;
            }
            set
            {
                _changePropMethod(Set_StyleID, value);
            }
        }
        /// <summary>
        /// Set the range to a specific value
        /// </summary>
        public object Value
        {
            get
            {
                if (IsName)
                {
                    if (_worksheet == null)
                    {
                        return _workbook._names[_address].NameValue;
                    }
                    else
                    {
                        return _worksheet.Names[_address].NameValue; ;
                    }
                }
                else
                {
                    if (_fromRow == _toRow && _fromCol == _toCol)
                    {
                        return _worksheet.GetValue(_fromRow, _fromCol);
                    }
                    else
                    {
                        return GetValueArray();
                    }
                }
            }
            set
            {
                if (IsName)
                {
                    if (_worksheet == null)
                    {
                        _workbook._names[_address].NameValue = value;
                    }
                    else
                    {
                        _worksheet.Names[_address].NameValue = value;
                    }
                }
                else
                {
                    _changePropMethod(Set_Value, value);
                }
            }
        }
 
        private bool IsInfinityValue(object value)
        {
            double? valueAsDouble = value as double?;
 
            if(valueAsDouble.HasValue && 
                (double.IsNegativeInfinity(valueAsDouble.Value) || double.IsPositiveInfinity(valueAsDouble.Value)))
            {
                return true;
            }
 
            return false;
        }
 
        private object GetValueArray()
        {
            ExcelAddressBase addr;
            if (_fromRow == 1 && _fromCol == 1 && _toRow == ExcelPackage.MaxRows && _toCol == ExcelPackage.MaxColumns)
            {
                addr = _worksheet.Dimension;
                if (addr == null) return null;
            }
            else
            {
                addr = this;
            }
            object[,] v = new object[addr._toRow - addr._fromRow + 1, addr._toCol - addr._fromCol + 1];
 
            for (int col = addr._fromCol; col <= addr._toCol; col++)
            {
                for (int row = addr._fromRow; row <= addr._toRow; row++)
                {
                    if (_worksheet._cells.ContainsKey(GetCellID(_worksheet.SheetID, row, col)))
                    {
                        if (IsRichText)
                        {
                            v[row - addr._fromRow, col - addr._fromCol] = GetRichText(row, col).Text;
                        }
                        else
                        {
                            v[row - addr._fromRow, col - addr._fromCol] = _worksheet.Cell(row, col).Value;
                        }
                    }
                }
            }
            return v;
        }
        private ExcelAddressBase GetAddressDim(ExcelRangeBase addr)
        {
            int fromRow, fromCol, toRow, toCol;
            var d = _worksheet.Dimension;
            fromRow = addr._fromRow < d._fromRow ? d._fromRow : addr._fromRow;
            fromCol = addr._fromCol < d._fromCol ? d._fromCol : addr._fromCol;
 
            toRow = addr._toRow > d._toRow ? d._toRow : addr._toRow;
            toCol = addr._toCol > d._toCol ? d._toCol : addr._toCol;
 
            if (addr._fromCol == fromRow && addr._fromCol == addr._fromCol && addr._toRow == toRow && addr._toCol == _toCol)
            {
                return addr;
            }
            else
            {
                if (_fromRow > _toRow || _fromCol > _toCol)
                {
                    return null;
                }
                else
                {
                    return new ExcelAddressBase(fromRow, fromCol, toRow, toCol);
                }
            }
        }
 
        private object GetSingleValue()
        {
            if (IsRichText)
            {
                return RichText.Text;
            }
            else
            {
                return _worksheet.Cell(_fromRow, _fromCol).Value;
            }
        }
        /// <summary>
        /// Returns the formatted value.
        /// </summary>
        public string Text
        {
            get
            {
                return GetFormatedText(false);
            }
        }
        /// <summary>
        /// Set the column width from the content of the range. The minimum width is the value of the ExcelWorksheet.defaultColumnWidth property.
        /// Note: Cells containing formulas are ignored since EPPlus don't have a calculation engine.
        /// Wrapped and merged cells are also ignored.
        /// </summary>
        public void AutoFitColumns()
        {
            AutoFitColumns(_worksheet.DefaultColWidth);
        }
 
        /// <summary>
        /// Set the column width from the content of the range.
        /// Note: Cells containing formulas are ignored since EPPlus don't have a calculation engine.
        ///       Wrapped and merged cells are also ignored.
        /// </summary>
        /// <remarks>This method will not work if you run in an environment that does not support GDI</remarks>
        /// <param name="MinimumWidth">Minimum column width</param>
        public void AutoFitColumns(double MinimumWidth)
        {
            AutoFitColumns(MinimumWidth, double.MaxValue);
        }
 
        /// <summary>
        /// Set the column width from the content of the range.
        /// Note: Cells containing formulas are ignored since EPPlus don't have a calculation engine.
        ///       Wrapped and merged cells are also ignored.
        /// </summary>
        /// <param name="MinimumWidth">Minimum column width</param>
        /// <param name="MaximumWidth">Maximum column width</param>
        public void AutoFitColumns(double MinimumWidth, double MaximumWidth)
        {
            if (_fromCol < 1 || _fromRow < 1)
            {
                SetToSelectedRange();
            }
            Dictionary<int, Font> fontCache = new Dictionary<int, Font>();
            Font f;
 
            bool doAdjust = _worksheet._package.DoAdjustDrawings;
            _worksheet._package.DoAdjustDrawings = false;
            var drawWidths = _worksheet.Drawings.GetDrawingWidths();
 
            int fromCol = _fromCol > _worksheet.Dimension._fromCol ? _fromCol : _worksheet.Dimension._fromCol;
            int toCol = _toCol < _worksheet.Dimension._toCol ? _toCol : _worksheet.Dimension._toCol;
            if (Addresses == null)
            {
                for (int col = fromCol; col <= toCol; col++)
                {
                    _worksheet.Column(col).Width = MinimumWidth;
                }
            }
            else
            {
                foreach (var addr in Addresses)
                {
                    fromCol = addr._fromCol > _worksheet.Dimension._fromCol ? addr._fromCol : _worksheet.Dimension._fromCol;
                    toCol = addr._toCol < _worksheet.Dimension._toCol ? addr._toCol : _worksheet.Dimension._toCol;
                    for (int col = fromCol; col <= toCol; col++)
                    {
                        _worksheet.Column(col).Width = MinimumWidth;
                    }
                }
            }
 
            //Get any autofilter to widen these columns
            List<ExcelAddressBase> afAddr = new List<ExcelAddressBase>();
            if (_worksheet.AutoFilterAddress != null)
            {
                afAddr.Add(new ExcelAddressBase(_worksheet.AutoFilterAddress._fromRow,
                                                                                _worksheet.AutoFilterAddress._fromCol,
                                                                                _worksheet.AutoFilterAddress._fromRow,
                                                                                _worksheet.AutoFilterAddress._toCol));
                afAddr[afAddr.Count - 1]._ws = WorkSheet;
            }
            foreach (var tbl in _worksheet.Tables)
            {
                if (tbl.AutoFilterAddress != null)
                {
                    afAddr.Add(new ExcelAddressBase(tbl.AutoFilterAddress._fromRow,
                                                                            tbl.AutoFilterAddress._fromCol,
                                                                            tbl.AutoFilterAddress._fromRow,
                                                                            tbl.AutoFilterAddress._toCol));
                    afAddr[afAddr.Count - 1]._ws = WorkSheet;
                }
            }
 
            var styles = _worksheet.Workbook.Styles;
            var nf = styles.Fonts[styles.CellXfs[0].FontId];
            FontStyle fs = FontStyle.Regular;
            if (nf.Bold) fs |= FontStyle.Bold;
            if (nf.UnderLine) fs |= FontStyle.Underline;
            if (nf.Italic) fs |= FontStyle.Italic;
            if (nf.Strike) fs |= FontStyle.Strikeout;
            var nfont = new Font(nf.Name, nf.Size, fs);
            
            using (Bitmap b = new Bitmap(1, 1))
            {
                using (Graphics g = Graphics.FromImage(b))
                {
                    float normalSize = (float)Math.Truncate(g.MeasureString("00", nfont).Width - g.MeasureString("0", nfont).Width);
                    g.PageUnit = GraphicsUnit.Pixel;
                    foreach (var cell in this)
                    {
                        if (cell.Merge == true || cell.Style.WrapText) continue;
                        var fntID = styles.CellXfs[cell.StyleID].FontId;
                        if (fontCache.ContainsKey(fntID))
                        {
                            f = fontCache[fntID];
                        }
                        else
                        {
                            var fnt = styles.Fonts[fntID];
                            fs = FontStyle.Regular;
                            if (fnt.Bold) fs |= FontStyle.Bold;
                            if (fnt.UnderLine) fs |= FontStyle.Underline;
                            if (fnt.Italic) fs |= FontStyle.Italic;
                            if (fnt.Strike) fs |= FontStyle.Strikeout;
                            f = new Font(fnt.Name, fnt.Size, fs);
                            fontCache.Add(fntID, f);
                        }
 
                        //Truncate(({pixels}-5)/{Maximum Digit Width} * 100+0.5)/100
 
                        var size = g.MeasureString(cell.TextForWidth, f);
                        double width;
                        double r = styles.CellXfs[cell.StyleID].TextRotation;
                        if (r <= 0 )
                        {
                            width = (size.Width + 5) / normalSize;
                        }
                        else
                        {
                            r = (r <= 90 ? r : r - 90);
                            width = (((size.Width - size.Height) * Math.Abs(System.Math.Cos(System.Math.PI * r / 180.0)) + size.Height) + 5) / normalSize;
                        }
 
                        foreach (var a in afAddr)
                        {
                            if (a.Collide(cell) != eAddressCollition.No)
                            {
                                width += 2.25;
                                break;
                            }
                        }
 
                        if (width > _worksheet.Column(cell._fromCol).Width)
                        {
                            _worksheet.Column(cell._fromCol).Width = width > MaximumWidth ? MaximumWidth : width;
                        }
                    }
                }
            }
            _worksheet.Drawings.AdjustWidth(drawWidths);
            _worksheet._package.DoAdjustDrawings = doAdjust;
        }
 
        internal string TextForWidth
        {
            get
            {
                return GetFormatedText(true);
            }
        }
        private string GetFormatedText(bool forWidthCalc)
        {
            object v = Value;
            if (v == null) return "";
            var styles = Worksheet.Workbook.Styles;
            var nfID = styles.CellXfs[StyleID].NumberFormatId;
            ExcelNumberFormatXml.ExcelFormatTranslator nf = null;
            for (int i = 0; i < styles.NumberFormats.Count; i++)
            {
                if (nfID == styles.NumberFormats[i].NumFmtId)
                {
                    nf = styles.NumberFormats[i].FormatTranslator;
                    break;
                }
            }
 
            string format, textFormat;
            if (forWidthCalc)
            {
                format = nf.NetFormatForWidth;
                textFormat = nf.NetTextFormatForWidth;
            }
            else
            {
                format = nf.NetFormat;
                textFormat = nf.NetTextFormat;
            }
 
            if (v is decimal || v.GetType().IsPrimitive)
            {
                double d;
                try
                {
                    d = Convert.ToDouble(v);
                }
                catch
                {
                    return "";
                }
 
                if (nf.DataType == ExcelNumberFormatXml.eFormatType.Number)
                {
                    if (string.IsNullOrEmpty(nf.FractionFormat))
                    {
                        return d.ToString(format, nf.Culture);
                    }
                    else
                    {
                        return nf.FormatFraction(d);
                    }
                }
                else if (nf.DataType == ExcelNumberFormatXml.eFormatType.DateTime)
                {
                    var date = DateTime.FromOADate(d);
                    return date.ToString(format, nf.Culture);
                }
            }
            else if (v is DateTime)
            {
                if (nf.DataType == ExcelNumberFormatXml.eFormatType.DateTime)
                {
                    return ((DateTime)v).ToString(format, nf.Culture);
                }
                else
                {
                    double d = ((DateTime)v).ToOADate();
                    if (string.IsNullOrEmpty(nf.FractionFormat))
                    {
                        return d.ToString(format, nf.Culture);
                    }
                    else
                    {
                        return nf.FormatFraction(d);
                    }
                }
            }
            else if (v is TimeSpan)
            {
                if (nf.DataType == ExcelNumberFormatXml.eFormatType.DateTime)
                {
                    return new DateTime(((TimeSpan)v).Ticks).ToString(format, nf.Culture);
                }
                else
                {
                    double d = (new DateTime(((TimeSpan)v).Ticks)).ToOADate();
                    if (string.IsNullOrEmpty(nf.FractionFormat))
                    {
                        return d.ToString(format, nf.Culture);
                    }
                    else
                    {
                        return nf.FormatFraction(d);
                    }
                }
            }
            else
            {
                if (textFormat == "")
                {
                    return v.ToString();
                }
                else
                {
                    return string.Format(textFormat, v);
                }
            }
            return v.ToString();
        }
        /// <summary>
        /// Gets or sets a formula for a range.
        /// </summary>
        public string Formula
        {
            get
            {
                if (IsName)
                {
                    if (_worksheet == null)
                    {
                        return _workbook._names[_address].NameFormula;
                    }
                    else
                    {
                        return _worksheet.Names[_address].NameFormula;
                    }
                }
                else
                {
                    return _worksheet.Cell(_fromRow, _fromCol).Formula;
                }
            }
            set
            {
                if (IsName)
                {
                    if (_worksheet == null)
                    {
                        _workbook._names[_address].NameFormula = value;
                    }
                    else
                    {
                        _worksheet.Names[_address].NameFormula = value;
                    }
                }
                else
                {
                    if (_fromRow == _toRow && _fromCol == _toCol)
                    {
                        Set_Formula(value, _fromRow, _fromCol);
                    }
                    else
                    {
                        Set_SharedFormula(value, this, false);
                        if (Addresses != null)
                        {
                            foreach (var address in Addresses)
                            {
                                Set_SharedFormula(value, address, false);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Gets or Set a formula in R1C1 format.
        /// </summary>
        public string FormulaR1C1
        {
            get
            {
                IsRangeValid("FormulaR1C1");
                return _worksheet.Cell(_fromRow, _fromCol).FormulaR1C1;
            }
            set
            {
                IsRangeValid("FormulaR1C1");
                if (value.Length > 0 && value[0] == '=') value = value.Substring(1, value.Length - 1); // remove any starting equalsign.
 
                if (Addresses == null)
                {
                    Set_SharedFormula(ExcelCell.TranslateFromR1C1(value, _fromRow, _fromCol), this, false);
                }
                else
                {
                    Set_SharedFormula(ExcelCell.TranslateFromR1C1(value, _fromRow, _fromCol), new ExcelAddress(FirstAddress), false);
                    foreach (var address in Addresses)
                    {
                        Set_SharedFormula(ExcelCell.TranslateFromR1C1(value, address.Start.Row, address.Start.Column), address, false);
                    }
                }
            }
        }
        /// <summary>
        /// Set the hyperlink property for a range of cells
        /// </summary>
        public Uri Hyperlink
        {
            get
            {
                IsRangeValid("formulaR1C1");
                return _worksheet.Cell(_fromRow, _fromCol).Hyperlink;
            }
            set
            {
                _changePropMethod(Set_HyperLink, value);
            }
        }
        /// <summary>
        /// If the cells in the range are merged.
        /// </summary>
        public bool Merge
        {
            get
            {
                IsRangeValid("merging");
                for (int col = _fromCol; col <= _toCol; col++)
                {
                    for (int row = _fromRow; row <= _toRow; row++)
                    {
                        if (!_worksheet.Cell(row, col).Merge)
                        {
                            return false;
                        }
                    }
                }
                return true;
            }
            set
            {
                IsRangeValid("merging");
                SetMerge(value, FirstAddress);
                if (Addresses != null)
                {
                    foreach (var address in Addresses)
                    {
                        SetMerge(value, address._address);
                    }
                }
            }
        }
 
        private void SetMerge(bool value, string address)
        {
            if (!value)
            {
                if (_worksheet.MergedCells.List.Contains(address))
                {
                    SetCellMerge(false, address);
                    _worksheet.MergedCells.List.Remove(address);
                }
                else if (!CheckMergeDiff(false, address))
                {
                    throw (new Exception("Range is not fully merged.Specify the exact range"));
                }
            }
            else
            {
                if (CheckMergeDiff(false, address))
                {
                    SetCellMerge(true, address);
                    _worksheet.MergedCells.List.Add(address);
                }
                else
                {
                    if (!_worksheet.MergedCells.List.Contains(address))
                    {
                        throw (new Exception("Cells are already merged"));
                    }
                }
            }
        }
        /// <summary>
        /// Set an autofilter for the range
        /// </summary>
        public bool AutoFilter
        {
            get
            {
                IsRangeValid("autofilter");
                ExcelAddressBase address = _worksheet.AutoFilterAddress;
                if (address == null) return false;
                if (_fromRow >= address.Start.Row
                        &&
                        _toRow <= address.End.Row
                        &&
                        _fromCol >= address.Start.Column
                        &&
                        _toCol <= address.End.Column)
                {
                    return true;
                }
                return false;
            }
            set
            {
                IsRangeValid("autofilter");
                _worksheet.AutoFilterAddress = this;
                if (_worksheet.Names.ContainsKey("_xlnm._FilterDatabase"))
                {
                    _worksheet.Names.Remove("_xlnm._FilterDatabase");
                }
                var result = _worksheet.Names.Add("_xlnm._FilterDatabase", this);
                result.IsNameHidden = true;
            }
        }
        /// <summary>
        /// If the value is in richtext format.
        /// </summary>
        public bool IsRichText
        {
            get
            {
                IsRangeValid("richtext");
                return _worksheet.Cell(_fromRow, _fromCol).IsRichText;
            }
            set
            {
                _changePropMethod(Set_IsRichText, value);
            }
        }
        /// <summary>
        /// Is the range a part of an Arrayformula
        /// </summary>
        public bool IsArrayFormula
        {
            get
            {
                IsRangeValid("arrayformulas");
                return _worksheet.Cell(_fromRow, _fromCol).IsArrayFormula;
            }
        }
        ExcelRichTextCollection _rtc = null;
        /// <summary>
        /// Cell value is richtext formated. 
        /// </summary>
        public ExcelRichTextCollection RichText
        {
            get
            {
                IsRangeValid("richtext");
                if (_rtc == null)
                {
                    _rtc = GetRichText(_fromRow, _fromCol);
                }
                return _rtc;
            }
        }
 
        private ExcelRichTextCollection GetRichText(int row, int col)
        {
            XmlDocument xml = new XmlDocument();
            var cell = _worksheet.Cell(row, col);
            if (cell.Value != null)
            {
                if (cell.IsRichText)
                {
                    XmlHelper.LoadXmlSafe(xml, "<d:si xmlns:d=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" >" + cell.Value.ToString() + "</d:si>");
                }
                else
                {
                    xml.LoadXml("<d:si xmlns:d=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" ><d:r><d:t>" + SecurityElement.Escape(cell.Value.ToString()) + "</d:t></d:r></d:si>");
                }
            }
            else
            {
                xml.LoadXml("<d:si xmlns:d=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" />");
            }
            var rtc = new ExcelRichTextCollection(_worksheet.NameSpaceManager, xml.SelectSingleNode("d:si", _worksheet.NameSpaceManager), this);
            if (rtc.Count == 1 && cell.IsRichText == false)
            {
                IsRichText = true;
                var fnt = cell.Style.Font;
                rtc[0].PreserveSpace = true;
                rtc[0].Bold = fnt.Bold;
                rtc[0].FontName = fnt.Name;
                rtc[0].Italic = fnt.Italic;
                rtc[0].Size = fnt.Size;
                rtc[0].UnderLine = fnt.UnderLine;
 
                int hex;
                if (fnt.Color.Rgb != "" && int.TryParse(fnt.Color.Rgb, NumberStyles.HexNumber, null, out hex))
                {
                    rtc[0].Color = Color.FromArgb(hex);
                }
 
            }
            return rtc;
        }
        /// <summary>
        /// returns the comment object of the first cell in the range
        /// </summary>
        public ExcelComment Comment
        {
            get
            {
                IsRangeValid("comments");
                ulong cellID = GetCellID(_worksheet.SheetID, _fromRow, _fromCol);
                if (_worksheet.Comments._comments.ContainsKey(cellID))
                {
                    return _worksheet._comments._comments[cellID] as ExcelComment;
                }
                return null;
            }
        }
        /// <summary>
        /// WorkSheet object 
        /// </summary>
        public ExcelWorksheet Worksheet
        {
            get
            {
                return _worksheet;
            }
        }
        /// <summary>
        /// Address including sheetname
        /// </summary>
        public string FullAddress
        {
            get
            {
                string fullAddress = GetFullAddress(_worksheet.Name, _address);
                if (Addresses != null)
                {
                    foreach (var a in Addresses)
                    {
                        fullAddress += "," + GetFullAddress(_worksheet.Name, a.Address); ;
                    }
                }
                return fullAddress;
            }
        }
        /// <summary>
        /// Address including sheetname
        /// </summary>
        public string FullAddressAbsolute
        {
            get
            {
                string wbwsRef = string.IsNullOrEmpty(base._wb) ? base._ws : "[" + base._wb.Replace("'", "''") + "]" + _ws;
                string fullAddress = GetFullAddress(wbwsRef, GetAddress(_fromRow, _fromCol, _toRow, _toCol, true));
                if (Addresses != null)
                {
                    foreach (var a in Addresses)
                    {
                        fullAddress += "," + GetFullAddress(wbwsRef, GetAddress(a.Start.Row, a.Start.Column, a.End.Row, a.End.Column, true)); ;
                    }
                }
                return fullAddress;
            }
        }
        /// <summary>
        /// Address including sheetname
        /// </summary>
        internal string FullAddressAbsoluteNoFullRowCol
        {
            get
            {
                string wbwsRef = string.IsNullOrEmpty(base._wb) ? base._ws : "[" + base._wb.Replace("'", "''") + "]" + _ws;
                string fullAddress = GetFullAddress(wbwsRef, GetAddress(_fromRow, _fromCol, _toRow, _toCol, true), false);
                if (Addresses != null)
                {
                    foreach (var a in Addresses)
                    {
                        fullAddress += "," + GetFullAddress(wbwsRef, GetAddress(a.Start.Row, a.Start.Column, a.End.Row, a.End.Column, true),false); ;
                    }
                }
                return fullAddress;
            }
        }
        #endregion
        #region Private Methods
        /// <summary>
        /// Check if the range is partly merged
        /// </summary>
        /// <param name="startValue">the starting value</param>
        /// <param name="address">the address</param>
        /// <returns></returns>
        private bool CheckMergeDiff(bool startValue, string address)
        {
            ExcelAddress a = new ExcelAddress(address);
            for (int col = a._fromCol; col <= a._toCol; col++)
            {
                for (int row = a._fromRow; row <= a._toRow; row++)
                {
                    if (_worksheet.Cell(row, col).Merge != startValue)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// Set the merge flag for the range
        /// </summary>
        /// <param name="value"></param>
        /// <param name="address"></param>
        internal void SetCellMerge(bool value, string address)
        {
            ExcelAddress a = new ExcelAddress(address);
            for (int col = a._fromCol; col <= a._toCol; col++)
            {
                for (int row = a._fromRow; row <= a._toRow; row++)
                {
                    _worksheet.Cell(row, col).Merge = value;
                }
            }
        }
        /// <summary>
        /// Set the value without altering the richtext property
        /// </summary>
        /// <param name="value">the value</param>
        internal void SetValueRichText(object value)
        {
            if (_fromRow == 1 && _fromCol == 1 && _toRow == ExcelPackage.MaxRows && _toCol == ExcelPackage.MaxColumns)  //Full sheet (ex ws.Cells.Value=0). Set value for A1 only to avoid hanging 
            {
                _worksheet.Cell(1, 1).SetValueRichText(value);
            }
            else
            {
                for (int col = _fromCol; col <= _toCol; col++)
                {
                    for (int row = _fromRow; row <= _toRow; row++)
                    {
                        _worksheet.Cell(row, col).SetValueRichText(value);
                    }
                }
            }
        }
        /// <summary>
        /// Removes a shared formula
        /// </summary>
        private void RemoveFormuls(ExcelAddress address)
        {
            List<int> removed = new List<int>();
            int fFromRow, fFromCol, fToRow, fToCol;
            foreach (int index in _worksheet._sharedFormulas.Keys)
            {
                ExcelWorksheet.Formulas f = _worksheet._sharedFormulas[index];
                ExcelCell.GetRowColFromAddress(f.Address, out fFromRow, out fFromCol, out fToRow, out fToCol);
                if (((fFromCol >= address.Start.Column && fFromCol <= address.End.Column) ||
                     (fToCol >= address.Start.Column && fToCol <= address.End.Column)) &&
                     ((fFromRow >= address.Start.Row && fFromRow <= address.End.Row) ||
                     (fToRow >= address.Start.Row && fToRow <= address.End.Row)))
                {
                    for (int col = fFromCol; col <= fToCol; col++)
                    {
                        for (int row = fFromRow; row <= fToRow; row++)
                        {
                            _worksheet.Cell(row, col).SharedFormulaID = int.MinValue;
                        }
                    }
                    removed.Add(index);
                }
            }
            foreach (int index in removed)
            {
                _worksheet._sharedFormulas.Remove(index);
            }
        }
        internal void SetSharedFormulaID(int id)
        {
            for (int col = _fromCol; col <= _toCol; col++)
            {
                for (int row = _fromRow; row <= _toRow; row++)
                {
                    _worksheet.Cell(row, col).SharedFormulaID = id;
                }
            }
        }
        private void CheckAndSplitSharedFormula()
        {
            for (int col = _fromCol; col <= _toCol; col++)
            {
                for (int row = _fromRow; row <= _toRow; row++)
                {
                    if (_worksheet.Cell(row, col).SharedFormulaID >= 0)
                    {
                        SplitFormulas();
                        return;
                    }
                }
            }
        }
 
        private void SplitFormulas()
        {
            List<int> formulas = new List<int>();
            for (int col = _fromCol; col <= _toCol; col++)
            {
                for (int row = _fromRow; row <= _toRow; row++)
                {
                    int id = _worksheet.Cell(row, col).SharedFormulaID;
                    if (id >= 0 && !formulas.Contains(id))
                    {
                        if (_worksheet._sharedFormulas[id].IsArray &&
                                Collide(_worksheet.Cells[_worksheet._sharedFormulas[id].Address]) == eAddressCollition.Partly) // If the formula is an array formula and its on inside the overwriting range throw an exception
                        {
                            throw (new Exception("Can not overwrite a part of an array-formula"));
                        }
                        formulas.Add(_worksheet.Cell(row, col).SharedFormulaID);
                    }
                }
            }
 
            foreach (int ix in formulas)
            {
                SplitFormula(ix);
            }
        }
 
        private void SplitFormula(int ix)
        {
            var f = _worksheet._sharedFormulas[ix];
            var fRange = _worksheet.Cells[f.Address];
            var collide = Collide(fRange);
 
            //The formula is inside the currenct range, remove it
            if (collide == eAddressCollition.Inside)
            {
                _worksheet._sharedFormulas.Remove(ix);
                fRange.SetSharedFormulaID(int.MinValue);
            }
            else if (collide == eAddressCollition.Partly)
            {
                //The formula partly collides with the current range
                bool fIsSet = false;
                string formulaR1C1 = fRange.FormulaR1C1;
                //Top Range
                if (fRange._fromRow < _fromRow)
                {
                    f.Address = ExcelCell.GetAddress(fRange._fromRow, fRange._fromCol, _fromRow - 1, fRange._toCol);
                    fIsSet = true;
                }
                //Left Range
                if (fRange._fromCol < _fromCol)
                {
                    if (fIsSet)
                    {
                        f = new ExcelWorksheet.Formulas();
                        f.Index = _worksheet.GetMaxShareFunctionIndex(false);
                        f.StartCol = fRange._fromCol;
                        f.IsArray = false;
                        _worksheet._sharedFormulas.Add(f.Index, f);
                    }
                    else
                    {
                        fIsSet = true;
                    }
                    if (fRange._fromRow < _fromRow)
                        f.StartRow = _fromRow;
                    else
                    {
                        f.StartRow = fRange._fromRow;
                    }
                    if (fRange._toRow < _toRow)
                    {
                        f.Address = ExcelCell.GetAddress(f.StartRow, f.StartCol,
                                fRange._toRow, _fromCol - 1);
                    }
                    else
                    {
                        f.Address = ExcelCell.GetAddress(f.StartRow, f.StartCol,
                             _toRow, _fromCol - 1);
                    }
                    f.Formula = TranslateFromR1C1(formulaR1C1, f.StartRow, f.StartCol);
                    _worksheet.Cells[f.Address].SetSharedFormulaID(f.Index);
                }
                //Right Range
                if (fRange._toCol > _toCol)
                {
                    if (fIsSet)
                    {
                        f = new ExcelWorksheet.Formulas();
                        f.Index = _worksheet.GetMaxShareFunctionIndex(false);
                        f.IsArray = false;
                        _worksheet._sharedFormulas.Add(f.Index, f);
                    }
                    else
                    {
                        fIsSet = true;
                    }
                    f.StartCol = _toCol + 1;
                    if (_fromRow < fRange._fromRow)
                        f.StartRow = fRange._fromRow;
                    else
                    {
                        f.StartRow = _fromRow;
                    }
 
                    if (fRange._toRow < _toRow)
                    {
                        f.Address = ExcelCell.GetAddress(f.StartRow, f.StartCol,
                                fRange._toRow, fRange._toCol);
                    }
                    else
                    {
                        f.Address = ExcelCell.GetAddress(f.StartRow, f.StartCol,
                                _toRow, fRange._toCol);
                    }
                    f.Formula = TranslateFromR1C1(formulaR1C1, f.StartRow, f.StartCol);
                    _worksheet.Cells[f.Address].SetSharedFormulaID(f.Index);
                }
                //Bottom Range
                if (fRange._toRow > _toRow)
                {
                    if (fIsSet)
                    {
                        f = new ExcelWorksheet.Formulas();
                        f.Index = _worksheet.GetMaxShareFunctionIndex(false);
                        f.IsArray = false;
                        _worksheet._sharedFormulas.Add(f.Index, f);
                    }
 
                    f.StartCol = fRange._fromCol;
                    f.StartRow = _toRow + 1;
 
                    f.Formula = TranslateFromR1C1(formulaR1C1, f.StartRow, f.StartCol);
 
                    f.Address = ExcelCell.GetAddress(f.StartRow, f.StartCol,
                            fRange._toRow, fRange._toCol);
                    _worksheet.Cells[f.Address].SetSharedFormulaID(f.Index);
 
                }
            }
        }
        private object ConvertData(ExcelTextFormat Format, string v, int col, bool isText)
        {
            if (isText && (Format.DataTypes == null || Format.DataTypes.Length < col)) return v;
 
            double d;
            DateTime dt;
            if (Format.DataTypes == null || Format.DataTypes.Length < col || Format.DataTypes[col] == eDataTypes.Unknown)
            {
                string v2 = v.EndsWith("%") ? v.Substring(0, v.Length - 1) : v;
                if (double.TryParse(v2, NumberStyles.Any, Format.Culture, out d))
                {
                    if (v2 == v)
                    {
                        return d;
                    }
                    else
                    {
                        return d / 100;
                    }
                }
                if (DateTime.TryParse(v, Format.Culture, DateTimeStyles.None, out dt))
                {
                    return dt;
                }
                else
                {
                    return v;
                }
            }
            else
            {
                switch (Format.DataTypes[col])
                {
                    case eDataTypes.Number:
                        if (double.TryParse(v, NumberStyles.Any, Format.Culture, out d))
                        {
                            return d;
                        }
                        else
                        {
                            return v;
                        }
                    case eDataTypes.DateTime:
                        if (DateTime.TryParse(v, Format.Culture, DateTimeStyles.None, out dt))
                        {
                            return dt;
                        }
                        else
                        {
                            return v;
                        }
                    case eDataTypes.Percent:
                        string v2 = v.EndsWith("%") ? v.Substring(0, v.Length - 1) : v;
                        if (double.TryParse(v2, NumberStyles.Any, Format.Culture, out d))
                        {
                            return d / 100;
                        }
                        else
                        {
                            return v;
                        }
 
                    default:
                        return v;
 
                }
            }
        }
        #endregion
        #region Public Methods
        #region ConditionalFormatting
        /// <summary>
        /// Conditional Formatting for this range.
        /// </summary>
        public IRangeConditionalFormatting ConditionalFormatting
        {
            get
            {
                return new RangeConditionalFormatting(_worksheet, new ExcelAddress(Address));
            }
        }
        #endregion
        #region DataValidation
        /// <summary>
        /// Data validation for this range.
        /// </summary>
        public IRangeDataValidation DataValidation
        {
            get
            {
                return new RangeDataValidation(_worksheet, Address);
            }
        }
        #endregion
        #region LoadFromDataTable
        /// <summary>
        /// Load the data from the datatable starting from the top left cell of the range
        /// </summary>
        /// <param name="Table">The datatable to load</param>
        /// <param name="PrintHeaders">Print the column caption property (if set) or the columnname property if not, on first row</param>
        /// <param name="TableStyle">The table style to apply to the data</param>
        /// <returns>The filled range</returns>
        public ExcelRangeBase LoadFromDataTable(DataTable Table, bool PrintHeaders, TableStyles TableStyle)
        {
            var r = LoadFromDataTable(Table, PrintHeaders);
 
            int rows = Table.Rows.Count + (PrintHeaders ? 1 : 0) - 1;
            if (rows >= 0 && Table.Columns.Count>0)
            {
                var tbl = _worksheet.Tables.Add(new ExcelAddressBase(_fromRow, _fromCol, _fromRow + (rows==0 ? 1 : rows), _fromCol + Table.Columns.Count-1), Table.TableName);
                tbl.ShowHeader = PrintHeaders;
                tbl.TableStyle = TableStyle;
            }
            return r;
        }
        /// <summary>
        /// Load the data from the datatable starting from the top left cell of the range
        /// </summary>
        /// <param name="Table">The datatable to load</param>
        /// <param name="PrintHeaders">Print the caption property (if set) or the columnname property if not, on first row</param>
        /// <returns>The filled range</returns>
        public ExcelRangeBase LoadFromDataTable(DataTable Table, bool PrintHeaders)
        {
            if (Table == null)
            {
                throw (new ArgumentNullException("Table can't be null"));
            }
 
            int col = _fromCol, row = _fromRow;
            if (PrintHeaders)
            {
                foreach (DataColumn dc in Table.Columns)
                {
                    // If no caption is set, the ColumnName property is called implicitly.
                    _worksheet.Cell(row, col++).Value = dc.Caption;
                }
                row++;
                col = _fromCol;
            }
            foreach (DataRow dr in Table.Rows)
            {
                foreach (object value in dr.ItemArray)
                {
                    _worksheet.Cell(row, col++).Value = value;
                }
                row++;
                col = _fromCol;
            }
            return _worksheet.Cells[_fromRow, _fromCol, row - 1, Table.Columns.Count];
        }
        #endregion
        #region LoadFromArrays
        /// <summary>
        /// Loads data from the collection of arrays of objects into the range, starting from
        /// the top-left cell.
        /// </summary>
        /// <param name="Data">The data.</param>
        public ExcelRangeBase LoadFromArrays(IEnumerable<object[]> Data)
        {
            //thanx to Abdullin for the code contribution
            if (Data == null) throw new ArgumentNullException("data");
 
            int column = _fromCol, row = _fromRow;
 
            foreach (var rowData in Data)
            {
                column = _fromCol;
                foreach (var cellData in rowData)
                {
                    _worksheet.Cell(row, column).Value = cellData;
                    column += 1;
                }
                row += 1;
            }
            return _worksheet.Cells[_fromRow, _fromCol, row - 1, column - 1];
        }
        #endregion
        #region LoadFromCollection
        /// <summary>
        /// Load a collection into a the worksheet starting from the top left row of the range.
        /// </summary>
        /// <typeparam name="T">The datatype in the collection</typeparam>
        /// <param name="Collection">The collection to load</param>
        /// <returns>The filled range</returns>
        public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection)
        {
            return LoadFromCollection<T>(Collection, false, TableStyles.None, BindingFlags.Public | BindingFlags.Instance, null);
        }
        /// <summary>
        /// Load a collection of T into the worksheet starting from the top left row of the range.
        /// Default option will load all public instance properties of T
        /// </summary>
        /// <typeparam name="T">The datatype in the collection</typeparam>
        /// <param name="Collection">The collection to load</param>
        /// <param name="PrintHeaders">Print the property names on the first row</param>
        /// <returns>The filled range</returns>
        public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection, bool PrintHeaders)
        {
            return LoadFromCollection<T>(Collection, PrintHeaders, TableStyles.None, BindingFlags.Public | BindingFlags.Instance, null);
        }
        /// <summary>
        /// Load a collection of T into the worksheet starting from the top left row of the range.
        /// Default option will load all public instance properties of T
        /// </summary>
        /// <typeparam name="T">The datatype in the collection</typeparam>
        /// <param name="Collection">The collection to load</param>
        /// <param name="PrintHeaders">Print the property names on the first row</param>
        /// <param name="TableStyle">Will create a table with this style. If set to TableStyles.None no table will be created</param>
        /// <returns>The filled range</returns>
        public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection, bool PrintHeaders, TableStyles TableStyle)
        {
            return LoadFromCollection<T>(Collection, PrintHeaders, TableStyle, BindingFlags.Public | BindingFlags.Instance, null);
        }
        /// <summary>
        /// Load a collection into the worksheet starting from the top left row of the range.
        /// </summary>
        /// <typeparam name="T">The datatype in the collection</typeparam>
        /// <param name="Collection">The collection to load</param>
        /// <param name="PrintHeaders">Print the property names on the first row. Any underscore in the property name will be converted to a space.</param>
        /// <param name="TableStyle">Will create a table with this style. If set to TableStyles.None no table will be created</param>
        /// <param name="memberFlags">Property flags to use</param>
        /// <param name="Members">The properties to output. Must be of type T</param>
        /// <returns>The filled range</returns>
        public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection, bool PrintHeaders, TableStyles TableStyle, BindingFlags memberFlags, MemberInfo[] Members)
        {
            var type = typeof(T);
            if (Members == null)
            {
                Members = type.GetProperties(memberFlags);
            }
            else
            {
                foreach (var t in Members)
                {
                    if (t.DeclaringType != type)
                    {
                        throw (new Exception("Supplied properties in parameter Properties must be of the same type as T"));
                    }
                }
            }
 
            int col = _fromCol, row = _fromRow;
            if (Members.Length > 0 && PrintHeaders)
            {
                foreach (var t in Members)
                {
                    _worksheet.Cell(row, col++).Value = t.Name.Replace('_', ' ');
                }
                row++;
            }
 
            if (Members.Length == 0)
            {
                foreach (var item in Collection)
                {
                    _worksheet.Cells[row++, col].Value = item;
                }
            }
            else
            {
                foreach (var item in Collection)
                {
                    col = _fromCol;
                    if (item is string || item is decimal || item is DateTime || item.GetType().IsPrimitive)
                    {
                        _worksheet.Cells[row, col++].Value = item;
                    }
                    else
                    {
                        foreach (var t in Members)
                        {
                            if (t is PropertyInfo)
                            {
                                _worksheet.Cells[row, col++].Value = ((PropertyInfo)t).GetValue(item, null);
                            }
                            else if (t is FieldInfo)
                            {
                                _worksheet.Cells[row, col++].Value = ((FieldInfo)t).GetValue(item);
                            }
                            else if (t is MethodInfo)
                            {
                                _worksheet.Cells[row, col++].Value = ((MethodInfo)t).Invoke(item, null);
                            }
                        }
                    }
                    row++;
                }
            }
 
            var r = _worksheet.Cells[_fromRow, _fromCol, row - 1, col - 1];
 
            if (TableStyle != TableStyles.None)
            {
                var tbl = _worksheet.Tables.Add(r, "");
                tbl.ShowHeader = PrintHeaders;
                tbl.TableStyle = TableStyle;
            }
            return r;
        }
        #endregion
        #region LoadFromText
        /// <summary>
        /// Loads a CSV text into a range starting from the top left cell.
        /// Default settings is Comma separation
        /// </summary>
        /// <param name="Text">The Text</param>
        /// <returns>The range containing the data</returns>
        public ExcelRangeBase LoadFromText(string Text)
        {
            return LoadFromText(Text, new ExcelTextFormat());
        }
        /// <summary>
        /// Loads a CSV text into a range starting from the top left cell.
        /// </summary>
        /// <param name="Text">The Text</param>
        /// <param name="Format">Information how to load the text</param>
        /// <returns>The range containing the data</returns>
        public ExcelRangeBase LoadFromText(string Text, ExcelTextFormat Format)
        {
            if (Format == null) Format = new ExcelTextFormat();
            string[] lines = Regex.Split(Text, Format.EOL);
            int row = _fromRow;
            int col = _fromCol;
            int maxCol = col;
            int lineNo = 1;
            if (Text == "")
            {
                _worksheet.Cells[_fromRow, _fromCol].Value = "";
            }
            else
            {
                foreach (string line in lines)
                {
                    if (lineNo > Format.SkipLinesBeginning && lineNo <= lines.Length - Format.SkipLinesEnd)
                    {
                        col = _fromCol;
                        string v = "";
                        bool isText = false, isQualifier = false;
                        int QCount = 0;
                        foreach (char c in line)
                        {
                            if (Format.TextQualifier != 0 && c == Format.TextQualifier)
                            {
                                if (!isText && v != "")
                                {
                                    throw (new Exception(string.Format("Invalid Text Qualifier in line : {0}", line)));
                                }
                                isQualifier = !isQualifier;
                                QCount += 1;
                                isText = true;
                            }
                            else
                            {
                                if (QCount > 1 && !string.IsNullOrEmpty(v))
                                {
                                    v += new string(Format.TextQualifier, QCount / 2);
                                }
                                else if(QCount>2 && string.IsNullOrEmpty(v))
                                {
                                    v += new string(Format.TextQualifier, (QCount-1) / 2);
                                }
 
                                if (isQualifier)
                                {
                                    v += c;
                                }
                                else
                                {
                                    if (c == Format.Delimiter)
                                    {
                                        _worksheet.Cell(row, col).Value = ConvertData(Format, v, col - _fromCol, isText);
                                        v = "";
                                        isText = false;
                                        col++;
                                    }
                                    else
                                    {
                                        if (QCount % 2 == 1)
                                        {
                                            throw (new Exception(string.Format("Text delimiter is not closed in line : {0}", line)));
                                        }
                                        v += c;
                                    }
                                }
                                QCount = 0;
                            }
                        }
                        if (QCount > 1)
                        {
                            v += new string(Format.TextQualifier, QCount / 2);
                        }
 
                        _worksheet.Cell(row, col).Value = ConvertData(Format, v, col - _fromCol, isText);
                        if (col > maxCol) maxCol = col;
                        row++;
                    }
                    lineNo++;
                }
            }
            return _worksheet.Cells[_fromRow, _fromCol, row - 1, maxCol];
        }
        /// <summary>
        /// Loads a CSV text into a range starting from the top left cell.
        /// </summary>
        /// <param name="Text">The Text</param>
        /// <param name="Format">Information how to load the text</param>
        /// <param name="TableStyle">Create a table with this style</param>
        /// <param name="FirstRowIsHeader">Use the first row as header</param>
        /// <returns></returns>
        public ExcelRangeBase LoadFromText(string Text, ExcelTextFormat Format, TableStyles TableStyle, bool FirstRowIsHeader)
        {
            var r = LoadFromText(Text, Format);
 
            var tbl = _worksheet.Tables.Add(r, "");
            tbl.ShowHeader = FirstRowIsHeader;
            tbl.TableStyle = TableStyle;
 
            return r;
        }
        /// <summary>
        /// Loads a CSV file into a range starting from the top left cell.
        /// </summary>
        /// <param name="TextFile">The Textfile</param>
        /// <returns></returns>
        public ExcelRangeBase LoadFromText(FileInfo TextFile)
        {
            return LoadFromText(File.ReadAllText(TextFile.FullName, Encoding.ASCII));
        }
        /// <summary>
        /// Loads a CSV file into a range starting from the top left cell.
        /// </summary>
        /// <param name="TextFile">The Textfile</param>
        /// <param name="Format">Information how to load the text</param>
        /// <returns></returns>
        public ExcelRangeBase LoadFromText(FileInfo TextFile, ExcelTextFormat Format)
        {
            return LoadFromText(File.ReadAllText(TextFile.FullName, Format.Encoding), Format);
        }
        /// <summary>
        /// Loads a CSV file into a range starting from the top left cell.
        /// </summary>
        /// <param name="TextFile">The Textfile</param>
        /// <param name="Format">Information how to load the text</param>
        /// <param name="TableStyle">Create a table with this style</param>
        /// <param name="FirstRowIsHeader">Use the first row as header</param>
        /// <returns></returns>
        public ExcelRangeBase LoadFromText(FileInfo TextFile, ExcelTextFormat Format, TableStyles TableStyle, bool FirstRowIsHeader)
        {
            return LoadFromText(File.ReadAllText(TextFile.FullName, Format.Encoding), Format, TableStyle, FirstRowIsHeader);
        }
        #endregion
        #region GetValue
        /// <summary>
        /// Get the strongly typed value of the cell.
        /// </summary>
        /// <typeparam name="T">The type</typeparam>
        /// <returns>The value. If the value can't be converted to the specified type, the default value will be returned</returns>
        public T GetValue<T>()
        {
            return _worksheet.GetTypedValue<T>(Value);
        }
        #endregion
        /// <summary>
        /// Get a range with an offset from the top left cell.
        /// The new range has the same dimensions as the current range
        /// </summary>
        /// <param name="RowOffset">Row Offset</param>
        /// <param name="ColumnOffset">Column Offset</param>
        /// <returns></returns>
        public ExcelRangeBase Offset(int RowOffset, int ColumnOffset)
        {
            if (_fromRow + RowOffset < 1 || _fromCol + ColumnOffset < 1 || _fromRow + RowOffset > ExcelPackage.MaxRows || _fromCol + ColumnOffset > ExcelPackage.MaxColumns)
            {
                throw (new ArgumentOutOfRangeException("Offset value out of range"));
            }
            string address = GetAddress(_fromRow + RowOffset, _fromCol + ColumnOffset, _toRow + RowOffset, _toCol + ColumnOffset);
            return new ExcelRangeBase(_worksheet, address);
        }
        /// <summary>
        /// Get a range with an offset from the top left cell.
        /// </summary>
        /// <param name="RowOffset">Row Offset</param>
        /// <param name="ColumnOffset">Column Offset</param>
        /// <param name="NumberOfRows">Number of rows. Minimum 1</param>
        /// <param name="NumberOfColumns">Number of colums. Minimum 1</param>
        /// <returns></returns>
        public ExcelRangeBase Offset(int RowOffset, int ColumnOffset, int NumberOfRows, int NumberOfColumns)
        {
            if (NumberOfRows < 1 || NumberOfColumns < 1)
            {
                throw (new Exception("Number of rows/columns must be greater than 0"));
            }
            NumberOfRows--;
            NumberOfColumns--;
            if (_fromRow + RowOffset < 1 || _fromCol + ColumnOffset < 1 || _fromRow + RowOffset > ExcelPackage.MaxRows || _fromCol + ColumnOffset > ExcelPackage.MaxColumns ||
                 _fromRow + RowOffset + NumberOfRows < 1 || _fromCol + ColumnOffset + NumberOfColumns < 1 || _fromRow + RowOffset + NumberOfRows > ExcelPackage.MaxRows || _fromCol + ColumnOffset + NumberOfColumns > ExcelPackage.MaxColumns)
            {
                throw (new ArgumentOutOfRangeException("Offset value out of range"));
            }
            string address = GetAddress(_fromRow + RowOffset, _fromCol + ColumnOffset, _fromRow + RowOffset + NumberOfRows, _fromCol + ColumnOffset + NumberOfColumns);
            return new ExcelRangeBase(_worksheet, address);
        }
        /// <summary>
        /// Adds a new comment for the range.
        /// If this range contains more than one cell, the top left comment is returned by the method.
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="Author"></param>
        /// <returns>A reference comment of the top left cell</returns>
        public ExcelComment AddComment(string Text, string Author)
        {
            //Check if any comments exists in the range and throw an exception
            _changePropMethod(Exists_Comment, null);
            //Create the comments
            _changePropMethod(Set_Comment, new string[] { Text, Author });
 
 
            return _worksheet.Comments[new ExcelCellAddress(_fromRow, _fromCol)];
        }
 
        /// <summary>
        /// Copies the range of cells to an other range
        /// </summary>
        /// <param name="Destination">The start cell where the range will be copied.</param>
        public void Copy(ExcelRangeBase Destination)
        {
            bool sameWorkbook = Destination._worksheet.Workbook == _worksheet.Workbook;
            ExcelStyles sourceStyles = _worksheet.Workbook.Styles,
                                    styles = Destination._worksheet.Workbook.Styles;
            Dictionary<int, int> styleCashe = new Dictionary<int, int>();
 
            //Delete all existing cells;            
            List<ExcelCell> newCells = new List<ExcelCell>();
            Dictionary<ulong, ExcelCell> mergedCells = new Dictionary<ulong, ExcelCell>();
            foreach (var cell in this)
            {
                //Clone the cell
                var copiedCell = (_worksheet._cells[GetCellID(_worksheet.SheetID, cell._fromRow, cell._fromCol)] as ExcelCell);
 
                var newCell = copiedCell.Clone(Destination._worksheet,
                        Destination._fromRow + (copiedCell.Row - _fromRow),
                        Destination._fromCol + (copiedCell.Column - _fromCol));
 
                newCell.MergeId = _worksheet.GetMergeCellId(copiedCell.Row, copiedCell.Column);
 
                //If the formula is shared, remove the shared ID and set the formula for the cell.
                if (newCell._sharedFormulaID >= 0)
                {
                    newCell._sharedFormulaID = int.MinValue;
                    newCell.Formula = cell.Formula;
                }
 
                if (!string.IsNullOrEmpty(newCell.Formula))
                {
                    newCell.Formula = ExcelCell.UpdateFormulaReferences(newCell.Formula, newCell.Row - copiedCell.Row, (newCell.Column - copiedCell.Column), 1, 1);
                }
 
                //If its not the same workbook we must copy the styles to the new workbook.
                if (!sameWorkbook)
                {
                    if (styleCashe.ContainsKey(cell.StyleID))
                    {
                        newCell.StyleID = styleCashe[cell.StyleID];
                    }
                    else
                    {
                        newCell.StyleID = styles.CloneStyle(sourceStyles, cell.StyleID);
                        styleCashe.Add(cell.StyleID, newCell.StyleID);
                    }
                }
                newCells.Add(newCell);
                if (newCell.Merge) mergedCells.Add(newCell.CellID, newCell);
            }
 
            //Now clear the destination.
            Destination.Offset(0, 0, (_toRow - _fromRow) + 1, (_toCol - _fromCol) + 1).Clear();
 
            //And last add the new cells to the worksheet
            foreach (var cell in newCells)
            {
                Destination.Worksheet._cells.Add(cell);
            }
            //Add merged cells
            if (mergedCells.Count > 0)
            {
                List<ExcelAddressBase> mergedAddresses = new List<ExcelAddressBase>();
                foreach (var cell in mergedCells.Values)
                {
                    if (!IsAdded(cell, mergedAddresses))
                    {
                        int startRow = cell.Row, startCol = cell.Column, endRow = cell.Row, endCol = cell.Column + 1;
                        while (mergedCells.ContainsKey(ExcelCell.GetCellID(Destination.Worksheet.SheetID, endRow, endCol)))
                        {
                            ExcelCell next = mergedCells[ExcelCell.GetCellID(Destination.Worksheet.SheetID, endRow, endCol)];
                            if (cell.MergeId != next.MergeId)
                            {
                                break;
                            }
                            endCol++;
                        }
 
                        while (IsMerged(mergedCells, Destination.Worksheet, endRow, startCol, endCol - 1, cell))
                        {
                            endRow++;
                        }
 
                        mergedAddresses.Add(new ExcelAddressBase(startRow, startCol, endRow - 1, endCol - 1));
                    }
                }
                Destination.Worksheet.MergedCells.List.AddRange((from r in mergedAddresses select r.Address));
            }
        }
 
        private bool IsAdded(ExcelCell cell, List<ExcelAddressBase> mergedAddresses)
        {
            foreach (var address in mergedAddresses)
            {
                if (address.Collide(new ExcelAddressBase(cell.CellAddress)) == eAddressCollition.Inside)
                {
                    return true;
                }
            }
            return false;
        }
 
        private bool IsMerged(Dictionary<ulong, ExcelCell> mergedCells, ExcelWorksheet worksheet, int row, int startCol, int endCol, ExcelCell cell)
        {
            for (int col = startCol; col <= endCol; col++)
            {
                if (!mergedCells.ContainsKey(ExcelCell.GetCellID(worksheet.SheetID, row, col)))
                {
                    return false;
                }
                else
                {
                    ExcelCell next = mergedCells[ExcelCell.GetCellID(worksheet.SheetID, row, col)];
                    if (cell.MergeId != next.MergeId)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
 
        /// <summary>
        /// Clear all cells
        /// </summary>
        public void Clear()
        {
            Delete(this);
        }
        /// <summary>
        /// Creates an array-formula.
        /// </summary>
        /// <param name="ArrayFormula">The formula</param>
        public void CreateArrayFormula(string ArrayFormula)
        {
            if (Addresses != null)
            {
                throw (new Exception("An Arrayformula can not have more than one address"));
            }
            Set_SharedFormula(ArrayFormula, this, true);
        }
        private void Delete(ExcelAddressBase Range)
        {
            DeleteCheckMergedCells(Range);
            //First find the start cell
            ulong startID = GetCellID(_worksheet.SheetID, Range._fromRow, Range._fromCol);
            int index = _worksheet._cells.IndexOf(startID);
            if (index < 0)
            {
                index = ~index;
            }
            ExcelCell cell;
            //int row=cell.Row, col=cell.Column;
            //Remove all cells in the range
            while (index < _worksheet._cells.Count)
            {
                cell = _worksheet._cells[index] as ExcelCell;
                if (cell.Row > Range._toRow || cell.Row == Range._toRow && cell.Column > Range._toCol)
                {
                    break;
                }
                else
                {
                    if (cell.Column >= Range._fromCol && cell.Column <= Range._toCol)
                    {
                        _worksheet._cells.Delete(cell.CellID);
                    }
                    else
                    {
                        index++;
                    }
                }
            }
 
            //Delete multi addresses as well
            if (Addresses != null)
            {
                foreach (var sub in Addresses)
                {
                    Delete(sub);
                }
            }
        }
 
        private void DeleteCheckMergedCells(ExcelAddressBase Range)
        {
            var removeItems = new List<string>();
            foreach (var addr in Worksheet.MergedCells)
            {
                var addrCol = Range.Collide(new ExcelAddress(Range.WorkSheet, addr));
                if (addrCol != eAddressCollition.No)
                {
                    if (addrCol == eAddressCollition.Inside)
                    {
                        removeItems.Add(addr);
                    }
                    else
                    {
                        throw (new InvalidOperationException("Can't remove/overwrite cells that are merged"));
                    }
                }
            }
            foreach (var item in removeItems)
            {
                Worksheet.MergedCells.Remove(item);
            }
        }
        #endregion
        #region IDisposable Members
 
        public void Dispose()
        {
            //_worksheet = null;
        }
 
        #endregion
        #region "Enumerator"
        int _index;
        ulong _toCellId;
        int _enumAddressIx;
        public IEnumerator<ExcelRangeBase> GetEnumerator()
        {
            Reset();
            return this;
        }
 
        IEnumerator IEnumerable.GetEnumerator()
        {
            Reset();
            return this;
        }
 
        /// <summary>
        /// The current range when enumerating
        /// </summary>
        public ExcelRangeBase Current
        {
            get
            {
                return new ExcelRangeBase(_worksheet, (_worksheet._cells[_index] as ExcelCell).CellAddress);
            }
        }
 
        /// <summary>
        /// The current range when enumerating
        /// </summary>
        object IEnumerator.Current
        {
            get
            {
                return ((object)(new ExcelRangeBase(_worksheet, (_worksheet._cells[_index] as ExcelCell).CellAddress)));
            }
        }
 
        public bool MoveNext()
        {
            _index++;
            if (_enumAddressIx == -1)
            {
                GetNextIndexEnum(_fromRow, _fromCol, _toRow, _toCol);
 
                if (_index >= _worksheet._cells.Count || _worksheet._cells[_index].RangeID > _toCellId)
                {
                    if (Addresses == null)
                    {
                        return false;
                    }
                    else
                    {
                        _enumAddressIx++;
                        GetStartIndexEnum(Addresses[0].Start.Row, Addresses[0].Start.Column, Addresses[0].End.Row, Addresses[0].End.Column);
                        return MoveNext();
                    }
                }
 
            }
            else
            {
                GetNextIndexEnum(Addresses[_enumAddressIx].Start.Row, Addresses[_enumAddressIx].Start.Column, Addresses[_enumAddressIx].End.Row, Addresses[_enumAddressIx].End.Column);
                if (_index >= _worksheet._cells.Count || _worksheet._cells[_index].RangeID > _toCellId)
                {
                    if (++_enumAddressIx < Addresses.Count)
                    {
                        GetStartIndexEnum(Addresses[_enumAddressIx].Start.Row, Addresses[_enumAddressIx].Start.Column, Addresses[_enumAddressIx].End.Row, Addresses[_enumAddressIx].End.Column);
                        MoveNext();
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return true;
        }
 
        public void Reset()
        {
            _enumAddressIx = -1;
            GetStartIndexEnum(_fromRow, _fromCol, _toRow, _toCol);
        }
 
        private void GetNextIndexEnum(int fromRow, int fromCol, int toRow, int toCol)
        {
            if (_index >= _worksheet._cells.Count) return;
            ExcelCell cell = _worksheet._cells[_index] as ExcelCell;
            while (cell.Column > toCol || cell.Column < fromCol)
            {
                if (cell.Column < fromCol)
                {
                    _index = _worksheet._cells.IndexOf(ExcelAddress.GetCellID(_worksheet.SheetID, cell.Row, fromCol));
                }
                else
                {
                    _index = _worksheet._cells.IndexOf(ExcelAddress.GetCellID(_worksheet.SheetID, cell.Row + 1, fromCol));
                }
 
                if (_index < 0)
                {
                    _index = ~_index;
                }
                if (_index >= _worksheet._cells.Count || _worksheet._cells[_index].RangeID > _toCellId)
                {
                    break;
                }
                cell = _worksheet._cells[_index] as ExcelCell;
            }
        }
 
        private void GetStartIndexEnum(int fromRow, int fromCol, int toRow, int toCol)
        {
            _index = _worksheet._cells.IndexOf(ExcelCellBase.GetCellID(_worksheet.SheetID, fromRow, fromCol));
            _toCellId = ExcelCellBase.GetCellID(_worksheet.SheetID, toRow, toCol);
            if (_index < 0)
            {
                _index = ~_index;
            }
            _index--;
        }
    #endregion
    }
}