Creating an ALV Report with CSV Download Functionality in SAP ABAP

Estimated read time 8 min read

In this blog, we’ll walk through the process of developing an ABAP report using the ALV (ABAP List Viewer) to display sales order information. We’ll also cover designing a custom GUI with a standard download functionality, allowing users to export data in CSV format at the click of a button. 

Introduction 

In SAP, ALV reports are a powerful way to display and manage tabular data. They offer various features like sorting, filtering, and summarizing data in an interactive grid format. In this tutorial, we’ll enhance an ALV report to include a “Download” button that allows users to export the displayed data into a CSV file. 

Setting Up Your ALV Report 

Step 1: Define Data Structures 

Firstly, define the internal table and the corresponding data structure that will hold the material data information. 

We have to declare or use the two types of groups like Slis and turxs Slis : for field catalog and layout. Turxs : for converting the data into different format like ASC : Flat ASCII CSV  : Comma Separated values TXT  : TXT Format (Notepad) HTML  : Hypertext Markup language 

Step 2: Populate Data 

Fetch the material data from your database or another source and populate the internal table 

Step 3 

We have to convert the data into .CSV format for that we should call function module “SAP_CONVERT_TO_TXT_FORMAT” and we have to pass material data internal table and pass the table type “TRUXS_T_TEXT_DATA”. 

Step 4 : Create ALV Grid Display 

We have to display the data by using “REUSE_ALV_GRID_DISPLAY” function module and we have to pass the material data internal table and field catalog internal table and also structure. 

Step 5 : Designing the Custom GUI 

Add a Download Button 

In the custom GUI, add a “Download” button. This can be done using SAP’s screen painter or in the custom container where your ALV grid is displayed. 

Define the Download Button Action 

Handle the download button click event to trigger the export functionality. 

Step 6 : Integrate Download Logic with ALV Event 

We have to call the function module called “CL_GUI_FORTEND_SERVICE=>GUI_DOWNLOAD” we need to pass “table type of the truxs” and important thing we need to assign our flat file path name within 

 

Refer the code below

 

REPORT zsh_rp_alv_in_csv_format.

TYPES : BEGIN OF ts_mara,
matnr TYPE matnr,
mtart TYPE mtart,
END OF ts_mara.

DATA : lt_mara TYPE TABLE OF ts_mara,
lt_text TYPE truxs_t_text_data,
filename1 TYPE string.

DATA :gt_fact TYPE slis_t_fieldcat_alv,
gs_fact TYPE slis_fieldcat_alv.

SELECT
matnr mtart
FROM mara INTO TABLE lt_mara UP TO 10 ROWS.

CALL FUNCTION ‘SAP_CONVERT_TO_TEX_FORMAT’
EXPORTING
i_field_seperator = ‘,’
TABLES
i_tab_sap_data = lt_mara
CHANGING
i_tab_converted_data = lt_text
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = ‘PF_STATUS’
i_callback_user_command = ‘USER_CMD’
i_structure_name = ‘MARA’
it_fieldcat = gt_fact
TABLES
t_outtab = lt_mara
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

FORM pf_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS ‘DOWN’.

ENDFORM.

FORM user_cmd USING r_ucomm LIKE sy-ucomm rs_selfiled TYPE slis_selfield.
filename1 = ‘Flie path.txt’.

CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = filename1 ” Name of file
CHANGING
data_tab = lt_text ” Transfer table
EXCEPTIONS
file_write_error = 1 ” Cannot write to file
no_batch = 2 ” Cannot execute front-end function in background
gui_refuse_filetransfer = 3 ” Incorrect Front End
invalid_type = 4 ” Invalid value for parameter FILETYPE
no_authority = 5 ” No Download Authorization
unknown_error = 6 ” Unknown error
header_not_allowed = 7 ” Invalid header
separator_not_allowed = 8 ” Invalid separator
filesize_not_allowed = 9 ” Invalid file size
header_too_long = 10 ” Header information currently restricted to 1023 bytes
dp_error_create = 11 ” Cannot create DataProvider
dp_error_send = 12 ” Error Sending Data with DataProvider
dp_error_write = 13 ” Error Writing Data with DataProvider
unknown_dp_error = 14 ” Error when calling data provider
access_denied = 15 ” Access to file denied.
dp_out_of_memory = 16 ” Not enough memory in data provider
disk_full = 17 ” Storage medium is full.
dp_timeout = 18 ” Data provider timeout
file_not_found = 19 ” Could not find file
dataprovider_exception = 20 ” General Exception Error in DataProvider
control_flush_error = 21 ” Error in Control Framework
not_supported_by_gui = 22 ” GUI does not support this
error_no_gui = 23 ” GUI not available
OTHERS = 24.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

