forked from Konctantin/Excel4Delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcel4Delphi.Stream.pas
6922 lines (6278 loc) · 246 KB
/
Excel4Delphi.Stream.pas
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
unit Excel4Delphi.Stream;
interface
uses
{$IFDEF MSWINDOWS}
Winapi.Windows,
{$ENDIF}
System.SysUtils,
System.Classes,
System.Types,
{$IFDEF FMX}
FMX.Graphics,
{$ELSE}
Vcl.Graphics,
{$ENDIF}
System.UITypes,
System.Zip,
System.IOUtils,
System.Generics.Collections,
Excel4Delphi.Formula,
Excel4Delphi.Xml,
Excel4Delphi,
Excel4Delphi.Common;
type
TRelationType = (
rtNone = -1,
rtWorkSheet = 0,
rtStyles = 1,
rtSharedStr = 2,
rtDoc = 3,
rtCoreProp = 4,
rtExtProps = 5,
rtHyperlink = 6,
rtComments = 7,
rtVmlDrawing = 8,
rtDrawing = 9
);
TZXLSXFileItem = record
name: string; //путь к файлу
nameArx: string;
original: string; //исходная строка
ftype: TRelationType; //тип контента
end;
TZXLSXRelations = record
id: string; //rID
ftype: TRelationType; //тип ссылки
target: string; //ссылка на файла
fileid: integer; //ссылка на запись
name: string; //имя листа
state: string; //cостояние, возможные значения см. https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-xlsb/74cb1d22-b931-4bf8-997d-17517e2416e9
sheetid: integer; //номер листа
end;
PZXLSXRelations = ^TZXLSXRelations;
TZXLSXRelationsArray = array of TZXLSXRelations;
TZXLSXDiffBorderItemStyle = class(TPersistent)
private
FUseStyle: boolean; //заменять ли стиль
FUseColor: boolean; //заменять ли цвет
FColor: TColor; //цвет линий
FLineStyle: TZBorderType; //стиль линий
FWeight: byte;
protected
public
constructor Create();
procedure Clear();
procedure Assign(Source: TPersistent); override;
property UseStyle: boolean read FUseStyle write FUseStyle;
property UseColor: boolean read FUseColor write FUseColor;
property Color: TColor read FColor write FColor;
property LineStyle: TZBorderType read FLineStyle write FLineStyle;
property Weight: byte read FWeight write FWeight;
end;
TZXLSXDiffBorder = class(TPersistent)
private
FBorder: array [0..5] of TZXLSXDiffBorderItemStyle;
procedure SetBorder(Num: TZBordersPos; Const Value: TZXLSXDiffBorderItemStyle);
function GetBorder(Num: TZBordersPos): TZXLSXDiffBorderItemStyle;
public
constructor Create(); virtual;
destructor Destroy(); override;
procedure Clear();
procedure Assign(Source: TPersistent);override;
property Border[Num: TZBordersPos]: TZXLSXDiffBorderItemStyle read GetBorder write SetBorder; default;
end;
//Итем для дифференцированного форматирования
//TODO:
// возможно, для excel xml тоже понадобится (перенести?)
TZXLSXDiffFormattingItem = class(TPersistent)
private
FUseFont: boolean; //заменять ли шрифт
FUseFontColor: boolean; //заменять ли цвет шрифта
FUseFontStyles: boolean; //заменять ли стиль шрифта
FFontColor: TColor; //цвет шрифта
FFontStyles: TFontStyles; //стиль шрифта
FUseBorder: boolean; //заменять ли рамку
FBorders: TZXLSXDiffBorder; //Что менять в рамке
FUseFill: boolean; //заменять ли заливку
FUseCellPattern: boolean; //Заменять ли тип заливки
FCellPattern: TZCellPattern; //тип заливки
FUseBGColor: boolean; //заменять ли цвет заливки
FBGColor: TColor; //цвет заливки
FUsePatternColor: boolean; //Заменять ли цвет шаблона заливки
FPatternColor: TColor; //Цвет шаблона заливки
protected
public
constructor Create();
destructor Destroy(); override;
procedure Clear();
procedure Assign(Source: TPersistent); override;
property UseFont: boolean read FUseFont write FUseFont;
property UseFontColor: boolean read FUseFontColor write FUseFontColor;
property UseFontStyles: boolean read FUseFontStyles write FUseFontStyles;
property FontColor: TColor read FFontColor write FFontColor;
property FontStyles: TFontStyles read FFontStyles write FFontStyles;
property UseBorder: boolean read FUseBorder write FUseBorder;
property Borders: TZXLSXDiffBorder read FBorders write FBorders;
property UseFill: boolean read FUseFill write FUseFill;
property UseCellPattern: boolean read FUseCellPattern write FUseCellPattern;
property CellPattern: TZCellPattern read FCellPattern write FCellPattern;
property UseBGColor: boolean read FUseBGColor write FUseBGColor;
property BGColor: TColor read FBGColor write FBGColor;
property UsePatternColor: boolean read FUsePatternColor write FUsePatternColor;
property PatternColor: TColor read FPatternColor write FPatternColor;
end;
// Differential formating
TZXLSXDiffFormatting = class(TPersistent)
private
FCount: integer;
FMaxCount: integer;
FItems: array of TZXLSXDiffFormattingItem;
protected
function GetItem(num: integer): TZXLSXDiffFormattingItem;
procedure SetItem(num: integer; const Value: TZXLSXDiffFormattingItem);
procedure SetCount(ACount: integer);
public
constructor Create();
destructor Destroy(); override;
procedure Add();
procedure Assign(Source: TPersistent); override;
procedure Clear();
property Count: integer read FCount;
property Items[num: integer]: TZXLSXDiffFormattingItem read GetItem write SetItem; default;
end;
//List of cell number formats (date/numbers/currencies etc formats)
TZEXLSXNumberFormats = class
private
FFormatsCount: integer;
FFormats: array of string; //numFmts (include default formats)
FStyleFmtID: array of integer;
FStyleFmtIDCount: integer;
protected
function GetFormat(num: integer): string;
procedure SetFormat(num: integer; const value: string);
function GetStyleFMTID(num: integer): integer;
procedure SetStyleFMTID(num: integer; const value: integer);
procedure SetStyleFMTCount(value: integer);
public
constructor Create();
destructor Destroy(); override;
procedure ReadNumFmts(const xml: TZsspXMLReaderH);
function IsDateFormat(StyleNum: integer): boolean;
function FindFormatID(const value: string): integer;
property FormatsCount: integer read FFormatsCount;
property Format[num: integer]: string read GetFormat write SetFormat; default;
property StyleFMTID[num: integer]: integer read GetStyleFMTID write SetStyleFMTID;
property StyleFMTCount: integer read FStyleFmtIDCount write SetStyleFMTCount;
end;
TZEXLSXReadHelper = class
private
FDiffFormatting: TZXLSXDiffFormatting;
FNumberFormats: TZEXLSXNumberFormats;
protected
procedure SetDiffFormatting(const Value: TZXLSXDiffFormatting);
public
constructor Create();
destructor Destroy(); override;
property DiffFormatting: TZXLSXDiffFormatting read FDiffFormatting write SetDiffFormatting;
property NumberFormats: TZEXLSXNumberFormats read FNumberFormats;
end;
//Store link item
TZEXLSXHyperLinkItem = record
RID: integer;
RelType: TRelationType;
CellRef: string;
Target: string;
ScreenTip: string;
TargetMode: string;
end;
{ TZEXLSXWriteHelper }
TZEXLSXWriteHelper = class
private
FHyperLinks: array of TZEXLSXHyperLinkItem;
FHyperLinksCount: integer;
FMaxHyperLinksCount: integer;
FCurrentRID: integer; //Current rID number (for HyperLinks/comments etc)
FisHaveComments: boolean; //Is Need create comments*.xml?
FisHaveDrawings: boolean; //Is Need create drawings*.xml?
FSheetHyperlinksArray: array of integer;
FSheetHyperlinksCount: integer;
protected
function GenerateRID(): integer;
public
constructor Create();
destructor Destroy(); override;
procedure AddHyperLink(const ACellRef, ATarget, AScreenTip, ATargetMode: string);
function AddDrawing(const ATarget: string): integer;
procedure WriteHyperLinksTag(const xml: TZsspXMLWriterH);
function CreateSheetRels(const Stream: TStream; TextConverter: TAnsiToCPConverter; CodePageName: string; BOM: ansistring): integer;
procedure AddSheetHyperlink(PageNum: integer);
function IsSheetHaveHyperlinks(PageNum: integer): boolean;
procedure Clear();
property HyperLinksCount: integer read FHyperLinksCount;
property isHaveComments: boolean read FisHaveComments write FisHaveComments; //Is need create comments*.xml?
property isHaveDrawings: boolean read FisHaveDrawings write FisHaveDrawings; //Is need create drawings*.xml?
end;
TZEXMLSSHelper = class helper for TZWorkBook
private
{}
public
procedure LoadFromStream(stream: TStream);
procedure LoadFromFile(fileName: string);
procedure SaveToStream(stream: TStream);
procedure SaveToFile(fileName: string);
end;
//Дополнительные функции для экспорта отдельных файлов
function ZEXLSXCreateStyles(var XMLSS: TZWorkBook; Stream: TStream; TextConverter: TAnsiToCPConverter; CodePageName: string; BOM: ansistring): integer;
function ZEXLSXCreateWorkBook(var XMLSS: TZWorkBook; Stream: TStream; const _pages: TIntegerDynArray; const _names: TStringDynArray; PageCount: integer; TextConverter: TAnsiToCPConverter; CodePageName: String; BOM: ansistring): integer;
function ZEXLSXCreateSheet(var XMLSS: TZWorkBook; Stream: TStream; SheetNum: integer; staredStrings: TSharedStringBank; TextConverter: TAnsiToCPConverter; CodePageName: String; BOM: ansistring; const WriteHelper: TZEXLSXWriteHelper): integer;
function ZEXLSXCreateContentTypes(var XMLSS: TZWorkBook; Stream: TStream; PageCount: integer; CommentCount: integer; const PagesComments: TIntegerDynArray; TextConverter: TAnsiToCPConverter; CodePageName: string; BOM: ansistring; const WriteHelper: TZEXLSXWriteHelper): integer;
function ZEXLSXCreateRelsMain(Stream: TStream; TextConverter: TAnsiToCPConverter; CodePageName: string; BOM: ansistring): integer;
function ZEXLSXCreateSharedStrings(var XMLSS: TZWorkBook; Stream: TStream; const SharedStrings: TSharedStringBank; TextConverter: TAnsiToCPConverter; CodePageName: string; BOM: ansistring): integer;
function ZEXLSXCreateDocPropsApp(Stream: TStream; TextConverter: TAnsiToCPConverter; CodePageName: string; BOM: ansistring): integer;
function ZEXLSXCreateDocPropsCore(var XMLSS: TZWorkBook; Stream: TStream; TextConverter: TAnsiToCPConverter; CodePageName: string; BOM: ansistring): integer;
function ZEXLSXCreateDrawing(sheet: TZSheet; Stream: TStream; TextConverter: TAnsiToCPConverter; CodePageName: String; BOM: ansistring): integer;
function ZEXLSXCreateDrawingRels(sheet: TZSheet; Stream: TStream; TextConverter: TAnsiToCPConverter; CodePageName: String; BOM: ansistring): integer;
procedure ZEAddRelsRelation(xml: TZsspXMLWriterH; const rid: string; ridType: TRelationType; const Target: string; const TargetMode: string = '');
function ReadXLSXPath(var XMLSS: TZWorkBook; DirName: string): integer;
function ReadXLSXFile(var XMLSS: TZWorkBook; zipStream: TStream): integer;
function SaveXmlssToXLSXPath(var XMLSS: TZWorkBook; PathName: string; const SheetsNumbers: array of integer; const SheetsNames: array of string; TextConverter: TAnsiToCPConverter; CodePageName: string; BOM: ansistring = ''): integer; overload;
function SaveXmlssToXLSXPath(var XMLSS: TZWorkBook; PathName: string; const SheetsNumbers: array of integer; const SheetsNames: array of string): integer; overload;
function SaveXmlssToXLSXPath(var XMLSS: TZWorkBook; PathName: string): integer; overload;
function SaveXmlssToXLSX(var XMLSS: TZWorkBook; zipStream: TStream; const SheetsNumbers: array of integer; const SheetsNames: array of string; TextConverter: TAnsiToCPConverter; CodePageName: string; BOM: ansistring = ''): integer;
//Дополнительные функции, на случай чтения отдельного файла
function ZEXSLXReadTheme(var Stream: TStream; var ThemaFillsColors: TIntegerDynArray; var ThemaColorCount: integer): boolean;
function ZEXSLXReadContentTypes(var Stream: TStream; var FileArray: TArray<TZXLSXFileItem>; var FilesCount: integer): boolean;
function ZEXSLXReadSharedStrings(var Stream: TStream; sharedStrings: TObjectList<TRichText>): boolean;
function ZEXSLXReadStyles(var XMLSS: TZWorkBook; var Stream: TStream; var ThemaFillsColors: TIntegerDynArray; var ThemaColorCount: integer; var MaximumDigitWidth: double; ReadHelper: TZEXLSXReadHelper): boolean;
function ZE_XSLXReadRelationships(var Stream: TStream; var Relations: TZXLSXRelationsArray; var RelationsCount: integer; var isWorkSheet: boolean; needReplaceDelimiter: boolean): boolean;
function ZEXSLXReadWorkBook(var XMLSS: TZWorkBook; var Stream: TStream; var Relations: TZXLSXRelationsArray; var RelationsCount: integer): boolean;
function ZEXSLXReadSheet(var XMLSS: TZWorkBook; var Stream: TStream; const SheetRelations: PZXLSXRelations; sharedStrings: TObjectList<TRichText>; var Relations: TZXLSXRelationsArray; RelationsCount: integer; MaximumDigitWidth: double; ReadHelper: TZEXLSXReadHelper): boolean;
function ZEXSLXReadComments(var XMLSS: TZWorkBook; var Stream: TStream): boolean;
implementation
uses
System.AnsiStrings,
System.StrUtils,
System.Math,
System.NetEncoding,
Excel4Delphi.NumberFormat;
const
SCHEMA_DOC = 'http://schemas.openxmlformats.org/officeDocument/2006';
SCHEMA_DOC_REL = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
SCHEMA_PACKAGE = 'http://schemas.openxmlformats.org/package/2006';
SCHEMA_PACKAGE_REL = 'http://schemas.openxmlformats.org/package/2006/relationships';
SCHEMA_SHEET_MAIN = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';
type
TZEXLSXFont = record
name: string;
bold: boolean;
italic: boolean;
underline: boolean;
strike: boolean;
charset: integer;
color: TColor;
ColorType: byte;
LumFactor: double;
fontsize: double;
superscript: boolean;
subscript: boolean;
end;
TZEXLSXFontArray = array of TZEXLSXFont;
TContentTypeRec=record
ftype: TRelationType;
name : string;
rel : string;
end;
const
CONTENT_TYPES: array[0..10] of TContentTypeRec = (
(ftype: TRelationType.rtWorkSheet;
name:'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml';
rel: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet'),
(ftype: TRelationType.rtStyles;
name:'application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml';
rel: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles'),
(ftype: TRelationType.rtSharedStr;
name:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml';
rel: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings'),
(ftype: TRelationType.rtSharedStr;
name:'application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml';
rel: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings'),
(ftype: TRelationType.rtDoc;
name:'application/vnd.openxmlformats-package.relationships+xml';
rel: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument'),
(ftype: TRelationType.rtCoreProp;
name:'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml';
rel: 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties'),
(ftype: TRelationType.rtExtProps;
name:'application/vnd.openxmlformats-package.core-properties+xml';
rel: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties'),
(ftype: TRelationType.rtHyperlink;
name:'application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml';
rel: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink'),
(ftype: TRelationType.rtComments;
name:'application/vnd.openxmlformats-officedocument.vmlDrawing';
rel: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments'),
(ftype: TRelationType.rtVmlDrawing;
name:'application/vnd.openxmlformats-officedocument.theme+xml';
rel: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing'),
(ftype: TRelationType.rtDrawing;
name:'application/vnd.openxmlformats-officedocument.drawing+xml';
rel: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing')
);
function GetMaximumDigitWidth(fontName: string; fontSize: double): double;
const
numbers = '0123456789';
var
bitmap: TBitmap;
number: string;
begin
//А.А.Валуев Расчитываем ширину самого широкого числа.
Result := 0;
bitmap := TBitmap.Create;
try
bitmap.Canvas.Font.PixelsPerInch := 96;
bitmap.Canvas.Font.Size := Trunc(fontSize);
bitmap.Canvas.Font.Name := fontName;
for number in numbers do
Result := Max(Result, bitmap.Canvas.TextWidth(number));
finally
bitmap.Free;
end;
end;
////::::::::::::: TZXLSXDiffFormating :::::::::::::::::////
constructor TZXLSXDiffFormatting.Create();
var i: integer;
begin
FCount := 0;
FMaxCount := 20;
SetLength(FItems, FMaxCount);
for i := 0 to FMaxCount - 1 do
FItems[i] := TZXLSXDiffFormattingItem.Create();
end;
destructor TZXLSXDiffFormatting.Destroy();
var i: integer;
begin
for i := 0 to FMaxCount - 1 do
if (Assigned(FItems[i])) then
FreeAndNil(FItems[i]);
inherited;
end;
procedure TZXLSXDiffFormatting.Add();
begin
SetCount(FCount + 1);
FItems[FCount - 1].Clear();
end;
procedure TZXLSXDiffFormatting.Assign(Source: TPersistent);
var df: TZXLSXDiffFormatting; i: integer; b: boolean;
begin
b := true;
if (Assigned(Source)) then
if (Source is TZXLSXDiffFormatting) then begin
b := false;
df := Source as TZXLSXDiffFormatting;
SetCount(df.Count);
for i := 0 to Count - 1 do
FItems[i].Assign(df[i]);
end;
if (b) then
inherited;
end; //Assign
procedure TZXLSXDiffFormatting.SetCount(ACount: integer);
var i: integer;
begin
if (ACount >= FMaxCount) then begin
FMaxCount := ACount + 20;
SetLength(FItems, FMaxCount);
//Здесь FCount + 1, потому что иначе затирается последний элемент.
//В результате утечки и потеря считанного форматирования.
for i := FCount + 1 to FMaxCount - 1 do
FItems[i] := TZXLSXDiffFormattingItem.Create();
end;
FCount := ACount;
end;
procedure TZXLSXDiffFormatting.Clear();
begin
FCount := 0;
end;
function TZXLSXDiffFormatting.GetItem(num: integer): TZXLSXDiffFormattingItem;
begin
if ((num >= 0) and (num < Count)) then
result := FItems[num]
else
result := nil;
end;
procedure TZXLSXDiffFormatting.SetItem(num: integer; const Value: TZXLSXDiffFormattingItem);
begin
if ((num >= 0) and (num < Count)) then
if (Assigned(Value)) then
FItems[num].Assign(Value);
end;
////::::::::::::: TZXLSXDiffBorderItemStyle :::::::::::::::::////
constructor TZXLSXDiffBorderItemStyle.Create();
begin
Clear();
end;
procedure TZXLSXDiffBorderItemStyle.Assign(Source: TPersistent);
var bs: TZXLSXDiffBorderItemStyle; b: boolean;
begin
b := true;
if (Assigned(Source)) then
if (Source is TZXLSXDiffBorderItemStyle) then begin
b := false;
bs := Source as TZXLSXDiffBorderItemStyle;
FUseStyle := bs.UseStyle;
FUseColor := bs.UseColor;
FColor := bs.Color;
FWeight := bs.Weight;
FLineStyle := bs.LineStyle;
end;
if (b) then
inherited;
end; //Assign
procedure TZXLSXDiffBorderItemStyle.Clear();
begin
FUseStyle := false;
FUseColor := false;
FColor := clBlack;
FWeight := 1;
FLineStyle := ZENone;
end;
////::::::::::::: TZXLSXDiffBorder :::::::::::::::::////
constructor TZXLSXDiffBorder.Create();
var i: integer;
begin
for i := 0 to 5 do
FBorder[i] := TZXLSXDiffBorderItemStyle.Create();
Clear();
end;
destructor TZXLSXDiffBorder.Destroy();
var i: integer;
begin
for i := 0 to 5 do
FreeAndNil(FBorder[i]);
inherited;
end;
procedure TZXLSXDiffBorder.Assign(Source: TPersistent);
var brd: TZXLSXDiffBorder; b: boolean; i: TZBordersPos;
begin
b := true;
if (Assigned(Source)) then
if (Source is TZXLSXDiffBorder) then begin
b := false;
brd := Source as TZXLSXDiffBorder;
for i := bpLeft to bpDiagonalRight do
FBorder[Ord(i)].Assign(brd[i]);
end;
if (b) then
inherited;
end; //Assign
procedure TZXLSXDiffBorder.Clear();
var i: TZBordersPos;
begin
for i := bpLeft to bpDiagonalRight do
FBorder[Ord(i)].Clear();
end;
function TZXLSXDiffBorder.GetBorder(Num: TZBordersPos): TZXLSXDiffBorderItemStyle;
begin
result := nil;
if ((num >= bpLeft) and (num <= bpDiagonalRight)) then
result := FBorder[ord(num)];
end;
procedure TZXLSXDiffBorder.SetBorder(Num: TZBordersPos; const Value: TZXLSXDiffBorderItemStyle);
begin
if ((num >= bpLeft) and (num <= bpDiagonalRight)) then
if (Assigned(Value)) then
FBorder[Ord(num)].Assign(Value);
end;
////::::::::::::: TZXLSXDiffFormatingItem :::::::::::::::::////
constructor TZXLSXDiffFormattingItem.Create();
begin
FBorders := TZXLSXDiffBorder.Create();
Clear();
end;
destructor TZXLSXDiffFormattingItem.Destroy();
begin
FreeAndNil(FBorders);
inherited;
end;
procedure TZXLSXDiffFormattingItem.Assign(Source: TPersistent);
var dxfItem: TZXLSXDiffFormattingItem; b: boolean;
begin
b := true;
if (Assigned(Source)) then
if (Source is TZXLSXDiffFormattingItem) then begin
b := false;
dxfItem := Source as TZXLSXDiffFormattingItem;
FUseFont := dxfItem.UseFont;
FUseFontColor := dxfItem.UseFontColor;
FUseFontStyles := dxfItem.UseFontStyles;
FFontColor := dxfItem.FontColor;
FFontStyles := dxfItem.FontStyles;
FUseBorder := dxfItem.UseBorder;
FUseFill := dxfItem.UseFill;
FUseCellPattern := dxfItem.UseCellPattern;
FCellPattern := dxfItem.CellPattern;
FUseBGColor := dxfItem.UseBGColor;
FBGColor := dxfItem.BGColor;
FUsePatternColor := dxfItem.UsePatternColor;
FPatternColor := dxfItem.PatternColor;
FBorders.Assign(dxfItem.Borders);
end;
if (b) then
inherited;
end; //Assign
procedure TZXLSXDiffFormattingItem.Clear();
begin
FUseFont := false;
FUseFontColor := false;
FUseFontStyles := false;
FFontColor := clBlack;
FFontStyles := [];
FUseBorder := false;
FBorders.Clear();
FUseFill := false;
FUseCellPattern := false;
FCellPattern := ZPNone;
FUseBGColor := false;
FBGColor := clWindow;
FUsePatternColor := false;
FPatternColor := clWindow;
end; //Clear
// END Differential Formatting
////////////////////////////////////////////////////////////////////////////////
////::::::::::::: TZEXLSXNumberFormats :::::::::::::::::////
constructor TZEXLSXNumberFormats.Create();
var i: integer;
begin
FStyleFmtIDCount := 0;
FFormatsCount := 164;
SetLength(FFormats, FFormatsCount);
for i := 0 to FFormatsCount - 1 do
FFormats[i] := '';
//Some "Standart" formats for xlsx:
FFormats[1] := '0';
FFormats[2] := '0.00';
FFormats[3] := '#,##0';
FFormats[4] := '#,##0.00';
FFormats[5] := '$#,##0;\-$#,##0';
FFormats[6] := '$#,##0;[Red]\-$#,##0';
FFormats[7] := '$#,##0.00;\-$#,##0.00';
FFormats[8] := '$#,##0.00;[Red]\-$#,##0.00';
FFormats[9] := '0%';
FFormats[10] := '0.00%';
FFormats[11] := '0.00E+00';
FFormats[12] := '# ?/?';
FFormats[13] := '# ??/??';
FFormats[14] := 'm/d/yyyy';
FFormats[15] := 'd-mmm-yy';
FFormats[16] := 'd-mmm';
FFormats[17] := 'mmm-yy';
FFormats[18] := 'h:mm AM/PM';
FFormats[19] := 'h:mm:ss AM/PM';
FFormats[20] := 'h:mm';
FFormats[21] := 'h:mm:ss';
FFormats[22] := 'm/d/yyyy h:mm';
FFormats[27] := '[$-404]e/m/d';
FFormats[37] := '#,##0 ;(#,##0)';
FFormats[38] := '#,##0 ;[Red](#,##0)';
FFormats[39] := '#,##0.00;(#,##0.00)';
FFormats[40] := '#,##0.00;[Red](#,##0.00)';
FFormats[44] := '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)';
FFormats[45] := 'mm:ss';
FFormats[46] := '[h]:mm:ss';
FFormats[47] := 'mmss.0';
FFormats[48] := '##0.0E+0';
FFormats[49] := '@';
FFormats[30] := 'm/d/yy';
FFormats[59] := 't0';
FFormats[60] := 't0.00';
FFormats[61] := 't#,##0';
FFormats[62] := 't#,##0.00';
FFormats[67] := 't0%';
FFormats[68] := 't0.00%';
FFormats[69] := 't# ?/?';
FFormats[70] := 't# ??/??';
FFormats[81] := 'd/m/bb';
end;
destructor TZEXLSXNumberFormats.Destroy();
begin
SetLength(FFormats, 0);
SetLength(FStyleFmtID, 0);
inherited;
end;
//Find format in formats. Return -1 if format not foud.
//INPUT
// const value: string - format
//RETURN
// integer - >= 0 - index number if store.
// -1 - not found
function TZEXLSXNumberFormats.FindFormatID(const value: string): integer;
var i: integer;
begin
Result := -1;
for i := 0 to FFormatsCount - 1 do
if (FFormats[i] = value) then begin
Result := i;
break;
end;
end;
function TZEXLSXNumberFormats.GetFormat(num: integer): string;
begin
Result := '';
if ((num >= 0) and (num < FFormatsCount)) then
Result := FFormats[num];
end;
procedure TZEXLSXNumberFormats.SetFormat(num: integer; const value: string);
var i: integer;
begin
if ((num >= 0) and (num < FFormatsCount)) then
FFormats[num] := value
else if (num >= 0) then begin
SetLength(FFormats, num + 1);
for i := FFormatsCount to num do
FFormats[i] := '';
FFormats[num] := value;
FFormatsCount := num + 1;
end;
end;
function TZEXLSXNumberFormats.GetStyleFMTID(num: integer): integer;
begin
if ((num >= 0) and (num < FStyleFmtIDCount)) then
Result := FStyleFmtID[num]
else
Result := 0;
end;
function TZEXLSXNumberFormats.IsDateFormat(StyleNum: integer): boolean;
var fmtId: integer;
begin
Result := false;
if ((StyleNum >= 0) and (StyleNum < FStyleFmtIDCount)) then
fmtId := FStyleFmtID[StyleNum]
else
exit;
//If default fmtID
if ((fmtId >= 0) and (fmtId < 100)) then
Result := fmtId in [14..22, 27..36, 45..47, 50..58, 71..76, 78..81]
else
Result := GetXlsxNumberFormatType(FFormats[fmtId]) = ZE_NUMFORMAT_IS_DATETIME;
end;
procedure TZEXLSXNumberFormats.ReadNumFmts(const xml: TZsspXMLReaderH);
var temp: integer;
begin
with THTMLEncoding.Create do
try
while xml.ReadToEndTagByName('numFmts') do begin
if (xml.TagName = 'numFmt') then
if (TryStrToInt(xml.Attributes['numFmtId'], temp)) then
Format[temp] := Decode(xml.Attributes['formatCode']);
end;
finally
Free;
end;
end;
procedure TZEXLSXNumberFormats.SetStyleFMTID(num: integer; const value: integer);
begin
if ((num >= 0) and (num < FStyleFmtIDCount)) then
FStyleFmtID[num] := value;
end;
procedure TZEXLSXNumberFormats.SetStyleFMTCount(value: integer);
begin
if (value >= 0) then begin
if (value > FStyleFmtIDCount) then
SetLength(FStyleFmtID, value);
FStyleFmtIDCount := value
end;
end;
////::::::::::::: TZEXLSXReadHelper :::::::::::::::::////
constructor TZEXLSXReadHelper.Create();
begin
FDiffFormatting := TZXLSXDiffFormatting.Create();
FNumberFormats := TZEXLSXNumberFormats.Create();
end;
destructor TZEXLSXReadHelper.Destroy();
begin
FreeAndNil(FDiffFormatting);
FreeAndNil(FNumberFormats);
inherited;
end;
procedure TZEXLSXReadHelper.SetDiffFormatting(const Value: TZXLSXDiffFormatting);
begin
if (Assigned(Value)) then
FDiffFormatting.Assign(Value);
end;
////::::::::::::: TZEXLSXWriteHelper :::::::::::::::::////
//Generate next RID for references
function TZEXLSXWriteHelper.GenerateRID(): integer;
begin
inc(FCurrentRID);
result := FCurrentRID;
end;
constructor TZEXLSXWriteHelper.Create();
begin
FMaxHyperLinksCount := 10;
FSheetHyperlinksCount := 0;
SetLength(FHyperLinks, FMaxHyperLinksCount);
Clear();
end;
destructor TZEXLSXWriteHelper.Destroy();
begin
SetLength(FHyperLinks, 0);
SetLength(FSheetHyperlinksArray, 0);
inherited Destroy;
end;
procedure TZEXLSXWriteHelper.AddHyperLink(const ACellRef, ATarget, AScreenTip, ATargetMode: string);
var num: integer;
begin
num := FHyperLinksCount;
inc(FHyperLinksCount);
if (FHyperLinksCount >= FMaxHyperLinksCount) then begin
inc(FMaxHyperLinksCount, 20);
SetLength(FHyperLinks, FMaxHyperLinksCount);
end;
FHyperLinks[num].RID := GenerateRID();
FHyperLinks[num].RelType := TRelationType.rtHyperlink;
FHyperLinks[num].TargetMode := ATargetMode;
FHyperLinks[num].CellRef := ACellRef;
FHyperLinks[num].Target := ATarget;
FHyperLinks[num].ScreenTip := AScreenTip;
end;
//Add hyperlink
//INPUT
// const ATarget: string - drawing target (../drawings/drawing2.xml)
function TZEXLSXWriteHelper.AddDrawing(const ATarget: string): integer;
var num: integer;
begin
num := FHyperLinksCount;
Inc(FHyperLinksCount);
if (FHyperLinksCount >= FMaxHyperLinksCount) then begin
Inc(FMaxHyperLinksCount, 20);
SetLength(FHyperLinks, FMaxHyperLinksCount);
end;
FHyperLinks[num].RID := GenerateRID();
FHyperLinks[num].RelType := TRelationType.rtDrawing;
FHyperLinks[num].TargetMode := '';
FHyperLinks[num].CellRef := '';
FHyperLinks[num].Target := ATarget;
FHyperLinks[num].ScreenTip := '';
Result := FHyperLinks[num].RID;
FisHaveDrawings := True;
end;
//Writes tag <hyperlinks> .. </hyperlinks>
//INPUT
// const xml: TZsspXMLWriterH
procedure TZEXLSXWriteHelper.WriteHyperLinksTag(const xml: TZsspXMLWriterH);
var i: integer;
begin
if (FHyperLinksCount > 0) then begin
xml.Attributes.Clear();
xml.WriteTagNode('hyperlinks', true, true, true);
for i := 0 to FHyperLinksCount - 1 do begin
xml.Attributes.Clear();
xml.Attributes.Add('ref', FHyperLinks[i].CellRef);
xml.Attributes.Add('r:id', 'rId' + IntToStr(FHyperLinks[i].RID));
if (FHyperLinks[i].ScreenTip <> '') then
xml.Attributes.Add('tooltip', FHyperLinks[i].ScreenTip);
xml.WriteEmptyTag('hyperlink', true);
{
xml.Attributes.Add('Id', 'rId' + IntToStr(FHyperLinks[i].ID));
xml.Attributes.Add('Type', ZEXLSXGetRelationName(6));
xml.Attributes.Add('Target', FHyperLinks[i].Target);
if (FHyperLinks[i].TargetMode <> '')
xml.Attributes.Add('TargetMode', FHyperLinks[i].TargetMode);
}
end; //for i
xml.WriteEndTagNode(); //hyperlinks
end; //if
end; //WriteHyperLinksTag
//Create sheet relations
//INPUT
// const Stream: TStream
// TextConverter: TAnsiToCPConverter
// CodePageName: string
// BOM: ansistring
//RETURN
function TZEXLSXWriteHelper.CreateSheetRels(const Stream: TStream;
TextConverter: TAnsiToCPConverter;
CodePageName: string; BOM: ansistring): integer;
var xml: TZsspXMLWriterH; i: integer;
begin
result := 0;
xml := TZsspXMLWriterH.Create(Stream);
try
xml.TabLength := 1;
xml.TextConverter := TextConverter;
xml.TabSymbol := ' ';
xml.WriteHeader(CodePageName, BOM);
xml.Attributes.Clear();
xml.Attributes.Add('xmlns', SCHEMA_PACKAGE_REL);
xml.WriteTagNode('Relationships', true, true, false);
for i := 0 to FHyperLinksCount - 1 do
ZEAddRelsRelation(xml,
'rId' + IntToStr(FHyperLinks[i].RID),
FHyperLinks[i].RelType,
FHyperLinks[i].Target,
FHyperLinks[i].TargetMode);
xml.WriteEndTagNode(); //Relationships
finally
xml.Free();
end;
end; //CreateSheetRels
procedure TZEXLSXWriteHelper.AddSheetHyperlink(PageNum: integer);
begin
SetLength(FSheetHyperlinksArray, FSheetHyperlinksCount + 1);
FSheetHyperlinksArray[FSheetHyperlinksCount] := PageNum;
inc(FSheetHyperlinksCount);
end;
function TZEXLSXWriteHelper.IsSheetHaveHyperlinks(PageNum: integer): boolean;
var i: integer;
begin
result := false;
for i := 0 to FSheetHyperlinksCount - 1 do
if (FSheetHyperlinksArray[i] = PageNum) then
exit(true);
end;
procedure TZEXLSXWriteHelper.Clear();
begin
FHyperLinksCount := 0;
FCurrentRID := 0;
FisHaveComments := false;
end;
//Возвращает номер Relations из rels
//INPUT
// const name: string - текст отношения
//RETURN
// integer - номер отношения. -1 - не определено
function ZEXLSXGetRelationNumber(const name: string): TRelationType;
var rec: TContentTypeRec;
begin
result := TRelationType.rtNone;
for rec in CONTENT_TYPES do begin
if rec.rel = name then
exit(rec.ftype);
end;
end; //ZEXLSXGetRelationNumber
//Возвращает текст Relations для rels
//INPUT
// num: integer - номер отношения
//RETURN
// integer - номер отношения. -1 - не определено
function ZEXLSXGetRelationName(num: TRelationType): string;
var rec: TContentTypeRec;
begin
result := '';
for rec in CONTENT_TYPES do begin
if rec.ftype = num then
exit(rec.rel);
end;
end; //ZEXLSXGetRelationName
function XLSXBoolToStr(value: boolean): string;
begin
if (value) then
result := 'true'
else
result := 'false';
end;
//Читает тему (themeXXX.xml)
//INPUT
// var Stream: TStream - поток чтения
// var ThemaFillsColors: TIntegerDynArray - массив с цветами заливки
// var ThemaColorCount: integer - кол-во цветов заливки
//RETURN
// boolean - true - всё прочиталось успешно