14.01.2020 Views

ABAP_to_the_Future

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Enhancing Custom Reports with ABAP2XLSX 11.2

an Excel object so that you can make some further changes and then download

the spreadsheet or email it out.

The good news—the wonderful news—i s that there is a class called ZCL_EXCEL_

CONVERTER that transforms a report object into an Excel object. Listing 11.5 first

creates an SALV object by passing in a table of monster data to the CL_SALV_TABLE

factory method. Once you have the SALV object, you create an instance of the CON-

VERTER class, pass in the ALV object (and the data ta ble again), and out pops an

Excel object.

DATA: lo_converter TYPE REF TO zcl_excel_converter,

lo_excel TYPE REF TO zcl_excel,

lo_exception TYPE REF TO zcx_excel,

lo_salv TYPE REF TO cl_salv_table.

cl_salv_table=>factory(

IMPORTING

r_salv_table = lo_salv

CHANGING

t_table = gt_monsters[] ).

* Add sort criteria and what have you....

CREATE OBJECT lo_converter.

TRY.

lo_converter->convert(

EXPORTING

io_alv = lo_salv

it_table = gt_monsters[]

i_row_int = 5

i_column_int = 6

i_table = abap_true

i_style_table = zcl_excel_table=>builtinstyle_medium2

CHANGING

co_excel = lo_excel ).

CATCH zcx_excel INTO lo_exception.

"Raise Fatal Exception

ENDTRY.

Listing 11.5 Transforming a Report Object into an Excel Object

In Listing 11.5, you can see an SALV object being created and then transformed

into an Excel object. Interestingly, you could have created and prepared a CL_GUI_

ALV_GRID object instead and passed that in; the IO_ALV parameter is typed as TYPE

REF TO OBJECT, so you can pass any object in, and within the method the dynamic

467

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!