Skip to content

Commit 480e955

Browse files
authored
Update xlsx.md
1 parent cb015e1 commit 480e955

File tree

1 file changed

+124
-1
lines changed

1 file changed

+124
-1
lines changed

docs/development/xlsx.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,130 @@ The code snippets above are not compatible with ABAP Cloud, modify the lcl_help
179179
:::
180180

181181
#### abap2xlsx
182-
Instead of using the code in `lcl_help`, consider taking the easy route and leveraging the wonderful open-source project [abap2xlsx](https://github.com/abap2xlsx/abap2xlsx), which offers reusable APIs for all common XLSX operations. It works entirely within the ABAP stack and, therefore, seamlessly with abap2UI5.
182+
Instead of using the above XLSX API (which may change between releases), consider leveraging the excellent open-source project abap2xlsx. It provides reusable APIs for common XLSX operations and works entirely within the ABAP stack, ensuring seamless integration with abap2UI5. The following example demonstrates using abap2xlsx in the LCL_HELP class:
183+
::: code-group
184+
185+
```abap
186+
METHOD z2ui5_if_app~main.
187+
188+
client->view_display( z2ui5_cl_xml_view=>factory(
189+
)->page(
190+
)->button(
191+
text = 'Open Download Popup'
192+
press = client->_event( 'DOWNLOAD' )
193+
)->stringify( ) ).
194+
195+
IF client->get( )-event = `DOWNLOAD`.
196+
197+
TYPES:
198+
BEGIN OF ty_row,
199+
count TYPE i,
200+
value TYPE string,
201+
descr TYPE string,
202+
END OF ty_row.
203+
TYPES ty_tab TYPE STANDARD TABLE OF ty_row WITH EMPTY KEY.
204+
205+
DATA(lt_tab) = VALUE ty_tab(
206+
( count = '1' value = `red` descr = `this is a description` )
207+
( count = '2' value = `red` descr = `this is a description` )
208+
( count = '3' value = `red` descr = `this is a description` ) ).
209+
210+
DATA(lv_file) = lcl_help=>get_xlsx_by_itab( lt_tab ).
211+
client->follow_up_action( val = client->_event_client(
212+
val = client->cs_event-download_b64_file
213+
t_arg = VALUE #( ( lv_file ) ( `test.xlsx` ) ) ) ).
214+
ENDIF.
215+
216+
ENDMETHOD.
217+
```
218+
219+
```abap [LCL_HELP]
220+
CLASS lcl_help DEFINITION
221+
FINAL
222+
CREATE PUBLIC .
223+
224+
PUBLIC SECTION.
225+
226+
CLASS-METHODS get_itab_by_xlsx
227+
IMPORTING
228+
val TYPE string
229+
RETURNING
230+
VALUE(result) TYPE REF TO data.
231+
232+
CLASS-METHODS get_xlsx_by_itab
233+
IMPORTING
234+
val TYPE any
235+
RETURNING
236+
VALUE(result) TYPE string.
237+
238+
ENDCLASS.
239+
240+
241+
CLASS lcl_help IMPLEMENTATION.
242+
243+
METHOD get_xlsx_by_itab.
244+
TRY.
245+
246+
DATA: lo_excel TYPE REF TO zcl_excel,
247+
lo_writer TYPE REF TO zif_excel_writer,
248+
lo_worksheet TYPE REF TO zcl_excel_worksheet.
249+
250+
DATA: lt_field_catalog TYPE zexcel_t_fieldcatalog,
251+
ls_table_settings TYPE zexcel_s_table_settings.
252+
253+
254+
" Creates active sheet
255+
CREATE OBJECT lo_excel.
256+
257+
" Get active sheet
258+
lo_worksheet = lo_excel->get_active_worksheet( ).
259+
lo_worksheet->set_title( 'Internal table' ).
260+
261+
lt_field_catalog = zcl_excel_common=>get_fieldcatalog( ip_table = val ).
262+
ls_table_settings-table_style = zcl_excel_table=>builtinstyle_medium5.
263+
lo_worksheet->bind_table( ip_table = val
264+
is_table_settings = ls_table_settings
265+
it_field_catalog = lt_field_catalog ).
266+
267+
lo_worksheet->freeze_panes( ip_num_rows = 1 ).
268+
269+
CREATE OBJECT lo_writer TYPE zcl_excel_writer_2007.
270+
DATA(lv_file) = lo_writer->write_file( lo_excel ).
271+
272+
result = z2ui5_cl_util=>conv_encode_x_base64( lv_file ).
273+
result = `data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,` && result.
274+
275+
276+
CATCH cx_root INTO DATA(x).
277+
z2ui5_cL_util=>x_raise( x->get_text( ) ).
278+
ENDTRY.
279+
ENDMETHOD.
280+
281+
METHOD get_itab_by_xlsx.
282+
TRY.
283+
284+
SPLIT val AT `;` INTO DATA(lv_dummy) DATA(lv_data).
285+
SPLIT lv_data AT `,` INTO lv_dummy lv_data.
286+
DATA(lv_xdata) = z2ui5_cl_util=>conv_decode_x_base64( lv_data ).
287+
288+
DATA: lo_excel TYPE REF TO zcl_excel,
289+
lo_reader TYPE REF TO zif_excel_reader,
290+
lo_worksheet TYPE REF TO zcl_excel_worksheet.
291+
292+
CREATE OBJECT lo_reader TYPE zcl_excel_reader_2007.
293+
lo_excel = lo_reader->load( lv_xdata ).
294+
lo_worksheet = lo_excel->get_worksheet_by_index( 1 ).
295+
lo_worksheet->convert_to_table(
296+
IMPORTING
297+
er_data = result ).
298+
299+
CATCH cx_root INTO DATA(x).
300+
z2ui5_cL_util=>x_raise( x->get_text( ) ).
301+
ENDTRY.
302+
ENDMETHOD.
303+
ENDCLASS.
304+
```
305+
:::
183306

184307
#### UI5 Control
185308
If you want to export the data directly at the frontend, SAP offers the sap.ui.export.Spreadsheet control to export table content. With some additional logic, this control is also usable with abap2UI5. Check out the UI-Extension add-on for a running sample [here.](/addons/popup) However, the programming effort might be higher compared to the file-based approach shown above.

0 commit comments

Comments
 (0)