Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Extract ALV list to a file in the background

Former Member
0 Kudos
1,452

I am currently working on an ALV report which the user requires that a file be generated automatically based on the ALV list output. Any ideas? Thanks in advance.

4 REPLIES 4

former_member223537
Active Contributor
0 Kudos
244

Hi,

On the ALV Tool Bar provide a TRANSFER button. When User clicks on this button, you want to transfer the data from ALV ( This would be available in an internal table ) to be transferred to a file.

Once user clicks on TRANSFER button, use EXPORT FROM DATABASE command to export the internal table data to the background report program.

Then use JOB_OPEN, JOB_SUBMIT ( pass the report program name here ), JOB_CLOSE.

Create a report program and Use

IMPORT FROM DATABASE... to import the internal table data.

Open Dataset .

Transfer.

Close Dataset.

This would download the data from internal table to application server in background.

The following thread might be useful :

Best regards,

Prashant

Former Member
0 Kudos
244

Hello Emanuel,

U can try like. U should get the file name from the user.

PARAMETERS: P_FILE TYPE RLGRAP-FILENAME.

IF SY-BATCH = 'X'.

IF NOT P_FILE IS INITIAL.

PERFORM DOWNLOAD_DATA.

ENDIF.

ENDIF.

FORM DOWNLOAD_DATA.

DATA: OUTPUT TYPE STRING.

OPEN DATASET P_FILE FOR OUTPUT IN TEXT MODE.

IF SY-SUBRC = 0.

LOOP AT G_T_OUTTAB.

CLEAR L_F_NETWR.

WRITE: G_T_OUTTAB-NETWR TO L_F_NETWR,

G_T_OUTTAB-ERDAT_OR TO L_F_ERDAT_OR,

G_T_OUTTAB-ERDAT_IN TO L_F_ERDAT_IN,

G_T_OUTTAB-ERDAT_DE TO L_F_ERDAT_DE.

CONCATENATE G_T_OUTTAB-VBELN

G_T_OUTTAB-POSNR

G_T_OUTTAB-VKBUR

L_F_POSID

G_T_OUTTAB-BSTKD

L_F_NETWR

L_F_ERDAT_OR

L_F_ERDAT_IN

G_T_OUTTAB-VBELN_IN

L_F_ERDAT_DE

G_T_OUTTAB-VBELN_DE

INTO OUTPUT

SEPARATED BY SPACE.

TRANSFER OUTPUT TO P_FILE.

ENDLOOP.

ELSE.

MESSAGE E041(S9) WITH P_FILE.

ENDIF.

CLOSE DATASET P_FILE.

ENDFORM. " DOWNLOAD_DATA

This code will download the file to the application server when the report is executed in background.

If u want to download the file in foreground also. Remove the first If statment.

If useful reward.

Vasanth

Former Member
0 Kudos
244

Thanks for all the input. I think the requirement is a little bit more complicated.

The user requires that the file be specified on the selection screen and generate the report automatically.

This means that the variant and the filters associated with it needs to be considered as well. I'm having quite a difficult time trying to figure out how to extract the data to the file at the same time or even before the ALV report is being generated ( or, even not at all for background job scheduling).

0 Kudos
244

Hello Emmanuel,

I have had the same problem, and i found no solution on the forums... So i have implemented my own solution: I implemented a child class, in order to add a new method write_file. This coding creates a file on the application server, considering the field catalog from the choosen variant. It can be enhanced in order to get the sort and the filter criteria (but if the file is exported to excel, the sort and filter can be managed within excel). Such a program can be run in background, and it works perfectly!

Sample coding:

REPORT z_alv_download_file.

----


  • CLASS LCL_GUI_ALV_GRID INHERITING FRO

----


  • Local child-class in order to implement a new method for *

  • download in a file in background *

----


CLASS lcl_gui_alv_grid DEFINITION INHERITING FROM cl_gui_alv_grid.

PUBLIC SECTION.

METHODS: write_file IMPORTING i_structure_name TYPE dd02l-tabname

is_variant TYPE disvariant

i_default TYPE char01

i_save TYPE char01

i_file TYPE fileextern

CHANGING it_outtab TYPE STANDARD TABLE.

ENDCLASS.

----


  • CLASS lcl_gui_alv_grid IMPLEMENTATION

----


  • Implementation of the new method write_file *

