Skip to content

Commit 36eeb09

Browse files
authored
update
1 parent fedb0b7 commit 36eeb09

File tree

1 file changed

+166
-7
lines changed

1 file changed

+166
-7
lines changed

docs/development/xlsx.md

Lines changed: 166 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,176 @@ outline: [2, 6]
33
---
44
# XLSX
55

6-
<i class="fa-brands fa-github"></i> [XLSX Addon on GitHub](https://github.com/abap2UI5-addons/xlsx)
6+
The abap2UI5 framework, being purely ABAP, allows you to leverage the existing XLSX features of your ABAP system seamlessly. You can implement file uploads or downloads, converting the contents of XLSX files into internal ABAP tables or exporting tables to XLSX files. This add-on simplifies the process with APIs and examples.
77

8-
Since abap2UI5 is a pure ABAP framework, you can easily leverage the existing XLSX features of your ABAP system. <br>
98

10-
In ABAP Cloud, you can use the new APIs provided by the `XCO_CP_XLSX`classes. <br>
9+
#### Download
1110

12-
In ABAP Classic, there are several ways to handle XLSX files. However, to stay on the open-source path, consider using the excellent project [abap2xlsx](https://github.com/abap2xlsx) — you’ll find no easier solution! <br>
11+
Convert an internal table to an XLSX file and download it as a Base64-encoded file:
1312

14-
Then with abap2UI5, you only need to implement a file upload and transfer the contents of the XLSX file into an internal ABAP table or vice versa. This add-on provides APIs and examples to simplify your development. <br>
13+
::: code-group
1514

15+
```abap
16+
METHOD z2ui5_if_app~main.
1617
17-
#### XCO API (cloud)
18+
client->view_display( z2ui5_cl_xml_view=>factory(
19+
)->page(
20+
)->button(
21+
text = 'Open Download Popup'
22+
press = client->_event( 'BUTTON_DOWNLOAD' )
23+
)->stringify( ) ).
1824
19-
#### abap2xlsx (classic)
25+
26+
CASE client->get( )-event.
27+
28+
WHEN 'BUTTON_DOWNLOAD'.
29+
30+
TYPES:
31+
BEGIN OF ty_row,
32+
count TYPE i,
33+
value TYPE string,
34+
descr TYPE string,
35+
END OF ty_row.
36+
TYPES ty_tab TYPE STANDARD TABLE OF ty_row WITH EMPTY KEY.
37+
38+
DATA(lt_tab) = VALUE ty_tab(
39+
( count = '1' value = `red` descr = `this is a description` )
40+
( count = '2' value = `red` descr = `this is a description` )
41+
( count = '3' value = `red` descr = `this is a description` )
42+
( count = '4' value = `red` descr = `this is a description` )
43+
( count = '5' value = `red` descr = `this is a description` ) ).
44+
45+
DATA(lv_file) = lcl_help=>xlsx_get_by_itab( lt_tab ).
46+
47+
client->follow_up_action( val = client->_event_client(
48+
val = client->cs_event-download_b64_file
49+
t_arg = VALUE #( ( lv_file ) ( `test.xlsx` ) ) ) ).
50+
51+
ENDCASE.
52+
53+
ENDMETHOD.
54+
```
55+
```abap [LCL_HELP]
56+
class lcl_help DEFINITION.
57+
58+
PUBLIC SECTION.
59+
CLASS-METHODS: xlsx_get_by_itab
60+
IMPORTING
61+
VALUE(val) TYPE STANDARD TABLE
62+
RETURNING
63+
VALUE(result) TYPE string.
64+
65+
endclass.
66+
67+
CLASS lcl_help IMPLEMENTATION.
68+
69+
METHOD xlsx_get_by_itab.
70+
71+
DATA(lt_data) = REF #( val ).
72+
73+
FIELD-SYMBOLS: <tab> TYPE STANDARD TABLE.
74+
ASSIGN lt_data->* TO <tab>.
75+
TRY.
76+
cl_salv_table=>factory(
77+
EXPORTING
78+
list_display = abap_false
79+
IMPORTING
80+
r_salv_table = DATA(lo_salv)
81+
CHANGING
82+
t_table = <tab> ).
83+
84+
DATA(lt_fcat) = cl_salv_controller_metadata=>get_lvc_fieldcatalog(
85+
r_columns = lo_salv->get_columns( )
86+
r_aggregations = lo_salv->get_aggregations( ) ).
87+
CATCH cx_salv_msg.
88+
RETURN.
89+
ENDTRY.
90+
91+
cl_salv_bs_lex=>export_from_result_data_table(
92+
EXPORTING
93+
is_format = if_salv_bs_lex_format=>mc_format_xlsx
94+
ir_result_data_table = cl_salv_ex_util=>factory_result_data_table(
95+
r_data = lt_data
96+
t_fieldcatalog = lt_fcat
97+
)
98+
IMPORTING
99+
er_result_file = DATA(lv_xstring) ).
100+
101+
result = z2ui5_cl_util=>conv_encode_x_base64( lv_xstring ).
102+
result = `data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,` && result.
103+
104+
ENDMETHOD.
105+
106+
ENDCLASS.
107+
```
108+
::: code-group
109+
110+
#### Upload
111+
112+
Transform uploaded content into an internal table:
113+
114+
::: code-group
115+
116+
```abap
117+
METHOD z2ui5_if_app~main.
118+
119+
client->view_display( z2ui5_cl_xml_view=>factory(
120+
)->page(
121+
)->_z2ui5( )->file_uploader(
122+
value = client->_bind_edit( mv_value )
123+
path = client->_bind_edit( mv_path )
124+
placeholder = 'filepath here...'
125+
upload = client->_event( 'UPLOAD' )
126+
)->stringify( ) ).
127+
128+
CASE client->get( )-event.
129+
WHEN 'UPLOAD'.
130+
131+
data(lr_itab) = lcl_help=>itab_get_by_xlsx( mv_value ).
132+
"further process with itab...
133+
client->message_box_display( `xlsx uploaded` ).
134+
ENDCASE.
135+
136+
ENDMETHOD.
137+
```
138+
```abap [LCL_HELP]
139+
CLASS lcl_help DEFINITION.
140+
141+
PUBLIC SECTION.
142+
143+
CLASS-METHODS itab_get_by_xlsx
144+
IMPORTING
145+
VALUE(val) TYPE string
146+
RETURNING
147+
VALUE(result) TYPE REF TO data.
148+
149+
ENDCLASS.
150+
151+
CLASS lcl_help IMPLEMENTATION.
152+
153+
METHOD itab_get_by_xlsx.
154+
155+
SPLIT val AT `;` INTO DATA(lv_dummy) DATA(lv_data).
156+
SPLIT lv_data AT `,` INTO lv_dummy lv_data.
157+
158+
DATA(lv_xdata) = z2ui5_cl_util=>conv_decode_x_base64( lv_data ).
159+
DATA(lo_excel) = NEW cl_fdt_xl_spreadsheet(
160+
document_name = `test`
161+
xdocument = lv_xdata ) .
162+
163+
lo_excel->if_fdt_doc_spreadsheet~get_worksheet_names(
164+
IMPORTING worksheet_names = DATA(lt_worksheets) ).
165+
166+
result = lo_excel->if_fdt_doc_spreadsheet~get_itab_from_worksheet( lt_worksheets[ 1 ] ).
167+
168+
ENDMETHOD.
169+
170+
ENDCLASS.
171+
```
172+
::: code-group
173+
174+
For advanced functionality, consider leveraging the wonderful open-source project [abap2xlsx](https://github.com/abap2xlsx/abap2xlsx), which offers reusable APIs for all common XLSX operations.
175+
176+
::: tip **ABAP Cloud**
177+
The snippets provided above are not compatible with ABAP Cloud. Use the XCO_CP_XLSX APIs for such scenarios.
178+
:::

0 commit comments

Comments
 (0)