summaryrefslogtreecommitdiff
path: root/sc/source/ui/docshell/impex.cxx
blob: 70c1eab74609aa10fc9d62466dcef823d905d4b7 (plain)
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
/*************************************************************************
 *
 *  $RCSfile: impex.cxx,v $
 *
 *  $Revision: 1.14 $
 *
 *  last change: $Author: er $ $Date: 2001-09-07 19:36:16 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

// System - Includes -----------------------------------------------------

class StarBASIC;

#ifdef PCH
#include "ui_pch.hxx"
#endif

#pragma hdrstop

#ifndef PCH
#include "sc.hrc"
#define GLOBALOVERFLOW
#endif

// INCLUDE ---------------------------------------------------------------

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

#include <tools/list.hxx>
#include <tools/string.hxx>
#include <tools/solmath.hxx>
#include <svtools/htmlout.hxx>
#include <svtools/zforlist.hxx>
#include <sot/formats.hxx>
#include <sfx2/mieclip.hxx>
#include <unotools/charclass.hxx>
#include <unotools/collatorwrapper.hxx>
#include <unotools/calendarwrapper.hxx>
#ifndef _COM_SUN_STAR_I18N_CALENDARFIELDINDEX_HPP_
#include <com/sun/star/i18n/CalendarFieldIndex.hpp>
#endif
#ifndef _UNOTOOLS_TRANSLITERATIONWRAPPER_HXX
#include <unotools/transliterationwrapper.hxx>
#endif

#include "global.hxx"
#include "docsh.hxx"
#include "undoblk.hxx"
#include "rangenam.hxx"
#include "viewdata.hxx"
#include "tabvwsh.hxx"
#include "filter.hxx"
#include "asciiopt.hxx"
#include "cell.hxx"
#include "rtfimp.hxx"
#include "htmlimp.hxx"
#include "docoptio.hxx"
#include "progress.hxx"

#include "impex.hxx"

#include "globstr.hrc"

#ifndef _SV_MSGBOX_HXX //autogen
#include <vcl/msgbox.hxx>
#endif

#ifndef _SV_SVAPP_HXX //autogen
#include <vcl/svapp.hxx>
#endif

//========================================================================


// Gesamtdokument ohne Undo


ScImportExport::ScImportExport( ScDocument* p )
    : pDoc( p ), pDocSh( PTR_CAST(ScDocShell,p->GetDocumentShell()) ),
      nSizeLimit( 0 ), bSingle( TRUE ), bAll( TRUE ), bUndo( FALSE ),
      cSep( '\t' ), cStr( '"' ), bFormulas( FALSE ), bOverflow( FALSE )
{
    pUndoDoc = NULL;
    pExtOptions = NULL;
}

// Insert am Punkt ohne Bereichschecks


ScImportExport::ScImportExport( ScDocument* p, const ScAddress& rPt )
    : pDoc( p ), pDocSh( PTR_CAST(ScDocShell,p->GetDocumentShell()) ),
      aRange( rPt ),
      nSizeLimit( 0 ), bSingle( TRUE ), bAll( FALSE ), bUndo( BOOL( pDocSh != NULL ) ),
      cSep( '\t' ), cStr( '"' ), bFormulas( FALSE ), bOverflow( FALSE )
{
    pUndoDoc = NULL;
    pExtOptions = NULL;
}


//  ctor with a range is only used for export
//! ctor with a string (and bSingle=TRUE) is also used for DdeSetData

ScImportExport::ScImportExport( ScDocument* p, const ScRange& r )
    : pDoc( p ), pDocSh( PTR_CAST(ScDocShell,p->GetDocumentShell()) ),
      aRange( r ),
      nSizeLimit( 0 ), bSingle( FALSE ), bAll( FALSE ), bUndo( BOOL( pDocSh != NULL ) ),
      cSep( '\t' ), cStr( '"' ), bFormulas( FALSE ), bOverflow( FALSE )
{
    pUndoDoc = NULL;
    pExtOptions = NULL;
    // Zur Zeit nur in einer Tabelle!
    aRange.aEnd.SetTab( aRange.aStart.Tab() );
}

// String auswerten: Entweder Bereich, Punkt oder Gesamtdoc (bei Fehler)
// Falls eine View existiert, wird die TabNo der View entnommen!


ScImportExport::ScImportExport( ScDocument* p, const String& rPos )
    : pDoc( p ), pDocSh( PTR_CAST(ScDocShell,p->GetDocumentShell()) ),
      nSizeLimit( 0 ), bSingle( TRUE ), bAll( FALSE ), bUndo( BOOL( pDocSh != NULL ) ),
      cSep( '\t' ), cStr( '"' ), bFormulas( FALSE ), bOverflow( FALSE )
{
    pUndoDoc = NULL;
    pExtOptions = NULL;

    USHORT nTab = ScDocShell::GetCurTab();
    aRange.aStart.SetTab( nTab );
    String aPos( rPos );
    //  Benannter Bereich?
    ScRangeName* pRange = pDoc->GetRangeName();
    if( pRange )
    {
        USHORT nPos;
        if( pRange->SearchName( aPos, nPos ) )
        {
            ScRangeData* pData = (*pRange)[ nPos ];
            if( pData->HasType( RT_REFAREA )
                || pData->HasType( RT_ABSAREA )
                || pData->HasType( RT_ABSPOS ) )
                pData->GetSymbol( aPos );                   // mit dem Inhalt weitertesten
        }
    }
    // Bereich?
    if( aRange.Parse( aPos, pDoc ) & SCA_VALID )
        bSingle = FALSE;
    // Zelle?
    else if( aRange.aStart.Parse( aPos, pDoc ) & SCA_VALID )
        aRange.aEnd = aRange.aStart;
    else
        bAll = TRUE;
}


ScImportExport::~ScImportExport()
{
    delete pUndoDoc;
    delete pExtOptions;
}


void ScImportExport::SetExtOptions( const ScAsciiOptions& rOpt )
{
    if ( pExtOptions )
        *pExtOptions = rOpt;
    else
        pExtOptions = new ScAsciiOptions( rOpt );

    //  "normale" Optionen uebernehmen

    cSep = rOpt.GetFieldSeps().GetChar(0);
    cStr = rOpt.GetTextSep();
}


BOOL ScImportExport::IsFormatSupported( ULONG nFormat )
{
    return BOOL( nFormat == FORMAT_STRING
              || nFormat == SOT_FORMATSTR_ID_SYLK
              || nFormat == SOT_FORMATSTR_ID_LINK
              || nFormat == SOT_FORMATSTR_ID_HTML
              || nFormat == SOT_FORMATSTR_ID_HTML_SIMPLE
              || nFormat == SOT_FORMATSTR_ID_DIF );
}


//////////////////////////////////////////////////////////////////////////////

// Vorbereitung fuer Undo: Undo-Dokument erzeugen


BOOL ScImportExport::StartPaste()
{
    if ( !bAll && !pDoc->IsBlockEditable
        ( aRange.aStart.Tab(), aRange.aStart.Col(),aRange.aStart.Row(),
                                  aRange.aEnd.Col(),aRange.aEnd.Row() ) )
    {
        InfoBox aInfoBox(Application::GetDefDialogParent(),
                            ScGlobal::GetRscString( STR_PROTECTIONERR ) );
        aInfoBox.Execute();
        return FALSE;
    }
    if( bUndo && pDocSh && pDoc->IsUndoEnabled())
    {
        pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
        pUndoDoc->InitUndo( pDoc, aRange.aStart.Tab(), aRange.aEnd.Tab() );
        pDoc->CopyToDocument( aRange, IDF_ALL, FALSE, pUndoDoc );
    }
    return TRUE;
}

// Nachbereitung Insert: Undo/Redo-Aktionen erzeugen, Invalidate/Repaint


void ScImportExport::EndPaste()
{
    BOOL bHeight = pDocSh && pDocSh->AdjustRowHeight(
                    aRange.aStart.Row(), aRange.aEnd.Row(), aRange.aStart.Tab() );

    if( pUndoDoc && pDoc->IsUndoEnabled() )
    {
        ScDocument* pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
        pRedoDoc->InitUndo( pDoc, aRange.aStart.Tab(), aRange.aEnd.Tab() );
        pDoc->CopyToDocument( aRange, IDF_ALL, FALSE, pRedoDoc );
        ScMarkData aDestMark;
        aDestMark.SelectOneTable( aRange.aStart.Tab() );
        pDocSh->GetUndoManager()->AddUndoAction(
            new ScUndoPaste( pDocSh,
                aRange.aStart.Col(), aRange.aStart.Row(), aRange.aStart.Tab(),
                aRange.aEnd.Col(), aRange.aEnd.Row(), aRange.aEnd.Tab(), aDestMark,
                pUndoDoc, pRedoDoc, IDF_ALL, NULL,NULL,NULL,NULL ) );
    }
    pUndoDoc = NULL;
    if( pDocSh )
    {
        if (!bHeight)
            pDocSh->PostPaint( aRange, PAINT_GRID );    // AdjustRowHeight paintet evtl. selber
        pDocSh->SetDocumentModified();
    }
    ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
    if ( pViewSh )
        pViewSh->UpdateInputHandler();

}

/////////////////////////////////////////////////////////////////////////////


#if 0
BOOL ScImportExport::ImportData( SvData& rData )
{
    ULONG nFmt = rData.GetFormat();
    if ( nFmt == SOT_FORMATSTR_ID_HTML_SIMPLE )
    {
        MSE40HTMLClipFormatObj aMSE40ClpObj;
        if ( aMSE40ClpObj.GetData( rData ) )
        {
            SvStream* pStream = aMSE40ClpObj.GetStream();
            return ImportStream( *pStream, nFmt );
        }
        return FALSE;
    }
    else
    {
        void* pMem;
        ULONG nSize = rData.GetMinMemorySize();
        rData.GetData( &pMem, TRANSFER_REFERENCE );
        if( nFmt == FORMAT_STRING
                 || nFmt == FORMAT_RTF
                 || nFmt == SOT_FORMATSTR_ID_SYLK
                 || nFmt == SOT_FORMATSTR_ID_HTML
                 || nFmt == SOT_FORMATSTR_ID_DIF )
        {
            //! String? Unicode??

            // Stringende ermitteln!
            sal_Char* pBegin = (sal_Char*) pMem;
            sal_Char* pEnd   = (sal_Char*) pMem + nSize;

            nSize = 0;
            while( pBegin != pEnd && *pBegin != '\0' )
                pBegin++, nSize++;
            // #72909# MT says only STRING has to be zero-terminated
            DBG_ASSERT( pBegin != pEnd || nFmt != FORMAT_STRING, "non zero-terminated String" )
        }
        SvMemoryStream aStrm( pMem, nSize, STREAM_READ );
        return ImportStream( aStrm, nFmt );
    }
}

#endif

BOOL ScImportExport::ImportData( const String& rMimeType,
                     const ::com::sun::star::uno::Any & rValue )
{
    DBG_ASSERT( !this, "Implementation is missing" );
    return FALSE;
}

BOOL ScImportExport::ExportData( const String& rMimeType,
                                 ::com::sun::star::uno::Any & rValue )
{
    SvMemoryStream aStrm;
    if( ExportStream( aStrm,
                SotExchange::GetFormatIdFromMimeType( rMimeType ) ))
    {
        aStrm << (BYTE) 0;
        rValue <<= ::com::sun::star::uno::Sequence< sal_Int8 >(
                                        (sal_Int8*)aStrm.GetData(),
                                        aStrm.Seek( STREAM_SEEK_TO_END ) );
        return TRUE;
    }
    return FALSE;
}


// static
inline void ScImportExport::SetNoEndianSwap( SvStream& rStrm )
{
#ifdef __BIGENDIAN
    rStrm.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
#else
    rStrm.SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
#endif
}


BOOL ScImportExport::ImportString( const ::rtl::OUString& rText, ULONG nFmt )
{
    switch ( nFmt )
    {
        // formats supporting unicode
        case FORMAT_STRING :
        {
            SvMemoryStream aStrm( (void*)rText.getStr(), rText.getLength() * sizeof(sal_Unicode), STREAM_READ );
            aStrm.SetStreamCharSet( RTL_TEXTENCODING_UNICODE );
            SetNoEndianSwap( aStrm );       //! no swapping in memory
            return ImportStream( aStrm, nFmt );
            // ImportStream must handle RTL_TEXTENCODING_UNICODE
        }
        break;
        default:
        {
            rtl_TextEncoding eEnc = gsl_getSystemTextEncoding();
            ::rtl::OString aTmp( rText.getStr(), rText.getLength(), eEnc );
            SvMemoryStream aStrm( (void*)aTmp.getStr(), aTmp.getLength() * sizeof(sal_Char), STREAM_READ );
            aStrm.SetStreamCharSet( eEnc );
            SetNoEndianSwap( aStrm );       //! no swapping in memory
            return ImportStream( aStrm, nFmt );
        }
    }
}


BOOL ScImportExport::ExportString( String& rText, ULONG nFmt )
{
    DBG_ASSERT( nFmt == FORMAT_STRING, "ScImportExport::ExportString: Unicode not supported for other formats than FORMAT_STRING" );
    if ( nFmt != FORMAT_STRING )
    {
        rtl_TextEncoding eEnc = gsl_getSystemTextEncoding();
        ByteString aTmp;
        BOOL bOk = ExportByteString( aTmp, eEnc, nFmt );
        rText = UniString( aTmp, eEnc );
        return bOk;
    }
    if (!nSizeLimit)
        nSizeLimit = STRING_MAXLEN;

    SvMemoryStream aStrm;
    aStrm.SetStreamCharSet( RTL_TEXTENCODING_UNICODE );
    SetNoEndianSwap( aStrm );       //! no swapping in memory
    if( ExportStream( aStrm, nFmt ) )
    {
        aStrm << (sal_Unicode) 0;
        aStrm.Seek( STREAM_SEEK_TO_END );
        // Sicherheits-Check:
        if( aStrm.Tell() <= (ULONG) STRING_MAXLEN )
        {
            rText = (const sal_Unicode*) aStrm.GetData();
            return TRUE;
        }
    }
    rText.Erase();
    return FALSE;

    // ExportStream must handle RTL_TEXTENCODING_UNICODE
}


BOOL ScImportExport::ExportByteString( ByteString& rText, rtl_TextEncoding eEnc, ULONG nFmt )
{
    DBG_ASSERT( eEnc != RTL_TEXTENCODING_UNICODE, "ScImportExport::ExportByteString: Unicode not supported" );
    if ( eEnc == RTL_TEXTENCODING_UNICODE )
        eEnc = gsl_getSystemTextEncoding();

    if (!nSizeLimit)
        nSizeLimit = STRING_MAXLEN;

    SvMemoryStream aStrm;
    aStrm.SetStreamCharSet( eEnc );
    SetNoEndianSwap( aStrm );       //! no swapping in memory
    if( ExportStream( aStrm, nFmt ) )
    {
        aStrm << (sal_Char) 0;
        aStrm.Seek( STREAM_SEEK_TO_END );
        // Sicherheits-Check:
        if( aStrm.Tell() <= (ULONG) STRING_MAXLEN )
        {
            rText = (const sal_Char*) aStrm.GetData();
            return TRUE;
        }
    }
    rText.Erase();
    return FALSE;
}


BOOL ScImportExport::ImportStream( SvStream& rStrm, ULONG nFmt )
{
    if( nFmt == FORMAT_STRING )
    {
        if( ExtText2Doc( rStrm ) )      // pExtOptions auswerten
            return TRUE;
    }
    if( nFmt == SOT_FORMATSTR_ID_SYLK )
    {
        if( Sylk2Doc( rStrm ) )
            return TRUE;
    }
    if( nFmt == SOT_FORMATSTR_ID_DIF )
    {
        if( Dif2Doc( rStrm ) )
            return TRUE;
    }
    if( nFmt == FORMAT_RTF )
    {
        if( RTF2Doc( rStrm ) )
            return TRUE;
    }
    if( nFmt == SOT_FORMATSTR_ID_LINK )
        return TRUE;            // Link-Import?
    if ( nFmt == SOT_FORMATSTR_ID_HTML )
    {
        if( HTML2Doc( rStrm ) )
            return TRUE;
    }
    if ( nFmt == SOT_FORMATSTR_ID_HTML_SIMPLE )
    {
        if( HTML2Doc( rStrm ) )
            return TRUE;
    }

    return FALSE;
}


BOOL ScImportExport::ExportStream( SvStream& rStrm, ULONG nFmt )
{
    if( nFmt == FORMAT_STRING )
    {
        if( Doc2Text( rStrm ) )
            return TRUE;
    }
    if( nFmt == SOT_FORMATSTR_ID_SYLK )
    {
        if( Doc2Sylk( rStrm ) )
            return TRUE;
    }
    if( nFmt == SOT_FORMATSTR_ID_DIF )
    {
        if( Doc2Dif( rStrm ) )
            return TRUE;
    }
    if( nFmt == SOT_FORMATSTR_ID_LINK && !bAll )
    {
        String aDocName;
        if ( pDoc->IsClipboard() )
            aDocName = ScGlobal::GetClipDocName();
        else
        {
            SfxObjectShell* pShell = pDoc->GetDocumentShell();
            if (pShell)
                aDocName = pShell->GetTitle( SFX_TITLE_FULLNAME );
        }

        DBG_ASSERT( aDocName.Len(), "ClipBoard document has no name! :-/" );
        if( aDocName.Len() )
        {
            String aRefName;
            USHORT nFlags = SCA_VALID | SCA_TAB_3D;
            if( bSingle )
                aRange.aStart.Format( aRefName, nFlags, pDoc );
            else
            {
                if( aRange.aStart.Tab() != aRange.aEnd.Tab() )
                    nFlags |= SCA_TAB2_3D;
                aRange.Format( aRefName, nFlags, pDoc );
            }
            String aAppName = Application::GetAppName();

            WriteUnicodeOrByteString( rStrm, aAppName, TRUE );
            WriteUnicodeOrByteString( rStrm, aDocName, TRUE );
            WriteUnicodeOrByteString( rStrm, aRefName, TRUE );
            if ( rStrm.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE )
                rStrm << sal_Unicode(0);
            else
                rStrm << sal_Char(0);
            return BOOL( rStrm.GetError() == SVSTREAM_OK );
        }
    }
    if( nFmt == SOT_FORMATSTR_ID_HTML )
    {
        if( Doc2HTML( rStrm ) )
            return TRUE;
    }
    if( nFmt == FORMAT_RTF )
    {
        if( Doc2RTF( rStrm ) )
            return TRUE;
    }

    return FALSE;
}


//static
void ScImportExport::WriteUnicodeOrByteString( SvStream& rStrm, const String& rString, BOOL bZero )
{
    rtl_TextEncoding eEnc = rStrm.GetStreamCharSet();
    if ( eEnc == RTL_TEXTENCODING_UNICODE )
    {
        if ( !IsEndianSwap( rStrm ) )
            rStrm.Write( rString.GetBuffer(), rString.Len() * sizeof(sal_Unicode) );
        else
        {
            const sal_Unicode* p = rString.GetBuffer();
            const sal_Unicode* const pStop = p + rString.Len();
            while ( p < pStop )
            {
                rStrm << *p;
            }
        }
        if ( bZero )
            rStrm << sal_Unicode(0);
    }
    else
    {
        ByteString aByteStr( rString, eEnc );
        rStrm << aByteStr.GetBuffer();
        if ( bZero )
            rStrm << sal_Char(0);
    }
}


// static
void ScImportExport::WriteUnicodeOrByteEndl( SvStream& rStrm )
{
    if ( rStrm.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE )
    {   // same as endl() but unicode
        switch ( rStrm.GetLineDelimiter() )
        {
            case LINEEND_CR :
                rStrm << sal_Unicode(_CR);
            break;
            case LINEEND_LF :
                rStrm << sal_Unicode(_LF);
            break;
            default:
                rStrm << sal_Unicode(_CR) << sal_Unicode(_LF);
        }
    }
    else
        endl( rStrm );
}


enum DoubledQuoteMode
{
    DQM_KEEP,       // both are taken
    DQM_ESCAPE,     // escaped quote, one is taken, one ignored
    DQM_CONCAT,     // first is end, next is start, both ignored => strings combined
    DQM_SEPARATE    // end one string and begin next
};

const sal_Unicode* lcl_ScanString( const sal_Unicode* p, String& rString,
            sal_Unicode cStr, DoubledQuoteMode eMode )
{
    p++;    //! jump over opening quote
    BOOL bCont;
    do
    {
        bCont = FALSE;
        const sal_Unicode* p0 = p;
        for( ;; )
        {
            if( !*p )
                break;
            if( *p == cStr )
            {
                if ( *++p != cStr )
                    break;
                // doubled quote char
                switch ( eMode )
                {
                    case DQM_KEEP :
                        p++;            // both for us (not breaking for-loop)
                    break;
                    case DQM_ESCAPE :
                        p++;            // one for us (breaking for-loop)
                        bCont = TRUE;   // and more
                    break;
                    case DQM_CONCAT :
                        if ( p0+1 < p )
                            rString.Append( p0, (p-1) - p0 );   // first part
                        p0 = ++p;       // text of next part starts here
                    break;
                    case DQM_SEPARATE :
                                        // positioned on next opening quote
                    break;
                }
                if ( eMode == DQM_ESCAPE || eMode == DQM_SEPARATE )
                    break;
            }
            else
                p++;
        }
        if ( p0 < p )
            rString.Append( p0, ((*p || *(p-1) == cStr) ? p-1 : p) - p0 );
    } while ( bCont );
    return p;
}


void lcl_WriteString( SvStream& rStrm, String& rString, sal_Unicode cStr )
{
    xub_StrLen n = 0;
    while( ( n = rString.Search( cStr, n ) ) != STRING_NOTFOUND )
    {
        rString.Insert( cStr, n );
        n += 2;
    }

    rString.Insert( cStr, 0 );
    rString.Append( cStr );

    ScImportExport::WriteUnicodeOrByteString( rStrm, rString );
}

inline void lcl_WriteSimpleString( SvStream& rStrm, const String& rString )
{
    ScImportExport::WriteUnicodeOrByteString( rStrm, rString );
}

//////////////////////////////////////////////////////////////////////////////


BOOL ScImportExport::Text2Doc( SvStream& rStrm )
{
    BOOL bOk = TRUE;

    USHORT nStartCol = aRange.aStart.Col();
    USHORT nStartRow = aRange.aStart.Row();
    USHORT nEndCol = aRange.aEnd.Col();
    USHORT nEndRow = aRange.aEnd.Row();
    ULONG  nOldPos = rStrm.Tell();
    if ( rStrm.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE )
        rStrm.StartReadingUnicodeText();
    BOOL   bData = BOOL( !bSingle );
    if( !bSingle)
        bOk = StartPaste();

    while( bOk )
    {
        ByteString aByteLine;
        String aLine, aCell;
        USHORT nRow = nStartRow;
        rStrm.Seek( nOldPos );
        for( ;; )
        {
            rStrm.ReadUniOrByteStringLine( aLine );
            if( rStrm.IsEof() )
                break;
            USHORT nCol = nStartCol;
            const sal_Unicode* p = aLine.GetBuffer();
            while( *p )
            {
                aCell.Erase();
                if( *p == cStr )
                {
                    p = lcl_ScanString( p, aCell, cStr, DQM_KEEP );
                    while( *p && *p != cSep )
                        p++;
                    if( *p )
                        p++;
                }
                else
                {
                    const sal_Unicode* q = p;
                    while( *p && *p != cSep )
                        p++;
                    aCell.Assign( q, p - q );
                    if( *p )
                        p++;
                }
                if (nCol<=MAXCOL && nRow<=MAXROW )
                {
                    if( bSingle )
                    {
                        if (nCol>nEndCol) nEndCol = nCol;
                        if (nRow>nEndRow) nEndRow = nRow;
                    }
                    if( bData && nCol <= nEndCol && nRow <= nEndRow )
                        pDoc->SetString( nCol, nRow, aRange.aStart.Tab(), aCell );
                }
                else                            // zuviele Spalten/Zeilen
                    bOverflow = TRUE;           // beim Import Warnung ausgeben
                ++nCol;
            }
            ++nRow;
        }

        if( !bData )
        {
            aRange.aEnd.SetCol( nEndCol );
            aRange.aEnd.SetRow( nEndRow );
            bOk = StartPaste();
            bData = TRUE;
        }
        else
            break;
    }

    EndPaste();
    return bOk;
}

        //
        //  erweiterter Ascii-Import
        //


void lcl_PutString( ScDocument* pDoc, USHORT nCol, USHORT nRow, USHORT nTab,
                    const String& rStr, BYTE nColFormat,
                    ::utl::TransliterationWrapper& rTransliteration,
                    CalendarWrapper& rCalendar,
                    ::utl::TransliterationWrapper* pSecondTransliteration,
                    CalendarWrapper* pSecondCalendar )
{
    if ( nColFormat == SC_COL_SKIP || !rStr.Len() || nCol > MAXCOL || nRow > MAXROW )
        return;

    if ( nColFormat == SC_COL_TEXT )
    {
        pDoc->PutCell( nCol, nRow, nTab, new ScStringCell( rStr ) );
        return;
    }

    if ( nColFormat == SC_COL_ENGLISH )
    {
        //! SetString mit Extra-Flag ???

        SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
        ULONG nEnglish = pFormatter->GetStandardIndex(LANGUAGE_ENGLISH_US);
        double fVal;
        if ( pFormatter->IsNumberFormat( rStr, nEnglish, fVal ) )
        {
            //  Zahlformat wird nicht auf englisch gesetzt
            pDoc->SetValue( nCol, nRow, nTab, fVal );
            return;
        }
        //  sonst weiter mit SetString
    }
    else if ( nColFormat != SC_COL_STANDARD )                   // Datumsformate
    {
        //  nach genau drei Teilen suchen

        xub_StrLen nLen = rStr.Len();
        xub_StrLen nStart[3];
        xub_StrLen nEnd[3];
        USHORT nFound = 0;
        BOOL bInNum = FALSE;
        for ( xub_StrLen nPos=0; nPos<nLen; nPos++ )
        {
            if ( ScGlobal::pCharClass->isLetterNumeric( rStr, nPos ) )
            {
                if (!bInNum)
                {
                    if ( nFound >= 3 )
                        break;                  // zuviele Teile
                    bInNum = TRUE;
                    nStart[nFound] = nPos;
                    ++nFound;
                }
                nEnd[nFound-1] = nPos;
            }
            else
                bInNum = FALSE;
        }
        if ( nFound == 3 )              // genau 3 Teile ?
        {
            using namespace ::com::sun::star;
            BOOL bSecondCal = FALSE;
            USHORT nDP, nMP, nYP;
            switch ( nColFormat )
            {
                case SC_COL_YMD: nDP = 2; nMP = 1; nYP = 0; break;
                case SC_COL_MDY: nDP = 1; nMP = 0; nYP = 2; break;
                case SC_COL_DMY:
                default:         nDP = 0; nMP = 1; nYP = 2; break;
            }
            USHORT nDay  = (USHORT) rStr.Copy( nStart[nDP], nEnd[nDP]+1-nStart[nDP] ).ToInt32();
            USHORT nYear = (USHORT) rStr.Copy( nStart[nYP], nEnd[nYP]+1-nStart[nYP] ).ToInt32();
            String aMStr = rStr.Copy( nStart[nMP], nEnd[nMP]+1-nStart[nMP] );
            sal_Int16 nMonth = (sal_Int16) aMStr.ToInt32();
            if (!nMonth)
            {
                uno::Sequence< i18n::CalendarItem > xMonths;
                sal_Int32 i, nLen;
                //  first test all month names from local international
                xMonths = rCalendar.getMonths();
                nLen = xMonths.getLength();
                for (i=0; i<nLen && !nMonth; i++)
                {
                    if ( rTransliteration.isEqual( aMStr, xMonths[i].FullName ) ||
                         rTransliteration.isEqual( aMStr, xMonths[i].AbbrevName ) )
                        nMonth = i+1;
                }
                //  if none found, then test english month names
                if ( !nMonth && pSecondCalendar && pSecondTransliteration )
                {
                    xMonths = pSecondCalendar->getMonths();
                    nLen = xMonths.getLength();
                    for (i=0; i<nLen && !nMonth; i++)
                    {
                        if ( pSecondTransliteration->isEqual( aMStr, xMonths[i].FullName ) ||
                             pSecondTransliteration->isEqual( aMStr, xMonths[i].AbbrevName ) )
                        {
                            nMonth = i+1;
                            bSecondCal = TRUE;
                        }
                    }
                }
            }

            SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
            if ( nYear < 100 )
                nYear = pFormatter->ExpandTwoDigitYear( nYear );

            CalendarWrapper* pCalendar = (bSecondCal ? pSecondCalendar : &rCalendar);
            sal_Int16 nNumMonths = pCalendar->getNumberOfMonthsInYear();
            if ( nDay && nMonth && nDay<=31 && nMonth<=nNumMonths )
            {
                --nMonth;
                pCalendar->setValue( i18n::CalendarFieldIndex::DAY_OF_MONTH, nDay );
                pCalendar->setValue( i18n::CalendarFieldIndex::MONTH, nMonth );
                pCalendar->setValue( i18n::CalendarFieldIndex::YEAR, nYear );
//! TODO: validity check if XCalendar supports it
                DateTime aDateTime( pCalendar->getGregorianDateTime() );
                Date aNullDate = *pFormatter->GetNullDate();
                double nValue = aDateTime - aNullDate;

                LanguageType eLatin, eCjk, eCtl;
                pDoc->GetLanguage( eLatin, eCjk, eCtl );
                LanguageType eDocLang = eLatin;                 //! which language for date formats?

                long nFormat = pFormatter->GetStandardFormat( NUMBERFORMAT_DATE, eDocLang );

                pDoc->PutCell( nCol, nRow, nTab, new ScValueCell(nValue), nFormat, FALSE );

                return;         // Erfolg
            }
        }
    }

    //  Standard oder Datum nicht erkannt -> SetString

    pDoc->SetString( nCol, nRow, nTab, rStr );
}


String lcl_GetFixed( const String& rLine, xub_StrLen nStart, xub_StrLen nNext )
{
    xub_StrLen nLen = rLine.Len();
    if (nNext > nLen)
        nNext = nLen;
    if ( nNext <= nStart )
        return EMPTY_STRING;

    const sal_Unicode* pStr = rLine.GetBuffer();

    xub_StrLen nSpace = nNext;
    while ( nSpace > nStart && pStr[nSpace-1] == ' ' )
        --nSpace;

    return rLine.Copy( nStart, nSpace-nStart );
}


BOOL ScImportExport::ExtText2Doc( SvStream& rStrm )
{
    if (!pExtOptions)
        return Text2Doc( rStrm );

    ULONG nOldPos = rStrm.Tell();
    rStrm.Seek( STREAM_SEEK_TO_END );
    ScProgress aProgress( pDocSh, ScGlobal::GetRscString( STR_LOAD_DOC ), rStrm.Tell() - nOldPos );
    rStrm.Seek( nOldPos );
    if ( rStrm.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE )
        rStrm.StartReadingUnicodeText();

    BOOL bOld = ScColumn::bDoubleAlloc;
    ScColumn::bDoubleAlloc = TRUE;

    DBG_ASSERT( !bUndo, "ExtText2Doc mit Undo noch nicht implementiert!" );
    USHORT nStartCol = aRange.aStart.Col();
    USHORT nStartRow = aRange.aStart.Row();
    USHORT nTab = aRange.aStart.Tab();

    BOOL    bFixed          = pExtOptions->IsFixedLen();
    const sal_Unicode* pSeps = pExtOptions->GetFieldSeps().GetBuffer();
    BOOL    bMerge          = pExtOptions->IsMergeSeps();
    USHORT  nInfoCount      = pExtOptions->GetInfoCount();
    const xub_StrLen* pColStart = pExtOptions->GetColStart();
    const BYTE* pColFormat  = pExtOptions->GetColFormat();
    long nSkipLines = pExtOptions->GetStartRow();

    LanguageType eLatin, eCjk, eCtl;
    pDoc->GetLanguage( eLatin, eCjk, eCtl );
    LanguageType eDocLang = eLatin;                 //! which language for date formats?

    // For date recognition
    ::utl::TransliterationWrapper aTransliteration(
        pDoc->GetServiceManager(), SC_TRANSLITERATION_IGNORECASE );
    aTransliteration.loadModuleIfNeeded( eDocLang );
    CalendarWrapper aCalendar( pDoc->GetServiceManager() );
    aCalendar.loadDefaultCalendar(
        SvNumberFormatter::ConvertLanguageToLocale( eDocLang ) );
    ::utl::TransliterationWrapper* pEnglishTransliteration = NULL;
    CalendarWrapper* pEnglishCalendar = NULL;
    if ( eDocLang != LANGUAGE_ENGLISH_US )
    {
        pEnglishTransliteration = new ::utl::TransliterationWrapper (
            pDoc->GetServiceManager(), SC_TRANSLITERATION_IGNORECASE );
        aTransliteration.loadModuleIfNeeded( LANGUAGE_ENGLISH_US );
        pEnglishCalendar = new CalendarWrapper ( pDoc->GetServiceManager() );
        pEnglishCalendar->loadDefaultCalendar(
            SvNumberFormatter::ConvertLanguageToLocale( LANGUAGE_ENGLISH_US ) );
    }

    String aLine, aCell;
    USHORT i;
    USHORT nRow = nStartRow;

    while(--nSkipLines>0)
    {
        rStrm.ReadUniOrByteStringLine( aLine );     // content is ignored
        if ( rStrm.IsEof() )
            break;
    }
    for( ;; )
    {
        rStrm.ReadUniOrByteStringLine( aLine );
        if ( rStrm.IsEof() )
            break;

        xub_StrLen nLineLen = aLine.Len();
        USHORT nCol = nStartCol;
        if ( bFixed )               //  Feste Satzlaenge
        {
            for ( i=0; i<nInfoCount; i++ )
            {
                if ( pColFormat[i] != SC_COL_SKIP )     // sonst auch nCol nicht hochzaehlen
                {
                    xub_StrLen nStart = pColStart[i];
                    xub_StrLen nNext = ( i+1 < nInfoCount ) ? pColStart[i+1] : nLineLen;
                    aCell = lcl_GetFixed( aLine, nStart, nNext );
                    lcl_PutString( pDoc, nCol, nRow, nTab, aCell, pColFormat[i],
                        aTransliteration, aCalendar, pEnglishTransliteration, pEnglishCalendar );
                    ++nCol;
                }
            }
        }
        else                        //  Nach Trennzeichen suchen
        {
            USHORT nSourceCol = 0;
            USHORT nInfoStart = 0;
            const sal_Unicode* p = aLine.GetBuffer();
            while (*p)
            {
                p = ScImportExport::ScanNextFieldFromString( p, aCell, cStr, pSeps, bMerge );

                BYTE nFmt = SC_COL_STANDARD;
                for ( i=nInfoStart; i<nInfoCount; i++ )
                {
                    if ( pColStart[i] == nSourceCol + 1 )       // pColStart ist 1-basiert
                    {
                        nFmt = pColFormat[i];
                        nInfoStart = i + 1;     // ColInfos sind in Reihenfolge
                        break;  // for
                    }
                }
                if ( nFmt != SC_COL_SKIP )
                {
                    lcl_PutString( pDoc, nCol, nRow, nTab, aCell, nFmt,
                        aTransliteration, aCalendar, pEnglishTransliteration, pEnglishCalendar );
                    ++nCol;
                }

                ++nSourceCol;
            }
        }

        aProgress.SetStateOnPercent( rStrm.Tell() - nOldPos );
        ++nRow;
        if ( nRow > MAXROW )
        {
            bOverflow = TRUE;           // beim Import Warnung ausgeben
            break;
        }
    }

    ScColumn::bDoubleAlloc = bOld;
    pDoc->DoColResize( nTab, 0, MAXCOL, 0 );

    delete pEnglishTransliteration;
    delete pEnglishCalendar;

    return TRUE;
}


// static
const sal_Unicode* ScImportExport::ScanNextFieldFromString( const sal_Unicode* p,
        String& rField, sal_Unicode cStr, const sal_Unicode* pSeps, BOOL bMergeSeps )
{
    rField.Erase();
    if ( *p == cStr )           // String in Anfuehrungszeichen
    {
        p = lcl_ScanString( p, rField, cStr, DQM_ESCAPE );
        while ( *p && !ScGlobal::UnicodeStrChr( pSeps, *p ) )
            p++;
        if( *p )
            p++;
    }
    else                        // bis zum Trennzeichen
    {
        const sal_Unicode* p0 = p;
        while ( *p && !ScGlobal::UnicodeStrChr( pSeps, *p ) )
            p++;
        rField.Append( p0, p - p0 );
        if( *p )
            p++;
    }
    if ( bMergeSeps )           // folgende Trennzeichen ueberspringen
    {
        while ( *p && ScGlobal::UnicodeStrChr( pSeps, *p ) )
            p++;
    }
    return p;
}

        //
        //
        //


BOOL ScImportExport::Doc2Text( SvStream& rStrm )
{
    USHORT nCol;
    USHORT nRow;
    USHORT nStartCol = aRange.aStart.Col();
    USHORT nStartRow = aRange.aStart.Row();
    USHORT nEndCol = aRange.aEnd.Col();
    USHORT nEndRow = aRange.aEnd.Row();
    String aCell;

    for (nRow = nStartRow; nRow <= nEndRow; nRow++)
    {
        for (nCol = nStartCol; nCol <= nEndCol; nCol++)
        {
            CellType eType;
            pDoc->GetCellType( nCol, nRow, aRange.aStart.Tab(), eType );
            switch (eType)
            {
                case CELLTYPE_FORMULA:
                {
                    if (bFormulas)
                    {
                        pDoc->GetFormula( nCol, nRow, aRange.aStart.Tab(), aCell, TRUE );
                        if( aCell.Search( cSep ) != STRING_NOTFOUND )
                            lcl_WriteString( rStrm, aCell, cStr );
                        else
                            lcl_WriteSimpleString( rStrm, aCell );
                    }
                    else
                    {
                        pDoc->GetString( nCol, nRow, aRange.aStart.Tab(), aCell );
                        if( aCell.Search( cSep ) != STRING_NOTFOUND )
                            lcl_WriteString( rStrm, aCell, cStr );
                        else
                            lcl_WriteSimpleString( rStrm, aCell );
                    }
                }
                break;
                case CELLTYPE_VALUE:
                {
                    pDoc->GetString( nCol, nRow, aRange.aStart.Tab(), aCell );
                    lcl_WriteSimpleString( rStrm, aCell );
                }
                break;
                case CELLTYPE_NOTE:
                case CELLTYPE_NONE:
                break;
                default:
                {
                    pDoc->GetString( nCol, nRow, aRange.aStart.Tab(), aCell );
                    if( aCell.Search( cSep ) != STRING_NOTFOUND )
                        lcl_WriteString( rStrm, aCell, cStr );
                    else
                        lcl_WriteSimpleString( rStrm, aCell );
                }
            }
            if( nCol < nEndCol )
                lcl_WriteSimpleString( rStrm, String(cSep) );
        }
//      if( nRow < nEndRow )
            WriteUnicodeOrByteEndl( rStrm );
        if( rStrm.GetError() != SVSTREAM_OK )
            break;
        if( nSizeLimit && rStrm.Tell() > nSizeLimit )
            break;
    }

    return BOOL( rStrm.GetError() == SVSTREAM_OK );
}


BOOL ScImportExport::Sylk2Doc( SvStream& rStrm )
{
    BOOL bOk = TRUE;
    BOOL bMyDoc = FALSE;

    // US-English separators for StringToDouble
    sal_Unicode cDecSep = '.';
    sal_Unicode cGrpSep = ',';

    USHORT nStartCol = aRange.aStart.Col();
    USHORT nStartRow = aRange.aStart.Row();
    USHORT nEndCol = aRange.aEnd.Col();
    USHORT nEndRow = aRange.aEnd.Row();
    ULONG nOldPos = rStrm.Tell();
    BOOL bData = BOOL( !bSingle );

    if( !bSingle)
        bOk = StartPaste();

    while( bOk )
    {
        String aLine;
        String aText;
        ByteString aByteLine;
        USHORT nCol = nStartCol;
        USHORT nRow = nStartRow;
        USHORT nRefCol, nRefRow;
        nRefCol = nRefRow = 1;
        rStrm.Seek( nOldPos );
        for( ;; )
        {
            //! allow unicode
            rStrm.ReadLine( aByteLine );
            aLine = String( aByteLine, rStrm.GetStreamCharSet() );
            if( rStrm.IsEof() )
                break;
            const sal_Unicode* p = aLine.GetBuffer();
            sal_Unicode cTag = *p++;
            if( cTag == 'I' && *p == 'D' )
            {
                aLine.Erase( 0, 4 );
                bMyDoc = aLine.EqualsAscii( "SCALC3" );
            }
            else if( cTag == 'E' )                      // Ende
                break;
            else if( cTag == 'C' || cTag == 'F' )       // in F kann die Position gesetzt werden
            {
                if( *p++ != ';' )
                    return FALSE;
                while( *p )
                {
                    sal_Unicode ch = *p++;
                    ch = ScGlobal::ToUpperAlpha( ch );
                    switch( ch )
                    {
                        case 'X':
                            nCol = String( p ).ToInt32() + nStartCol - 1;
                            break;
                        case 'Y':
                            nRow = String( p ).ToInt32() + nStartRow - 1;
                            break;
                        case 'C':
                            nRefCol = String( p ).ToInt32() + nStartCol - 1;
                            break;
                        case 'R':
                            nRefRow = String( p ).ToInt32() + nStartRow - 1;
                            break;
                        case 'K':
                        {
                            if( cTag != 'C' )           // nur bei 'C'
                                break;
                            if( !bSingle &&
                              ( nCol < nStartCol || nCol > nEndCol
                             || nRow < nStartRow || nRow > nEndRow
                             || nCol > MAXCOL || nRow > MAXROW ) )
                                break;
                            if( !bData )
                            {
                                if( nRow > nEndRow )
                                    nEndRow = nRow;
                                if( nCol > nEndCol )
                                    nEndCol = nCol;
                                break;
                            }
                            BOOL bText;
                            if( *p == '"' )
                            {
                                bText = TRUE;
                                aText.Erase();
                                p = lcl_ScanString( p, aText, '"', DQM_ESCAPE );
                            }
                            else
                                bText = FALSE;
                            const sal_Unicode* q = p;
                            while( *q && *q != ';' )
                                q++;
                            if ( !(*q == ';' && *(q+1) == 'I') )
                            {   // don't ignore value
                                if( bText )
                                    pDoc->SetString( nCol, nRow, aRange.aStart.Tab(), aText );
                                else
                                {
                                    int nErr;
                                    double fVal = SolarMath::StringToDouble( p, cGrpSep, cDecSep, nErr );
                                    pDoc->SetValue( nCol, nRow, aRange.aStart.Tab(), fVal );
                                }
                            }
                        }
                        break;
                        case 'E':
                        case 'M':
                        {
                            if( !bMyDoc || !bData )
                                break;
                            aText = '=';
                            if( *p == '"' )
                                p = lcl_ScanString( p, aText, '"', DQM_ESCAPE );
                            else
                            {
                                const sal_Unicode* q = p;
                                while( *p && *p != ';' )
                                    p++;
                                aText.Append( q, p-q );
                            }
                            ScAddress aPos( nCol, nRow, aRange.aStart.Tab() );
                            ScCompiler aComp( pDoc, aPos );
                            aComp.SetCompileEnglish( TRUE );
                            ScTokenArray* pCode = aComp.CompileString( aText );
                            if ( ch == 'M' )
                            {
                                if ( nRefCol < nCol )
                                    nRefCol = nCol;
                                if ( nRefRow < nRow )
                                    nRefRow = nRow;
                                ScMarkData aMark;
                                aMark.SelectTable( aPos.Tab(), TRUE );
                                pDoc->InsertMatrixFormula( nCol, nRow, nRefCol,
                                    nRefRow, aMark, EMPTY_STRING, pCode );
                            }
                            else
                            {
                                ScFormulaCell* pFCell = new ScFormulaCell( pDoc, aPos, pCode, 0 );
                                pDoc->PutCell( aPos, pFCell );
                            }
                            delete pCode;   // ctor/InsertMatrixFormula did copy TokenArray
                        }
                        break;
                    }
                    while( *p && *p != ';' )
                        p++;
                    if( *p )
                        p++;
                }
            }
        }
        if( !bData )
        {
            aRange.aEnd.SetCol( nEndCol );
            aRange.aEnd.SetRow( nEndRow );
            bOk = StartPaste();
            bData = TRUE;
        }
        else
            break;
    }

    EndPaste();
    return bOk;
}


BOOL ScImportExport::Doc2Sylk( SvStream& rStrm )
{
    USHORT nCol;
    USHORT nRow;
    USHORT nStartCol = aRange.aStart.Col();
    USHORT nStartRow = aRange.aStart.Row();
    USHORT nEndCol = aRange.aEnd.Col();
    USHORT nEndRow = aRange.aEnd.Row();
    String aCellStr;
    String aValStr;
    lcl_WriteSimpleString( rStrm,
            String::CreateFromAscii(RTL_CONSTASCII_STRINGPARAM( "ID;PSCALC3" )) );
    WriteUnicodeOrByteEndl( rStrm );

    for (nRow = nStartRow; nRow <= nEndRow; nRow++)
    {
        for (nCol = nStartCol; nCol <= nEndCol; nCol++)
        {
            String aBufStr;
            double nVal;
            BOOL bForm = FALSE;
            USHORT r = nRow - nStartRow + 1;
            USHORT c = nCol - nStartCol + 1;
            ScBaseCell* pCell;
            pDoc->GetCell( nCol, nRow, aRange.aStart.Tab(), pCell );
            CellType eType = (pCell ? pCell->GetCellType() : CELLTYPE_NONE);
            switch( eType )
            {
                case CELLTYPE_FORMULA:
                    bForm = bFormulas;
                    if( pDoc->HasValueData( nCol, nRow, aRange.aStart.Tab()) )
                        goto hasvalue;
                    else
                        goto hasstring;

                case CELLTYPE_VALUE:
                hasvalue:
                    pDoc->GetValue( nCol, nRow, aRange.aStart.Tab(), nVal );
                    //sprintf( cBuf, "C;X%d;Y%d;K%lg", c, r, nVal );

                    aValStr.Erase();
                    SolarMath::DoubleToString( aValStr, nVal, 'A', INT_MAX, '.', TRUE );

                    aBufStr.AssignAscii(RTL_CONSTASCII_STRINGPARAM( "C;X" ));
                    aBufStr += String::CreateFromInt32( c );
                    aBufStr.AppendAscii(RTL_CONSTASCII_STRINGPARAM( ";Y" ));
                    aBufStr += String::CreateFromInt32( r );
                    aBufStr.AppendAscii(RTL_CONSTASCII_STRINGPARAM( ";K" ));
                    aBufStr += aValStr;
                    lcl_WriteSimpleString( rStrm, aBufStr );
                    goto checkformula;

                case CELLTYPE_STRING:
                case CELLTYPE_EDIT:
                hasstring:
                    pDoc->GetString( nCol, nRow, aRange.aStart.Tab(), aCellStr );
                    //sprintf( cBuf, "C;X%d;Y%d;K", c, r );

                    aBufStr.AssignAscii(RTL_CONSTASCII_STRINGPARAM( "C;X" ));
                    aBufStr += String::CreateFromInt32( c );
                    aBufStr.AppendAscii(RTL_CONSTASCII_STRINGPARAM( ";Y" ));
                    aBufStr += String::CreateFromInt32( r );
                    aBufStr.AppendAscii(RTL_CONSTASCII_STRINGPARAM( ";K" ));
                    lcl_WriteSimpleString( rStrm, aBufStr );
                    lcl_WriteString( rStrm, aCellStr, '"' );

                checkformula:
                    if( bForm )
                    {
                        const ScFormulaCell* pFCell =
                            static_cast<const ScFormulaCell*>(pCell);
                        switch ( pFCell->GetMatrixFlag() )
                        {
                            case MM_REFERENCE :
                                aCellStr.Erase();
                            break;
                            default:
                                pFCell->GetEnglishFormula( aCellStr );
                        }
                        if ( pFCell->GetMatrixFlag() != MM_NONE &&
                                aCellStr.Len() > 2 &&
                                aCellStr.GetChar(0) == '{' &&
                                aCellStr.GetChar(aCellStr.Len()-1) == '}' )
                        {   // cut off matrix {} characters
                            aCellStr.Erase(aCellStr.Len()-1,1);
                            aCellStr.Erase(0,1);
                        }
                        if ( aCellStr.GetChar(0) == '=' )
                            aCellStr.Erase(0,1);
                        String aPrefix;
                        switch ( pFCell->GetMatrixFlag() )
                        {
                            case MM_FORMULA :
                            {   // diff expression with 'M' M$-extension
                                USHORT nC, nR;
                                pFCell->GetMatColsRows( nC, nR );
                                nC += c - 1;
                                nR += r - 1;
                                aPrefix.AssignAscii( RTL_CONSTASCII_STRINGPARAM( ";R" ) );
                                aPrefix += String::CreateFromInt32( nR );
                                aPrefix.AppendAscii( RTL_CONSTASCII_STRINGPARAM( ";C" ) );
                                aPrefix += String::CreateFromInt32( nC );
                                aPrefix.AppendAscii( RTL_CONSTASCII_STRINGPARAM( ";M" ) );
                            }
                            break;
                            case MM_REFERENCE :
                            {   // diff expression with 'I' M$-extension
                                ScAddress aPos;
                                pFCell->GetMatrixOrigin( aPos );
                                aPrefix.AssignAscii( RTL_CONSTASCII_STRINGPARAM( ";I;R" ) );
                                aPrefix += String::CreateFromInt32( aPos.Row() - nStartRow + 1 );
                                aPrefix.AppendAscii( RTL_CONSTASCII_STRINGPARAM( ";C" ) );
                                aPrefix += String::CreateFromInt32( aPos.Col() - nStartCol + 1 );
                            }
                            break;
                            default:
                                // formula Expression
                                aPrefix.AssignAscii( RTL_CONSTASCII_STRINGPARAM( ";E" ) );
                        }
                        lcl_WriteSimpleString( rStrm, aPrefix );
                        if ( aCellStr.Len() )
                        {
                            if( aCellStr.Search( ';' ) != STRING_NOTFOUND )
                                lcl_WriteString( rStrm, aCellStr, '"' );
                            else
                                lcl_WriteSimpleString( rStrm, aCellStr );
                        }
                    }
                    WriteUnicodeOrByteEndl( rStrm );
                    break;
            }
        }
    }
    lcl_WriteSimpleString( rStrm, String( 'E' ) );
    WriteUnicodeOrByteEndl( rStrm );
    return BOOL( rStrm.GetError() == SVSTREAM_OK );
}


BOOL ScImportExport::Doc2HTML( SvStream& rStrm )
{
    // CharSet is ignored in ScExportHTML, read from Load/Save HTML options
    ScExportHTML( rStrm, pDoc, aRange, RTL_TEXTENCODING_DONTKNOW, bAll,
        aStreamPath, aNonConvertibleChars );
    return BOOL( rStrm.GetError() == SVSTREAM_OK );
}

BOOL ScImportExport::Doc2RTF( SvStream& rStrm )
{
    //  CharSet is ignored in ScExportRTF
    ScExportRTF( rStrm, pDoc, aRange, RTL_TEXTENCODING_DONTKNOW );
    return BOOL( rStrm.GetError() == SVSTREAM_OK );
}


BOOL ScImportExport::Doc2Dif( SvStream& rStrm )
{
    ScExportDif( rStrm, pDoc, aRange, gsl_getSystemTextEncoding() );
    return TRUE;
}


BOOL ScImportExport::Dif2Doc( SvStream& rStrm )
{
    USHORT nTab = aRange.aStart.Tab();
    ScDocument* pImportDoc = new ScDocument( SCDOCMODE_UNDO );
    pImportDoc->InitUndo( pDoc, nTab, nTab );

    ScImportDif( rStrm, pImportDoc, aRange.aStart, gsl_getSystemTextEncoding() );

    USHORT nEndCol, nEndRow;
    pImportDoc->GetCellArea( nTab, nEndCol, nEndRow );
    aRange.aEnd = ScAddress( nEndCol, nEndRow, nTab );

    BOOL bOk = StartPaste();
    if (bOk)
    {
        USHORT nFlags = IDF_ALL & ~IDF_STYLES;
        pDoc->DeleteAreaTab( aRange, nFlags );
        pImportDoc->CopyToDocument( aRange, nFlags, FALSE, pDoc );
        EndPaste();
    }

    delete pImportDoc;

    return bOk;
}


BOOL ScImportExport::RTF2Doc( SvStream& rStrm )
{
    ScRTFImport aImp( pDoc, aRange );
    aImp.Read( rStrm );
    aRange = aImp.GetRange();

    BOOL bOk = StartPaste();
    if (bOk)
    {
        USHORT nFlags = IDF_ALL & ~IDF_STYLES;
        pDoc->DeleteAreaTab( aRange, nFlags );
        aImp.WriteToDocument();
        EndPaste();
    }

    return bOk;
}


BOOL ScImportExport::HTML2Doc( SvStream& rStrm )
{
    ScHTMLImport aImp( pDoc, aRange );
    aImp.Read( rStrm );
    aRange = aImp.GetRange();

    BOOL bOk = StartPaste();
    if (bOk)
    {
        //  ScHTMLImport may call ScDocument::InitDrawLayer, resulting in
        //  a DrawLayer but no DrawView -> create DrawLayer and View here
        if (pDocSh)
            pDocSh->MakeDrawLayer();

        USHORT nFlags = IDF_ALL & ~IDF_STYLES;
        pDoc->DeleteAreaTab( aRange, nFlags );
        aImp.WriteToDocument();
        EndPaste();
    }

    return bOk;
}