-
Notifications
You must be signed in to change notification settings - Fork 285
/
zcl_excel_reader_2007.clas.abap
4463 lines (3760 loc) · 192 KB
/
zcl_excel_reader_2007.clas.abap
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
CLASS zcl_excel_reader_2007 DEFINITION
PUBLIC
CREATE PUBLIC .
PUBLIC SECTION.
*"* public components of class ZCL_EXCEL_READER_2007
*"* do not include other source files here!!!
INTERFACES zif_excel_reader .
CLASS-METHODS fill_struct_from_attributes
IMPORTING
!ip_element TYPE REF TO if_ixml_element
CHANGING
!cp_structure TYPE any .
PROTECTED SECTION.
TYPES:
*"* protected components of class ZCL_EXCEL_READER_2007
*"* do not include other source files here!!!
BEGIN OF t_relationship,
id TYPE string,
type TYPE string,
target TYPE string,
targetmode TYPE string,
worksheet TYPE REF TO zcl_excel_worksheet,
sheetid TYPE string, "ins #235 - repeat rows/cols - needed to identify correct sheet
localsheetid TYPE string,
END OF t_relationship .
TYPES:
BEGIN OF t_fileversion,
appname TYPE string,
lastedited TYPE string,
lowestedited TYPE string,
rupbuild TYPE string,
codename TYPE string,
END OF t_fileversion .
TYPES:
BEGIN OF t_sheet,
name TYPE string,
sheetid TYPE string,
id TYPE string,
state TYPE string,
END OF t_sheet .
TYPES:
BEGIN OF t_workbookpr,
codename TYPE string,
defaultthemeversion TYPE string,
END OF t_workbookpr .
TYPES:
BEGIN OF t_sheetpr,
codename TYPE string,
END OF t_sheetpr .
TYPES:
BEGIN OF t_range,
name TYPE string,
hidden TYPE string, "inserted with issue #235 because Autofilters didn't passthrough
localsheetid TYPE string, " issue #163
END OF t_range .
TYPES:
t_fills TYPE STANDARD TABLE OF REF TO zcl_excel_style_fill WITH NON-UNIQUE DEFAULT KEY .
TYPES:
t_borders TYPE STANDARD TABLE OF REF TO zcl_excel_style_borders WITH NON-UNIQUE DEFAULT KEY .
TYPES:
t_fonts TYPE STANDARD TABLE OF REF TO zcl_excel_style_font WITH NON-UNIQUE DEFAULT KEY .
TYPES:
t_style_refs TYPE STANDARD TABLE OF REF TO zcl_excel_style WITH NON-UNIQUE DEFAULT KEY .
TYPES:
BEGIN OF t_color,
indexed TYPE string,
rgb TYPE string,
theme TYPE string,
tint TYPE string,
END OF t_color .
TYPES:
BEGIN OF t_rel_drawing,
id TYPE string,
content TYPE xstring,
file_ext TYPE string,
content_xml TYPE REF TO if_ixml_document,
END OF t_rel_drawing .
TYPES:
t_rel_drawings TYPE STANDARD TABLE OF t_rel_drawing WITH NON-UNIQUE DEFAULT KEY .
TYPES:
BEGIN OF gts_external_hyperlink,
id TYPE string,
target TYPE string,
END OF gts_external_hyperlink .
TYPES:
gtt_external_hyperlinks TYPE HASHED TABLE OF gts_external_hyperlink WITH UNIQUE KEY id .
TYPES:
BEGIN OF ty_ref_formulae,
sheet TYPE REF TO zcl_excel_worksheet,
row TYPE i,
column TYPE i,
si TYPE i,
ref TYPE string,
formula TYPE string,
END OF ty_ref_formulae .
TYPES:
tyt_ref_formulae TYPE HASHED TABLE OF ty_ref_formulae WITH UNIQUE KEY sheet row column .
TYPES:
BEGIN OF t_shared_string,
value TYPE string,
rtf TYPE zexcel_t_rtf,
END OF t_shared_string .
TYPES:
t_shared_strings TYPE STANDARD TABLE OF t_shared_string WITH DEFAULT KEY .
TYPES:
BEGIN OF t_table,
id TYPE string,
target TYPE string,
END OF t_table .
TYPES:
t_tables TYPE HASHED TABLE OF t_table WITH UNIQUE KEY id .
DATA shared_strings TYPE t_shared_strings .
DATA styles TYPE t_style_refs .
DATA mt_ref_formulae TYPE tyt_ref_formulae .
DATA mt_dxf_styles TYPE zexcel_t_styles_cond_mapping .
METHODS fill_row_outlines
IMPORTING
!io_worksheet TYPE REF TO zcl_excel_worksheet
RAISING
zcx_excel .
METHODS get_from_zip_archive
IMPORTING
!i_filename TYPE string
RETURNING
VALUE(r_content) TYPE xstring
RAISING
zcx_excel .
METHODS get_ixml_from_zip_archive
IMPORTING
!i_filename TYPE string
!is_normalizing TYPE abap_bool DEFAULT 'X'
RETURNING
VALUE(r_ixml) TYPE REF TO if_ixml_document
RAISING
zcx_excel .
METHODS load_drawing_anchor
IMPORTING
!io_anchor_element TYPE REF TO if_ixml_element
!io_worksheet TYPE REF TO zcl_excel_worksheet
!it_related_drawings TYPE t_rel_drawings .
METHODS load_shared_strings
IMPORTING
!ip_path TYPE string
RAISING
zcx_excel .
METHODS load_styles
IMPORTING
!ip_path TYPE string
!ip_excel TYPE REF TO zcl_excel
RAISING
zcx_excel .
METHODS load_dxf_styles
IMPORTING
!iv_path TYPE string
!io_excel TYPE REF TO zcl_excel
RAISING
zcx_excel .
METHODS load_style_borders
IMPORTING
!ip_xml TYPE REF TO if_ixml_document
RETURNING
VALUE(ep_borders) TYPE t_borders .
METHODS load_style_fills
IMPORTING
!ip_xml TYPE REF TO if_ixml_document
RETURNING
VALUE(ep_fills) TYPE t_fills .
METHODS load_style_font
IMPORTING
!io_xml_element TYPE REF TO if_ixml_element
RETURNING
VALUE(ro_font) TYPE REF TO zcl_excel_style_font .
METHODS load_style_fonts
IMPORTING
!ip_xml TYPE REF TO if_ixml_document
RETURNING
VALUE(ep_fonts) TYPE t_fonts .
METHODS load_style_num_formats
IMPORTING
!ip_xml TYPE REF TO if_ixml_document
RETURNING
VALUE(ep_num_formats) TYPE zcl_excel_style_number_format=>t_num_formats .
METHODS load_workbook
IMPORTING
!iv_workbook_full_filename TYPE string
!io_excel TYPE REF TO zcl_excel
RAISING
zcx_excel .
METHODS load_worksheet
IMPORTING
!ip_path TYPE string
!io_worksheet TYPE REF TO zcl_excel_worksheet
RAISING
zcx_excel .
METHODS load_worksheet_cond_format
IMPORTING
!io_ixml_worksheet TYPE REF TO if_ixml_document
!io_worksheet TYPE REF TO zcl_excel_worksheet
RAISING
zcx_excel .
METHODS load_worksheet_cond_format_aa
IMPORTING
!io_ixml_rule TYPE REF TO if_ixml_element
!io_style_cond TYPE REF TO zcl_excel_style_cond.
METHODS load_worksheet_cond_format_ci
IMPORTING
!io_ixml_rule TYPE REF TO if_ixml_element
!io_style_cond TYPE REF TO zcl_excel_style_cond .
METHODS load_worksheet_cond_format_cs
IMPORTING
!io_ixml_rule TYPE REF TO if_ixml_element
!io_style_cond TYPE REF TO zcl_excel_style_cond .
METHODS load_worksheet_cond_format_ex
IMPORTING
!io_ixml_rule TYPE REF TO if_ixml_element
!io_style_cond TYPE REF TO zcl_excel_style_cond .
METHODS load_worksheet_cond_format_is
IMPORTING
!io_ixml_rule TYPE REF TO if_ixml_element
!io_style_cond TYPE REF TO zcl_excel_style_cond .
METHODS load_worksheet_cond_format_db
IMPORTING
!io_ixml_rule TYPE REF TO if_ixml_element
!io_style_cond TYPE REF TO zcl_excel_style_cond .
METHODS load_worksheet_cond_format_t10
IMPORTING
!io_ixml_rule TYPE REF TO if_ixml_element
!io_style_cond TYPE REF TO zcl_excel_style_cond .
METHODS load_worksheet_drawing
IMPORTING
!ip_path TYPE string
!io_worksheet TYPE REF TO zcl_excel_worksheet
RAISING
zcx_excel .
METHODS load_comments
IMPORTING
ip_path TYPE string
io_worksheet TYPE REF TO zcl_excel_worksheet
RAISING
zcx_excel .
METHODS load_worksheet_hyperlinks
IMPORTING
!io_ixml_worksheet TYPE REF TO if_ixml_document
!io_worksheet TYPE REF TO zcl_excel_worksheet
!it_external_hyperlinks TYPE gtt_external_hyperlinks
RAISING
zcx_excel .
METHODS load_worksheet_ignored_errors
IMPORTING
!io_ixml_worksheet TYPE REF TO if_ixml_document
!io_worksheet TYPE REF TO zcl_excel_worksheet
RAISING
zcx_excel .
METHODS load_worksheet_pagebreaks
IMPORTING
!io_ixml_worksheet TYPE REF TO if_ixml_document
!io_worksheet TYPE REF TO zcl_excel_worksheet
RAISING
zcx_excel .
METHODS load_worksheet_autofilter
IMPORTING
io_ixml_worksheet TYPE REF TO if_ixml_document
io_worksheet TYPE REF TO zcl_excel_worksheet
RAISING
zcx_excel.
METHODS load_worksheet_pagemargins
IMPORTING
!io_ixml_worksheet TYPE REF TO if_ixml_document
!io_worksheet TYPE REF TO zcl_excel_worksheet
RAISING
zcx_excel .
"! <p class="shorttext synchronized" lang="en">Load worksheet tables</p>
METHODS load_worksheet_tables
IMPORTING
io_ixml_worksheet TYPE REF TO if_ixml_document
io_worksheet TYPE REF TO zcl_excel_worksheet
iv_dirname TYPE string
it_tables TYPE t_tables
RAISING
zcx_excel .
CLASS-METHODS resolve_path
IMPORTING
!ip_path TYPE string
RETURNING
VALUE(rp_result) TYPE string .
METHODS resolve_referenced_formulae .
METHODS unescape_string_value
IMPORTING
i_value TYPE string
RETURNING
VALUE(result) TYPE string.
METHODS get_dxf_style_guid
IMPORTING
!io_ixml_dxf TYPE REF TO if_ixml_element
!io_excel TYPE REF TO zcl_excel
RETURNING
VALUE(rv_style_guid) TYPE zexcel_cell_style .
METHODS load_theme
IMPORTING
iv_path TYPE string
!ip_excel TYPE REF TO zcl_excel
RAISING
zcx_excel.
METHODS provided_string_is_escaped
IMPORTING
!value TYPE string
RETURNING
VALUE(is_escaped) TYPE abap_bool.
CONSTANTS: BEGIN OF namespace,
x14ac TYPE string VALUE 'http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac',
vba_project TYPE string VALUE 'http://schemas.microsoft.com/office/2006/relationships/vbaProject', "#EC NEEDED for future incorporation of XLSM-reader
c TYPE string VALUE 'http://schemas.openxmlformats.org/drawingml/2006/chart',
a TYPE string VALUE 'http://schemas.openxmlformats.org/drawingml/2006/main',
xdr TYPE string VALUE 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing',
mc TYPE string VALUE 'http://schemas.openxmlformats.org/markup-compatibility/2006',
r TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
chart TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
drawing TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
hyperlink TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
image TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
office_document TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
printer_settings TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings',
shared_strings TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
styles TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
theme TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
worksheet TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
relationships TYPE string VALUE 'http://schemas.openxmlformats.org/package/2006/relationships',
core_properties TYPE string VALUE 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
main TYPE string VALUE 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
END OF namespace.
PRIVATE SECTION.
DATA zip TYPE REF TO lcl_zip_archive .
DATA: gid TYPE i.
METHODS create_zip_archive
IMPORTING
!i_xlsx_binary TYPE xstring
!i_use_alternate_zip TYPE seoclsname OPTIONAL
RETURNING
VALUE(e_zip) TYPE REF TO lcl_zip_archive
RAISING
zcx_excel .
METHODS read_from_applserver
IMPORTING
!i_filename TYPE csequence
RETURNING
VALUE(r_excel_data) TYPE xstring
RAISING
zcx_excel.
METHODS read_from_local_file
IMPORTING
!i_filename TYPE csequence
RETURNING
VALUE(r_excel_data) TYPE xstring
RAISING
zcx_excel .
ENDCLASS.
CLASS zcl_excel_reader_2007 IMPLEMENTATION.
METHOD create_zip_archive.
CASE i_use_alternate_zip.
WHEN space.
e_zip = lcl_abap_zip_archive=>create( i_xlsx_binary ).
WHEN OTHERS.
e_zip = lcl_alternate_zip_archive=>create( i_data = i_xlsx_binary
i_alternate_zip_class = i_use_alternate_zip ).
ENDCASE.
ENDMETHOD.
METHOD fill_row_outlines.
TYPES: BEGIN OF lts_row_data,
row TYPE i,
outline_level TYPE i,
END OF lts_row_data,
ltt_row_data TYPE SORTED TABLE OF lts_row_data WITH UNIQUE KEY row.
DATA: lt_row_data TYPE ltt_row_data,
ls_row_data LIKE LINE OF lt_row_data,
lt_collapse_rows TYPE HASHED TABLE OF i WITH UNIQUE KEY table_line,
lv_collapsed TYPE abap_bool,
lv_outline_level TYPE i,
lv_next_consecutive_row TYPE i,
lt_outline_rows TYPE zcl_excel_worksheet=>mty_ts_outlines_row,
ls_outline_row LIKE LINE OF lt_outline_rows,
lo_row TYPE REF TO zcl_excel_row,
lo_row_iterator TYPE REF TO zcl_excel_collection_iterator,
lv_row_offset TYPE i,
lv_row_collapse_flag TYPE i.
FIELD-SYMBOLS: <ls_row_data> LIKE LINE OF lt_row_data.
* First collect information about outlines ( outline leven and collapsed state )
lo_row_iterator = io_worksheet->get_rows_iterator( ).
WHILE lo_row_iterator->has_next( ) = abap_true.
lo_row ?= lo_row_iterator->get_next( ).
ls_row_data-row = lo_row->get_row_index( ).
ls_row_data-outline_level = lo_row->get_outline_level( ).
IF ls_row_data-outline_level IS NOT INITIAL.
INSERT ls_row_data INTO TABLE lt_row_data.
ENDIF.
lv_collapsed = lo_row->get_collapsed( ).
IF lv_collapsed = abap_true.
INSERT lo_row->get_row_index( ) INTO TABLE lt_collapse_rows.
ENDIF.
ENDWHILE.
* Now parse this information - we need consecutive rows - any gap will create a new outline
DO 7 TIMES. " max number of outlines allowed
lv_outline_level = sy-index.
CLEAR lv_next_consecutive_row.
CLEAR ls_outline_row.
LOOP AT lt_row_data ASSIGNING <ls_row_data> WHERE outline_level >= lv_outline_level.
IF lv_next_consecutive_row <> <ls_row_data>-row " A gap --> close all open outlines
AND lv_next_consecutive_row IS NOT INITIAL. " First time in loop.
INSERT ls_outline_row INTO TABLE lt_outline_rows.
CLEAR: ls_outline_row.
ENDIF.
IF ls_outline_row-row_from IS INITIAL.
ls_outline_row-row_from = <ls_row_data>-row.
ENDIF.
ls_outline_row-row_to = <ls_row_data>-row.
lv_next_consecutive_row = <ls_row_data>-row + 1.
ENDLOOP.
IF ls_outline_row-row_from IS NOT INITIAL.
INSERT ls_outline_row INTO TABLE lt_outline_rows.
ENDIF.
ENDDO.
* lt_outline_rows holds all outline information
* we now need to determine whether the outline is collapsed or not
LOOP AT lt_outline_rows INTO ls_outline_row.
IF io_worksheet->zif_excel_sheet_properties~summarybelow = zif_excel_sheet_properties=>c_below_off.
lv_row_collapse_flag = ls_outline_row-row_from - 1.
ELSE.
lv_row_collapse_flag = ls_outline_row-row_to + 1.
ENDIF.
READ TABLE lt_collapse_rows TRANSPORTING NO FIELDS WITH TABLE KEY table_line = lv_row_collapse_flag.
IF sy-subrc = 0.
ls_outline_row-collapsed = abap_true.
ENDIF.
io_worksheet->set_row_outline( iv_row_from = ls_outline_row-row_from
iv_row_to = ls_outline_row-row_to
iv_collapsed = ls_outline_row-collapsed ).
ENDLOOP.
* Finally purge outline information ( collapsed state, outline leve) from row_dimensions, since we want to keep these in the outline-table
lo_row_iterator = io_worksheet->get_rows_iterator( ).
WHILE lo_row_iterator->has_next( ) = abap_true.
lo_row ?= lo_row_iterator->get_next( ).
lo_row->set_outline_level( 0 ).
lo_row->set_collapsed( abap_false ).
ENDWHILE.
ENDMETHOD.
METHOD fill_struct_from_attributes.
*--------------------------------------------------------------------*
* issue #230 - Pimp my Code
* - Stefan Schmoecker, (done) 2012-11-07
* - ...
* changes: renaming variables to naming conventions
* aligning code
* adding comments to explain what we are trying to achieve
*--------------------------------------------------------------------*
DATA: lv_name TYPE string,
lo_attributes TYPE REF TO if_ixml_named_node_map,
lo_attribute TYPE REF TO if_ixml_attribute,
lo_iterator TYPE REF TO if_ixml_node_iterator.
FIELD-SYMBOLS: <component> TYPE any.
*--------------------------------------------------------------------*
* The values of named attributes of a tag are being read and moved into corresponding
* fields of given structure
* Behaves like move-corresonding tag to structure
* Example:
* <Relationship Target="docProps/app.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Id="rId3"/>
* Here the attributes are Target, Type and Id. Thus if the passed
* structure has fieldnames Id and Target these would be filled with
* "rId3" and "docProps/app.xml" respectively
*--------------------------------------------------------------------*
CLEAR cp_structure.
lo_attributes = ip_element->get_attributes( ).
lo_iterator = lo_attributes->create_iterator( ).
lo_attribute ?= lo_iterator->get_next( ).
WHILE lo_attribute IS BOUND.
lv_name = lo_attribute->get_name( ).
TRANSLATE lv_name TO UPPER CASE.
ASSIGN COMPONENT lv_name OF STRUCTURE cp_structure TO <component>.
IF sy-subrc = 0.
<component> = lo_attribute->get_value( ).
ENDIF.
lo_attribute ?= lo_iterator->get_next( ).
ENDWHILE.
ENDMETHOD.
METHOD get_dxf_style_guid.
DATA: lo_ixml_dxf_children TYPE REF TO if_ixml_node_list,
lo_ixml_iterator_dxf_children TYPE REF TO if_ixml_node_iterator,
lo_ixml_dxf_child TYPE REF TO if_ixml_element,
lv_dxf_child_type TYPE string,
lo_ixml_element TYPE REF TO if_ixml_element,
lo_ixml_element2 TYPE REF TO if_ixml_element,
lv_val TYPE string.
DATA: ls_cstyle TYPE zexcel_s_cstyle_complete,
ls_cstylex TYPE zexcel_s_cstylex_complete.
lo_ixml_dxf_children = io_ixml_dxf->get_children( ).
lo_ixml_iterator_dxf_children = lo_ixml_dxf_children->create_iterator( ).
lo_ixml_dxf_child ?= lo_ixml_iterator_dxf_children->get_next( ).
WHILE lo_ixml_dxf_child IS BOUND.
lv_dxf_child_type = lo_ixml_dxf_child->get_name( ).
CASE lv_dxf_child_type.
WHEN 'font'.
*--------------------------------------------------------------------*
* italic
*--------------------------------------------------------------------*
lo_ixml_element = lo_ixml_dxf_child->find_from_name_ns( name = 'i' uri = namespace-main ).
IF lo_ixml_element IS BOUND.
CLEAR lv_val.
lv_val = lo_ixml_element->get_attribute_ns( 'val' ).
IF lv_val <> '0'.
ls_cstyle-font-italic = 'X'.
ls_cstylex-font-italic = 'X'.
ENDIF.
ENDIF.
*--------------------------------------------------------------------*
* bold
*--------------------------------------------------------------------*
lo_ixml_element = lo_ixml_dxf_child->find_from_name_ns( name = 'b' uri = namespace-main ).
IF lo_ixml_element IS BOUND.
CLEAR lv_val.
lv_val = lo_ixml_element->get_attribute_ns( 'val' ).
IF lv_val <> '0'.
ls_cstyle-font-bold = 'X'.
ls_cstylex-font-bold = 'X'.
ENDIF.
ENDIF.
*--------------------------------------------------------------------*
* strikethrough
*--------------------------------------------------------------------*
lo_ixml_element = lo_ixml_dxf_child->find_from_name_ns( name = 'strike' uri = namespace-main ).
IF lo_ixml_element IS BOUND.
CLEAR lv_val.
lv_val = lo_ixml_element->get_attribute_ns( 'val' ).
IF lv_val <> '0'.
ls_cstyle-font-strikethrough = 'X'.
ls_cstylex-font-strikethrough = 'X'.
ENDIF.
ENDIF.
*--------------------------------------------------------------------*
* color
*--------------------------------------------------------------------*
lo_ixml_element = lo_ixml_dxf_child->find_from_name_ns( name = 'color' uri = namespace-main ).
IF lo_ixml_element IS BOUND.
CLEAR lv_val.
lv_val = lo_ixml_element->get_attribute_ns( 'rgb' ).
ls_cstyle-font-color-rgb = lv_val.
ls_cstylex-font-color-rgb = 'X'.
ENDIF.
WHEN 'fill'.
lo_ixml_element = lo_ixml_dxf_child->find_from_name_ns( name = 'patternFill' uri = namespace-main ).
IF lo_ixml_element IS BOUND.
lo_ixml_element2 = lo_ixml_dxf_child->find_from_name_ns( name = 'bgColor' uri = namespace-main ).
IF lo_ixml_element2 IS BOUND.
CLEAR lv_val.
lv_val = lo_ixml_element2->get_attribute_ns( 'rgb' ).
IF lv_val IS NOT INITIAL.
ls_cstyle-fill-filltype = zcl_excel_style_fill=>c_fill_solid.
ls_cstyle-fill-bgcolor-rgb = lv_val.
ls_cstylex-fill-filltype = 'X'.
ls_cstylex-fill-bgcolor-rgb = 'X'.
ENDIF.
CLEAR lv_val.
lv_val = lo_ixml_element2->get_attribute_ns( 'theme' ).
IF lv_val IS NOT INITIAL.
ls_cstyle-fill-filltype = zcl_excel_style_fill=>c_fill_solid.
ls_cstyle-fill-bgcolor-theme = lv_val.
ls_cstylex-fill-filltype = 'X'.
ls_cstylex-fill-bgcolor-theme = 'X'.
ENDIF.
CLEAR lv_val.
ENDIF.
ENDIF.
ENDCASE.
lo_ixml_dxf_child ?= lo_ixml_iterator_dxf_children->get_next( ).
ENDWHILE.
rv_style_guid = io_excel->get_static_cellstyle_guid( ip_cstyle_complete = ls_cstyle
ip_cstylex_complete = ls_cstylex ).
ENDMETHOD.
METHOD get_from_zip_archive.
ASSERT zip IS BOUND. " zip object has to exist at this point
r_content = zip->read( i_filename ).
ENDMETHOD.
METHOD get_ixml_from_zip_archive.
DATA: lv_content TYPE xstring,
lo_ixml TYPE REF TO if_ixml,
lo_streamfactory TYPE REF TO if_ixml_stream_factory,
lo_istream TYPE REF TO if_ixml_istream,
lo_parser TYPE REF TO if_ixml_parser.
*--------------------------------------------------------------------*
* Load XML file from archive into an input stream,
* and parse that stream into an ixml object
*--------------------------------------------------------------------*
lv_content = me->get_from_zip_archive( i_filename ).
lo_ixml = cl_ixml=>create( ).
lo_streamfactory = lo_ixml->create_stream_factory( ).
lo_istream = lo_streamfactory->create_istream_xstring( lv_content ).
r_ixml = lo_ixml->create_document( ).
lo_parser = lo_ixml->create_parser( stream_factory = lo_streamfactory
istream = lo_istream
document = r_ixml ).
lo_parser->set_normalizing( is_normalizing ).
lo_parser->set_validating( mode = if_ixml_parser=>co_no_validation ).
lo_parser->parse( ).
ENDMETHOD.
METHOD load_drawing_anchor.
TYPES: BEGIN OF t_c_nv_pr,
name TYPE string,
id TYPE string,
END OF t_c_nv_pr.
TYPES: BEGIN OF t_blip,
cstate TYPE string,
embed TYPE string,
END OF t_blip.
TYPES: BEGIN OF t_chart,
id TYPE string,
END OF t_chart.
TYPES: BEGIN OF t_ext,
cx TYPE string,
cy TYPE string,
END OF t_ext.
CONSTANTS: lc_xml_attr_true TYPE string VALUE 'true',
lc_xml_attr_true_int TYPE string VALUE '1'.
CONSTANTS: lc_rel_chart TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
lc_rel_image TYPE string VALUE 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image'.
DATA: lo_drawing TYPE REF TO zcl_excel_drawing,
node TYPE REF TO if_ixml_element,
node2 TYPE REF TO if_ixml_element,
node3 TYPE REF TO if_ixml_element,
node4 TYPE REF TO if_ixml_element,
ls_upper TYPE zexcel_drawing_location,
ls_lower TYPE zexcel_drawing_location,
ls_size TYPE zexcel_drawing_size,
ext TYPE t_ext,
lv_content TYPE xstring,
lv_relation_id TYPE string,
lv_title TYPE string,
cnvpr TYPE t_c_nv_pr,
blip TYPE t_blip,
chart TYPE t_chart,
drawing_type TYPE zexcel_drawing_type,
rel_drawing TYPE t_rel_drawing.
node ?= io_anchor_element->find_from_name_ns( name = 'from' uri = namespace-xdr ).
CHECK node IS NOT INITIAL.
node2 ?= node->find_from_name_ns( name = 'col' uri = namespace-xdr ).
ls_upper-col = node2->get_value( ).
node2 ?= node->find_from_name_ns( name = 'row' uri = namespace-xdr ).
ls_upper-row = node2->get_value( ).
node2 ?= node->find_from_name_ns( name = 'colOff' uri = namespace-xdr ).
ls_upper-col_offset = node2->get_value( ).
node2 ?= node->find_from_name_ns( name = 'rowOff' uri = namespace-xdr ).
ls_upper-row_offset = node2->get_value( ).
node ?= io_anchor_element->find_from_name_ns( name = 'ext' uri = namespace-xdr ).
IF node IS INITIAL.
CLEAR ls_size.
ELSE.
me->fill_struct_from_attributes( EXPORTING ip_element = node CHANGING cp_structure = ext ).
ls_size-width = ext-cx.
ls_size-height = ext-cy.
TRY.
ls_size-width = zcl_excel_drawing=>emu2pixel( ls_size-width ).
CATCH cx_root.
ENDTRY.
TRY.
ls_size-height = zcl_excel_drawing=>emu2pixel( ls_size-height ).
CATCH cx_root.
ENDTRY.
ENDIF.
node ?= io_anchor_element->find_from_name_ns( name = 'to' uri = namespace-xdr ).
IF node IS INITIAL.
CLEAR ls_lower.
ELSE.
node2 ?= node->find_from_name_ns( name = 'col' uri = namespace-xdr ).
ls_lower-col = node2->get_value( ).
node2 ?= node->find_from_name_ns( name = 'row' uri = namespace-xdr ).
ls_lower-row = node2->get_value( ).
node2 ?= node->find_from_name_ns( name = 'colOff' uri = namespace-xdr ).
ls_lower-col_offset = node2->get_value( ).
node2 ?= node->find_from_name_ns( name = 'rowOff' uri = namespace-xdr ).
ls_lower-row_offset = node2->get_value( ).
ENDIF.
node ?= io_anchor_element->find_from_name_ns( name = 'pic' uri = namespace-xdr ).
IF node IS NOT INITIAL.
node2 ?= node->find_from_name_ns( name = 'nvPicPr' uri = namespace-xdr ).
CHECK node2 IS NOT INITIAL.
node3 ?= node2->find_from_name_ns( name = 'cNvPr' uri = namespace-xdr ).
CHECK node3 IS NOT INITIAL.
me->fill_struct_from_attributes( EXPORTING ip_element = node3 CHANGING cp_structure = cnvpr ).
lv_title = cnvpr-name.
node2 ?= node->find_from_name_ns( name = 'blipFill' uri = namespace-xdr ).
CHECK node2 IS NOT INITIAL.
node3 ?= node2->find_from_name_ns( name = 'blip' uri = namespace-a ).
CHECK node3 IS NOT INITIAL.
me->fill_struct_from_attributes( EXPORTING ip_element = node3 CHANGING cp_structure = blip ).
lv_relation_id = blip-embed.
drawing_type = zcl_excel_drawing=>type_image.
ENDIF.
node ?= io_anchor_element->find_from_name_ns( name = 'graphicFrame' uri = namespace-xdr ).
IF node IS NOT INITIAL.
node2 ?= node->find_from_name_ns( name = 'nvGraphicFramePr' uri = namespace-xdr ).
CHECK node2 IS NOT INITIAL.
node3 ?= node2->find_from_name_ns( name = 'cNvPr' uri = namespace-xdr ).
CHECK node3 IS NOT INITIAL.
me->fill_struct_from_attributes( EXPORTING ip_element = node3 CHANGING cp_structure = cnvpr ).
lv_title = cnvpr-name.
node2 ?= node->find_from_name_ns( name = 'graphic' uri = namespace-a ).
CHECK node2 IS NOT INITIAL.
node3 ?= node2->find_from_name_ns( name = 'graphicData' uri = namespace-a ).
CHECK node3 IS NOT INITIAL.
node4 ?= node2->find_from_name_ns( name = 'chart' uri = namespace-c ).
CHECK node4 IS NOT INITIAL.
me->fill_struct_from_attributes( EXPORTING ip_element = node4 CHANGING cp_structure = chart ).
lv_relation_id = chart-id.
drawing_type = zcl_excel_drawing=>type_chart.
ENDIF.
lo_drawing = io_worksheet->excel->add_new_drawing(
ip_type = drawing_type
ip_title = lv_title ).
io_worksheet->add_drawing( lo_drawing ).
lo_drawing->set_position2(
EXPORTING
ip_from = ls_upper
ip_to = ls_lower ).
READ TABLE it_related_drawings INTO rel_drawing
WITH KEY id = lv_relation_id.
lo_drawing->set_media(
EXPORTING
ip_media = rel_drawing-content
ip_media_type = rel_drawing-file_ext
ip_width = ls_size-width
ip_height = ls_size-height ).
IF drawing_type = zcl_excel_drawing=>type_chart.
* Begin fix for Issue #551
DATA: lo_tmp_node_2 TYPE REF TO if_ixml_element.
lo_tmp_node_2 ?= rel_drawing-content_xml->find_from_name_ns( name = 'pieChart' uri = namespace-c ).
IF lo_tmp_node_2 IS NOT INITIAL.
lo_drawing->graph_type = zcl_excel_drawing=>c_graph_pie.
ELSE.
lo_tmp_node_2 ?= rel_drawing-content_xml->find_from_name_ns( name = 'barChart' uri = namespace-c ).
IF lo_tmp_node_2 IS NOT INITIAL.
lo_drawing->graph_type = zcl_excel_drawing=>c_graph_bars.
ELSE.
lo_tmp_node_2 ?= rel_drawing-content_xml->find_from_name_ns( name = 'lineChart' uri = namespace-c ).
IF lo_tmp_node_2 IS NOT INITIAL.
lo_drawing->graph_type = zcl_excel_drawing=>c_graph_line.
ENDIF.
ENDIF.
ENDIF.
* End fix for issue #551
"-------------Added by Alessandro Iannacci - Should load chart attributes
lo_drawing->load_chart_attributes( rel_drawing-content_xml ).
ENDIF.
ENDMETHOD.
METHOD load_dxf_styles.
DATA: lo_styles_xml TYPE REF TO if_ixml_document,
lo_node_dxfs TYPE REF TO if_ixml_element,
lo_nodes_dxf TYPE REF TO if_ixml_node_collection,
lo_iterator_dxf TYPE REF TO if_ixml_node_iterator,
lo_node_dxf TYPE REF TO if_ixml_element,
lv_dxf_count TYPE i.
FIELD-SYMBOLS: <ls_dxf_style> LIKE LINE OF mt_dxf_styles.
*--------------------------------------------------------------------*
* Look for dxfs-node
*--------------------------------------------------------------------*
lo_styles_xml = me->get_ixml_from_zip_archive( iv_path ).
lo_node_dxfs = lo_styles_xml->find_from_name_ns( name = 'dxfs' uri = namespace-main ).
CHECK lo_node_dxfs IS BOUND.
*--------------------------------------------------------------------*
* loop through all dxf-nodes and create style for each
*--------------------------------------------------------------------*
lo_nodes_dxf ?= lo_node_dxfs->get_elements_by_tag_name_ns( name = 'dxf' uri = namespace-main ).
lo_iterator_dxf = lo_nodes_dxf->create_iterator( ).
lo_node_dxf ?= lo_iterator_dxf->get_next( ).
WHILE lo_node_dxf IS BOUND.
APPEND INITIAL LINE TO mt_dxf_styles ASSIGNING <ls_dxf_style>.
<ls_dxf_style>-dxf = lv_dxf_count. " We start counting at 0
ADD 1 TO lv_dxf_count. " prepare next entry
<ls_dxf_style>-guid = get_dxf_style_guid( io_ixml_dxf = lo_node_dxf
io_excel = io_excel ).
lo_node_dxf ?= lo_iterator_dxf->get_next( ).
ENDWHILE.
ENDMETHOD.
METHOD load_shared_strings.
*--------------------------------------------------------------------*
* ToDos:
* 2do§1 Support partial formatting of strings in cells
*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
* issue #230 - Pimp my Code
* - Stefan Schmoecker, (done) 2012-11-11
* - ...
* changes: renaming variables to naming conventions
* renaming variables to indicate what they are used for
* aligning code
* adding comments to explain what we are trying to achieve
* rewriting code for better readibility
*--------------------------------------------------------------------*
DATA:
lo_shared_strings_xml TYPE REF TO if_ixml_document,
lo_node_si TYPE REF TO if_ixml_element,
lo_node_si_child TYPE REF TO if_ixml_element,
lo_node_r_child_t TYPE REF TO if_ixml_element,
lo_node_r_child_rpr TYPE REF TO if_ixml_element,
lo_font TYPE REF TO zcl_excel_style_font,
ls_rtf TYPE zexcel_s_rtf,
lv_current_offset TYPE int2,
lv_tag_name TYPE string,
lv_node_value TYPE string.
FIELD-SYMBOLS: <ls_shared_string> LIKE LINE OF me->shared_strings.
*--------------------------------------------------------------------*
* §1 Parse shared strings file and get into internal table
* So far I have encountered 2 ways how a string can be represented in the shared strings file
* §1.1 - "simple" strings
* §1.2 - rich text formatted strings
* Following is an example how this file could be set up; 2 strings in simple formatting, 3rd string rich textformatted
* <?xml version="1.0" encoding="UTF-8" standalone="true"?>
* <sst uniqueCount="6" count="6" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
* <si>
* <t>This is a teststring 1</t>
* </si>
* <si>
* <t>This is a teststring 2</t>
* </si>
* <si>
* <r>
* <t>T</t>
* </r>
* <r>
* <rPr>
* <sz val="11"/>
* <color rgb="FFFF0000"/>
* <rFont val="Calibri"/>
* <family val="2"/>
* <scheme val="minor"/>
* </rPr>
* <t xml:space="preserve">his is a </t>
* </r>
* <r>
* <rPr>
* <sz val="11"/>
* <color theme="1"/>
* <rFont val="Calibri"/>
* <family val="2"/>
* <scheme val="minor"/>
* </rPr>
* <t>teststring 3</t>
* </r>
* </si>
* </sst>
*--------------------------------------------------------------------*
lo_shared_strings_xml = me->get_ixml_from_zip_archive( i_filename = ip_path
is_normalizing = space ). " NO!!! normalizing - otherwise leading blanks will be omitted and that is not really desired for the stringtable
lo_node_si ?= lo_shared_strings_xml->find_from_name_ns( name = 'si' uri = namespace-main ).
WHILE lo_node_si IS BOUND.
APPEND INITIAL LINE TO me->shared_strings ASSIGNING <ls_shared_string>. " Each <si>-entry in the xml-file must lead to an entry in our stringtable
lo_node_si_child ?= lo_node_si->get_first_child( ).
IF lo_node_si_child IS BOUND.
lv_tag_name = lo_node_si_child->get_name( ).
IF lv_tag_name = 't'.
*--------------------------------------------------------------------*
* §1.1 - "simple" strings
* Example: see above
*--------------------------------------------------------------------*
<ls_shared_string>-value = unescape_string_value( lo_node_si_child->get_value( ) ).
ELSE.
*--------------------------------------------------------------------*
* §1.2 - rich text formatted strings
* it is sufficient to strip the <t>...</t> tag from each <r>-tag and concatenate these
* as long as rich text formatting is not supported (2do§1) ignore all info about formatting
* Example: see above
*--------------------------------------------------------------------*
CLEAR: lv_current_offset.