-
Notifications
You must be signed in to change notification settings - Fork 19
/
zdemo_excel31.prog.abap
160 lines (129 loc) · 6.24 KB
/
zdemo_excel31.prog.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
*&---------------------------------------------------------------------*
*& Report ZDEMO_EXCEL1
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zdemo_excel31.
DATA: lo_excel TYPE REF TO zcl_excel,
lo_worksheet TYPE REF TO zcl_excel_worksheet,
lo_hyperlink TYPE REF TO zcl_excel_hyperlink,
lo_column TYPE REF TO zcl_excel_column.
DATA: fieldval TYPE text80,
row TYPE i,
style_column_a TYPE REF TO zcl_excel_style,
style_column_a_guid TYPE zexcel_cell_style,
style_column_b TYPE REF TO zcl_excel_style,
style_column_b_guid TYPE zexcel_cell_style,
style_column_c TYPE REF TO zcl_excel_style,
style_column_c_guid TYPE zexcel_cell_style,
style_font TYPE REF TO zcl_excel_style_font.
CONSTANTS: gc_save_file_name TYPE string VALUE '31_AutosizeWithDifferentFontSizes.xlsx'.
INCLUDE zdemo_excel_outputopt_incl.
START-OF-SELECTION.
CREATE OBJECT lo_excel.
" Use active sheet
lo_worksheet = lo_excel->get_active_worksheet( ).
lo_worksheet->set_title( ip_title = 'Regular Font' ).
style_column_a = lo_excel->add_new_style( ).
style_column_a->font->size = 32. " quite large
style_column_a_guid = style_column_a->get_guid( ).
style_column_c = lo_excel->add_new_style( ).
style_column_c->font->size = 16. " not so large
style_column_c_guid = style_column_c->get_guid( ).
DO 20 TIMES.
row = sy-index.
CLEAR fieldval.
DO sy-index TIMES.
CONCATENATE fieldval 'X' INTO fieldval.
ENDDO.
lo_worksheet->set_cell( ip_column = 'A' ip_row = row ip_value = fieldval ip_style = style_column_a_guid ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = row ip_value = fieldval ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = row ip_value = fieldval ip_style = style_column_c_guid ).
ENDDO.
lo_column = lo_worksheet->get_column( 'A' ).
lo_column->set_auto_size( ip_auto_size = abap_true ).
lo_column = lo_worksheet->get_column( 'B' ).
lo_column->set_auto_size( ip_auto_size = abap_true ).
lo_column = lo_worksheet->get_column( 'C' ).
lo_column->set_auto_size( ip_auto_size = abap_true ).
lo_worksheet->calculate_column_widths( ).
" Add sheet
lo_worksheet = lo_excel->add_new_worksheet( ).
lo_worksheet->set_title( ip_title = 'Bold Font' ).
style_column_a = lo_excel->add_new_style( ).
style_column_a->font->size = 32. " quite large
style_column_a->font->bold = abap_true.
style_column_a_guid = style_column_a->get_guid( ).
style_column_b = lo_excel->add_new_style( ).
style_column_b->font->bold = abap_true.
style_column_b_guid = style_column_b->get_guid( ).
style_column_c = lo_excel->add_new_style( ).
style_column_c->font->size = 16. " not so large
style_column_c->font->bold = abap_true.
style_column_c_guid = style_column_c->get_guid( ).
DO 20 TIMES.
row = sy-index.
CLEAR fieldval.
DO sy-index TIMES.
CONCATENATE fieldval 'X' INTO fieldval.
ENDDO.
lo_worksheet->set_cell( ip_column = 'A' ip_row = row ip_value = fieldval ip_style = style_column_a_guid ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = row ip_value = fieldval ip_style = style_column_b_guid ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = row ip_value = fieldval ip_style = style_column_c_guid ).
ENDDO.
lo_column = lo_worksheet->get_column( 'A' ).
lo_column->set_auto_size( ip_auto_size = abap_true ).
lo_column = lo_worksheet->get_column( 'B' ).
lo_column->set_auto_size( ip_auto_size = abap_true ).
lo_column = lo_worksheet->get_column( 'C' ).
lo_column->set_auto_size( ip_auto_size = abap_true ).
lo_worksheet->calculate_column_widths( ).
" Add sheet
lo_worksheet = lo_excel->add_new_worksheet( ).
lo_worksheet->set_title( ip_title = 'Italic Font' ).
style_column_a = lo_excel->add_new_style( ).
style_column_a->font->size = 32. " quite large
style_column_a->font->italic = abap_true.
style_column_a_guid = style_column_a->get_guid( ).
style_column_b = lo_excel->add_new_style( ).
style_column_b->font->italic = abap_true.
style_column_b_guid = style_column_b->get_guid( ).
style_column_c = lo_excel->add_new_style( ).
style_column_c->font->size = 16. " not so large
style_column_c->font->italic = abap_true.
style_column_c_guid = style_column_c->get_guid( ).
DO 20 TIMES.
row = sy-index.
CLEAR fieldval.
DO sy-index TIMES.
CONCATENATE fieldval 'X' INTO fieldval.
ENDDO.
lo_worksheet->set_cell( ip_column = 'A' ip_row = row ip_value = fieldval ip_style = style_column_a_guid ).
lo_worksheet->set_cell( ip_column = 'B' ip_row = row ip_value = fieldval ip_style = style_column_b_guid ).
lo_worksheet->set_cell( ip_column = 'C' ip_row = row ip_value = fieldval ip_style = style_column_c_guid ).
ENDDO.
lo_column = lo_worksheet->get_column( 'A' ).
lo_column->set_auto_size( ip_auto_size = abap_true ).
lo_column = lo_worksheet->get_column( 'B' ).
lo_column->set_auto_size( ip_auto_size = abap_true ).
lo_column = lo_worksheet->get_column( 'C' ).
lo_column->set_auto_size( ip_auto_size = abap_true ).
lo_worksheet->calculate_column_widths( ).
" Add sheet for merged cells
lo_worksheet = lo_excel->add_new_worksheet( ).
lo_worksheet->set_title( ip_title = 'Merged cells' ).
lo_worksheet->set_cell( ip_column = 'A' ip_row = 1 ip_value = 'This is a very long header text' ).
lo_worksheet->set_cell( ip_column = 'A' ip_row = 2 ip_value = 'Some data' ).
lo_worksheet->set_cell( ip_column = 'A' ip_row = 3 ip_value = 'Some more data' ).
lo_worksheet->set_merge(
EXPORTING
ip_column_start = 'A'
ip_column_end = 'C'
ip_row = 1 ).
lo_column = lo_worksheet->get_column( 'A' ).
lo_column->set_auto_size( ip_auto_size = abap_true ).
lo_excel->set_active_sheet_index( i_active_worksheet = 1 ).
*** Create output
lcl_output=>output( lo_excel ).