ENDFORM.

 

 

 

 

 

​ In this blog, we’ll walk through the process of developing an ABAP report using the ALV (ABAP List Viewer) to display sales order information. We’ll also cover designing a custom GUI with a standard download functionality, allowing users to export data in CSV format at the click of a button. Introduction In SAP, ALV reports are a powerful way to display and manage tabular data. They offer various features like sorting, filtering, and summarizing data in an interactive grid format. In this tutorial, we’ll enhance an ALV report to include a “Download” button that allows users to export the displayed data into a CSV file. Setting Up Your ALV Report Step 1: Define Data Structures Firstly, define the internal table and the corresponding data structure that will hold the material data information. We have to declare or use the two types of groups like Slis and turxs Slis : for field catalog and layout. Turxs : for converting the data into different format like ASC : Flat ASCII CSV  : Comma Separated values TXT  : TXT Format (Notepad) HTML  : Hypertext Markup language Step 2: Populate Data Fetch the material data from your database or another source and populate the internal table Step 3 We have to convert the data into .CSV format for that we should call function module “SAP_CONVERT_TO_TXT_FORMAT” and we have to pass material data internal table and pass the table type “TRUXS_T_TEXT_DATA”. Step 4 : Create ALV Grid Display We have to display the data by using “REUSE_ALV_GRID_DISPLAY” function module and we have to pass the material data internal table and field catalog internal table and also structure. Step 5 : Designing the Custom GUI Add a Download Button In the custom GUI, add a “Download” button. This can be done using SAP’s screen painter or in the custom container where your ALV grid is displayed. Define the Download Button Action Handle the download button click event to trigger the export functionality. Step 6 : Integrate Download Logic with ALV Event We have to call the function module called “CL_GUI_FORTEND_SERVICE=>GUI_DOWNLOAD” we need to pass “table type of the truxs” and important thing we need to assign our flat file path name within  Refer the code below REPORT zsh_rp_alv_in_csv_format.

TYPES : BEGIN OF ts_mara,
matnr TYPE matnr,
mtart TYPE mtart,
END OF ts_mara.

DATA : lt_mara TYPE TABLE OF ts_mara,
lt_text TYPE truxs_t_text_data,
filename1 TYPE string.

DATA :gt_fact TYPE slis_t_fieldcat_alv,
gs_fact TYPE slis_fieldcat_alv.

SELECT
matnr mtart
FROM mara INTO TABLE lt_mara UP TO 10 ROWS.

CALL FUNCTION ‘SAP_CONVERT_TO_TEX_FORMAT’
EXPORTING
i_field_seperator = ‘,’
TABLES
i_tab_sap_data = lt_mara
CHANGING
i_tab_converted_data = lt_text
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = ‘PF_STATUS’
i_callback_user_command = ‘USER_CMD’
i_structure_name = ‘MARA’
it_fieldcat = gt_fact
TABLES
t_outtab = lt_mara
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

FORM pf_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS ‘DOWN’.

ENDFORM.

FORM user_cmd USING r_ucomm LIKE sy-ucomm rs_selfiled TYPE slis_selfield.
filename1 = ‘Flie path.txt’.

CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = filename1 ” Name of file
CHANGING
data_tab = lt_text ” Transfer table
EXCEPTIONS
file_write_error = 1 ” Cannot write to file
no_batch = 2 ” Cannot execute front-end function in background
gui_refuse_filetransfer = 3 ” Incorrect Front End
invalid_type = 4 ” Invalid value for parameter FILETYPE
no_authority = 5 ” No Download Authorization
unknown_error = 6 ” Unknown error
header_not_allowed = 7 ” Invalid header
separator_not_allowed = 8 ” Invalid separator
filesize_not_allowed = 9 ” Invalid file size
header_too_long = 10 ” Header information currently restricted to 1023 bytes
dp_error_create = 11 ” Cannot create DataProvider
dp_error_send = 12 ” Error Sending Data with DataProvider
dp_error_write = 13 ” Error Writing Data with DataProvider
unknown_dp_error = 14 ” Error when calling data provider
access_denied = 15 ” Access to file denied.
dp_out_of_memory = 16 ” Not enough memory in data provider
disk_full = 17 ” Storage medium is full.
dp_timeout = 18 ” Data provider timeout
file_not_found = 19 ” Could not find file
dataprovider_exception = 20 ” General Exception Error in DataProvider
control_flush_error = 21 ” Error in Control Framework
not_supported_by_gui = 22 ” GUI does not support this
error_no_gui = 23 ” GUI not available
OTHERS = 24.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

ENDFORM.       Read More Application Development Blog Posts articles 

#SAP

You May Also Like

More From Author