----


CLASS lcl_gui_alv_grid IMPLEMENTATION.

METHOD write_file.

  • Local types

TYPES: BEGIN OF ty_fieldnames,

field(60),

field2 TYPE help_info-tabname,

field3 TYPE help_info-fieldname,

key(1),

END OF ty_fieldnames.

  • Local data

DATA: lt_catalog TYPE lvc_t_fcat,

ls_catalog TYPE lvc_s_fcat,

lt_datatab TYPE TABLE OF hrdatatab,

ls_datatab TYPE hrdatatab,

lt_fieldnames TYPE TABLE OF ty_fieldnames,

ls_fieldnames TYPE ty_fieldnames,

ls_record TYPE string,

l_field(60) TYPE c,

l_nbcol TYPE i,

l_colpos TYPE i,

l_colpos_c(2) TYPE c,

l_fieldname(21) TYPE c VALUE 'ls_datatab-langtext '.

FIELD-SYMBOLS <f> TYPE ANY.

  • Call the grid display

CALL METHOD me->set_table_for_first_display

EXPORTING i_structure_name = i_structure_name

is_variant = is_variant

i_default = i_default

i_save = i_save

CHANGING it_outtab = it_outtab.

  • Get the field catalog according to the display variant which is used

CALL METHOD me->get_frontend_fieldcatalog

IMPORTING et_fieldcatalog = lt_catalog.

  • Get an internal table with only the fields from the field catalog

CALL FUNCTION 'ALV_CONVERT_DATA'

EXPORTING

alv_fieldcat = lt_catalog

TABLES

alv_datatab = it_outtab

hr_datatab = lt_datatab

hr_fieldnametab = lt_fieldnames.

  • Keep only the displayed fields from the field catalog

DELETE lt_catalog WHERE no_out = 'X'.

SORT lt_catalog BY col_pos DESCENDING.

READ TABLE lt_catalog INTO ls_catalog INDEX 1.

IF sy-subrc = 0.

CLEAR ls_record.

l_nbcol = ls_catalog-col_pos.

  • Open the file

OPEN DATASET i_file FOR OUTPUT IN TEXT MODE.

  • Write the column names

LOOP AT lt_fieldnames INTO ls_fieldnames.

CONDENSE ls_fieldnames-field.

CONCATENATE ls_record ls_fieldnames-field

INTO ls_record SEPARATED BY ';'.

ENDLOOP.

SHIFT ls_record.

TRANSFER ls_record TO i_file.

  • Write the lines

LOOP AT lt_datatab INTO ls_datatab.

CLEAR: l_colpos,

ls_record.

  • Condense the fields in a single string, separated by ';' (csv)

DO l_nbcol TIMES.

ADD 1 TO l_colpos.

CLEAR l_colpos_c.

l_colpos_c = l_colpos.

CONDENSE l_colpos_c.

l_fieldname+19(2) = l_colpos_c.

ASSIGN (l_fieldname) TO <f>.

WRITE <f> TO l_field.

CONDENSE l_field.

CONCATENATE ls_record l_field INTO ls_record SEPARATED BY ';'.

ENDDO.

SHIFT ls_record.

TRANSFER ls_record TO i_file.

ENDLOOP.

  • Close the file

CLOSE DATASET i_file.

ENDIF.

ENDMETHOD.

ENDCLASS.

----


  • MAIN PROGRAM

----


DATA: wo_alv TYPE REF TO lcl_gui_alv_grid,

wt_sflight TYPE TABLE OF sflight,

ws_variant TYPE disvariant.

PARAMETERS: p_file TYPE fileextern OBLIGATORY

DEFAULT '/usr/sap/.../file.csv',

p_vari TYPE slis_vari

DEFAULT 'DEFAULT'.

START-OF-SELECTION.

ws_variant-report = sy-repid.

ws_variant-username = sy-uname.

ws_variant-variant = p_vari.

SELECT * FROM sflight INTO TABLE wt_sflight.

CREATE OBJECT wo_alv EXPORTING i_parent = cl_gui_container=>screen0.

CALL METHOD wo_alv->write_file

EXPORTING i_structure_name = 'SFLIGHT'

is_variant = ws_variant

i_default = 'X'

i_save = 'A'

i_file = p_file

CHANGING it_outtab = wt_sflight.