Tuesday, January 18, 2011
This generic program is used to download PA and PY infotype data to presentation server.
Steps in the program
Steps in the program
- Get file directory using method cl_gui_frontend_services=>directory_browse.
- Create Dynamic table
- Generate records header by fulling header description using DDIF_FIELDINFO_GET funtion module.
- Extract data from Database
- Download data using GUI_DOWNLOAD function module by passing table with TAB delimited columns.
REPORT ztest_notepad. *--------------------------------------------------------------------------------------------------* *& Declarations *--------------------------------------------------------------------------------------------------* " Structures TABLES: p0001. " Types TYPES: ty_fdesc TYPE dfies, BEGIN OF ty_header, data TYPE string, END OF ty_header, BEGIN OF ty_data, data TYPE string, END OF ty_data, BEGIN OF ty_files, records TYPE i, filename TYPE ibipparms-path, END OF ty_files. " Work areas DATA: wa_fdesc TYPE ty_fdesc, wa_header TYPE ty_header, wa_data TYPE ty_data, wa_files TYPE ty_files. " Internal tables DATA: it_fdesc TYPE STANDARD TABLE OF ty_fdesc, it_header TYPE STANDARD TABLE OF ty_header, it_data TYPE STANDARD TABLE OF ty_data, it_files TYPE STANDARD TABLE OF ty_files. " Variables DATA: g_table TYPE rsrd1-tbma_val. " References DATA: it_object TYPE REF TO data, wa_object TYPE REF TO data. " Field-symbols FIELD-SYMBOLS:TYPE STANDARD TABLE, TYPE ANY. " Constants CONSTANTS: c_hor_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab. *--------------------------------------------------------------------------------------------------* " ALV Declarations *--------------------------------------------------------------------------------------------------* " Types Pools TYPE-POOLS: slis. " Types TYPES: ty_fieldcat TYPE slis_fieldcat_alv, ty_events TYPE slis_alv_event, ty_layout TYPE slis_layout_alv. " Workareas DATA: wa_fieldcat TYPE ty_fieldcat, wa_events TYPE ty_events, wa_layout TYPE ty_layout. " Internal Tables DATA: it_fieldcat TYPE STANDARD TABLE OF ty_fieldcat, it_events TYPE STANDARD TABLE OF ty_events. *--------------------------------------------------------------------------------------------------* *& Selection-screen PBO *--------------------------------------------------------------------------------------------------* SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-t01. PARAMETERS: p_infty(4) TYPE c OBLIGATORY. SELECT-OPTIONS: s_pernr FOR p0001-pernr, s_subty FOR p0001-subty, s_objps FOR p0001-objps, s_sprps FOR p0001-sprps. PARAMETERS: p_begda TYPE p0001-begda OBLIGATORY, p_endda TYPE p0001-endda OBLIGATORY. SELECT-OPTIONS: s_seqnr FOR p0001-seqnr, s_aedtm FOR p0001-aedtm. SELECTION-SCREEN END OF BLOCK b1. SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-t02. PARAMETERS: p_fsize TYPE i OBLIGATORY, p_fname TYPE string OBLIGATORY DEFAULT 'C:\'. SELECTION-SCREEN END OF BLOCK b2. *--------------------------------------------------------------------------------------------------* *& Selection screen F4 *--------------------------------------------------------------------------------------------------* AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname. PERFORM f4_2_browse_directory. *--------------------------------------------------------------------------------------------------* *& Selection screen PAI *--------------------------------------------------------------------------------------------------* AT SELECTION-SCREEN. PERFORM validate_directory_entered. PERFORM validate_input. *--------------------------------------------------------------------------------------------------* *& start-of-selection *--------------------------------------------------------------------------------------------------* START-OF-SELECTION. " Generate dynamic table PERFORM gen_dyn_tab. " Get file header PERFORM get_file_header. " Get data PERFORM extract_data. " Download data table PERFORM download_data. *--------------------------------------------------------------------------------------------------* *& end-of-selection *--------------------------------------------------------------------------------------------------* END-OF-SELECTION. "Display download log PERFORM display_log. *--------------------------------------------------------------------------------------------------* *& Form f4_2_browse_directory *--------------------------------------------------------------------------------------------------* FORM f4_2_browse_directory . DATA: l_init_dir TYPE string VALUE 'C:\', l_s_dir TYPE string, l_title TYPE string. l_title = text-w01. CALL METHOD cl_gui_frontend_services=>directory_browse EXPORTING window_title = l_title initial_folder = l_init_dir CHANGING selected_folder = l_s_dir EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ELSE. p_fname = l_s_dir. ENDIF. ENDFORM. " f4_2_browse_directory *--------------------------------------------------------------------------------------------------* *& Form validate_directory_entered *--------------------------------------------------------------------------------------------------* FORM validate_directory_entered . DATA: l_result TYPE abap_bool, l_length TYPE i. CALL METHOD cl_gui_frontend_services=>directory_exist EXPORTING directory = p_fname RECEIVING result = l_result EXCEPTIONS cntl_error = 1 error_no_gui = 2 wrong_parameter = 3 not_supported_by_gui = 4 OTHERS = 5. IF l_result NE 'X'. MESSAGE 'Please specify right directory' TYPE 'E'. ENDIF. l_length = STRLEN( p_fname ). l_length = l_length - 1. IF p_fname+l_length(1) NE '\'. CONCATENATE p_fname '\' INTO p_fname. ENDIF. ENDFORM. " validate_directory_entered *--------------------------------------------------------------------------------------------------* *& Form gen_dyn_tab *--------------------------------------------------------------------------------------------------* FORM gen_dyn_tab. CONCATENATE 'PA' p_infty INTO g_table. CREATE DATA it_object TYPE TABLE OF (g_table). ASSIGN it_object->* TO . ENDFORM. " gen_dyn_tab *--------------------------------------------------------------------------------------------------* *& Form extract_data *--------------------------------------------------------------------------------------------------* FORM extract_data. DATA: typ TYPE c, l_last_field TYPE i, char_data TYPE c LENGTH 100. FIELD-SYMBOLS: TYPE ANY, TYPE char100, TYPE d. CREATE DATA wa_object LIKE LINE OF . ASSIGN wa_object->* TO . SELECT * FROM (g_table) INTO TABLE WHERE pernr IN s_pernr AND subty IN s_subty AND objps IN s_objps AND sprps IN s_sprps AND endda GE p_begda AND begda LE p_endda AND seqnr IN s_seqnr AND aedtm IN s_aedtm. IF sy-subrc NE 0. MESSAGE 'No data for the selection.' TYPE 'E'. ELSE. DESCRIBE TABLE it_fdesc LINES l_last_field. LOOP AT INTO . DO. ASSIGN COMPONENT sy-index OF STRUCTURE TO . IF sy-subrc EQ 0. ASSIGN char_data TO . DESCRIBE FIELD TYPE typ. CASE typ. WHEN 'D'. CONCATENATE +6(2) +4(2) +0(4) INTO . CONCATENATE '''' INTO . WHEN 'N'. CONCATENATE '''' INTO . WHEN OTHERS. MOVE TO . ENDCASE. CASE sy-index. WHEN 1. wa_data-data = . WHEN l_last_field. CONCATENATE wa_data-data c_hor_tab INTO wa_data-data. APPEND wa_data TO it_data. CLEAR wa_data. WHEN OTHERS. CONCATENATE wa_data-data INTO wa_data-data SEPARATED BY c_hor_tab. ENDCASE. CLEAR . ELSE. EXIT. ENDIF. ENDDO. ENDLOOP. ENDIF. ENDFORM. " extract_data *--------------------------------------------------------------------------------------------------* *& Form download_data *--------------------------------------------------------------------------------------------------* FORM download_data . DATA: l_filename TYPE string, l_append TYPE char01, l_tot_records TYPE i, l_no_of_files TYPE i, l_frac TYPE char10, l_from_record TYPE i, l_to_recrd TYPE i, l_count(2) TYPE n, l_records TYPE string. DATA: l_it_data TYPE STANDARD TABLE OF ty_data. DESCRIBE TABLE it_data LINES l_tot_records. IF l_tot_records < p_fsize. "Generate file name CONCATENATE p_fname 'IT' g_table '_' 'F01' '_' sy-datum sy-uzeit '.xls' INTO l_filename. " Download header l_append = space. PERFORM download TABLES it_header USING l_filename l_append. " Download data l_append = 'X'. PERFORM download TABLES it_data USING l_filename l_append. "Create log record wa_files-records = l_tot_records. wa_files-filename = l_filename. APPEND wa_files TO it_files. CLEAR wa_files. ELSE. l_no_of_files = l_tot_records DIV p_fsize . l_frac = l_tot_records MOD p_fsize . IF NOT l_frac IS INITIAL. l_no_of_files = l_no_of_files + 1. ENDIF. l_from_record = 1. l_to_recrd = p_fsize. DO l_no_of_files TIMES. l_count = sy-index. IF sy-index <> 1. l_from_record = l_from_record + 1. l_to_recrd = p_fsize * sy-index. ENDIF. APPEND LINES OF it_data FROM l_from_record TO l_to_recrd TO l_it_data. l_from_record = l_to_recrd. " Get File pathe CONCATENATE p_fname 'IT' g_table '_' 'F' l_count '_' sy-datum sy-uzeit '.xls' INTO l_filename. " Download header l_append = space. PERFORM download TABLES it_header USING l_filename l_append. " Download data l_append = 'X'. PERFORM download TABLES l_it_data USING l_filename l_append. "Create log record. DESCRIBE TABLE l_it_data LINES l_records. wa_files-records = l_records. wa_files-filename = l_filename. APPEND wa_files TO it_files. CLEAR wa_files. CLEAR: l_it_data[], l_records. ENDDO. ENDIF. ENDFORM. " download_data *--------------------------------------------------------------------------------------------------* *& Form get_file_header *--------------------------------------------------------------------------------------------------* FORM get_file_header . DATA: l_last_rec TYPE i, l_tabname TYPE ddobjname. l_tabname = g_table. CALL FUNCTION 'DDIF_FIELDINFO_GET' EXPORTING tabname = l_tabname langu = sy-langu TABLES dfies_tab = it_fdesc EXCEPTIONS not_found = 1 internal_error = 2 OTHERS = 3. IF it_fdesc[] IS NOT INITIAL. DESCRIBE TABLE it_fdesc LINES l_last_rec. LOOP AT it_fdesc INTO wa_fdesc. CASE sy-tabix. WHEN 1. wa_header-data = wa_fdesc-scrtext_m. WHEN l_last_rec. CONCATENATE wa_header-data c_hor_tab wa_fdesc-scrtext_m INTO wa_header-data. APPEND wa_header TO it_header. CLEAR wa_header. WHEN OTHERS. CONCATENATE wa_header-data wa_fdesc-scrtext_m INTO wa_header-data SEPARATED BY c_hor_tab. ENDCASE. ENDLOOP. ENDIF. ENDFORM. " get_file_header *--------------------------------------------------------------------------------------------------* *& Form download *--------------------------------------------------------------------------------------------------* * text *--------------------------------------------------------------------------------------------------* * -->p_i_datatab text * -->p_l_filename text * -->p_l_append text *--------------------------------------------------------------------------------------------------* FORM download TABLES p_i_datatab USING p_l_filename p_l_append. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING filename = p_l_filename filetype = 'ASC' append = p_l_append write_field_separator = 'X' TABLES data_tab = p_i_datatab EXCEPTIONS file_write_error = 1 no_batch = 2 gui_refuse_filetransfer = 3 invalid_type = 4 no_authority = 5 unknown_error = 6 header_not_allowed = 7 separator_not_allowed = 8 filesize_not_allowed = 9 header_too_long = 10 dp_error_create = 11 dp_error_send = 12 dp_error_write = 13 unknown_dp_error = 14 access_denied = 15 dp_out_of_memory = 16 disk_full = 17 dp_timeout = 18 file_not_found = 19 dataprovider_exception = 20 control_flush_error = 21 OTHERS = 22. 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. " download *--------------------------------------------------------------------------------------------------* *& Form display_log *--------------------------------------------------------------------------------------------------* FORM display_log . wa_fieldcat-fieldname = 'RECORDS'. wa_fieldcat-tabname = 'IT_FILES'. wa_fieldcat-seltext_l = 'Downloaded records'. APPEND wa_fieldcat TO it_fieldcat. CLEAR wa_fieldcat. wa_fieldcat-fieldname = 'FILENAME'. wa_fieldcat-tabname = 'IT_FILES'. wa_fieldcat-seltext_l = 'File Name & Path'. wa_fieldcat-outputlen = '130'. APPEND wa_fieldcat TO it_fieldcat. CLEAR wa_fieldcat. CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY' EXPORTING i_callback_program = sy-repid it_fieldcat = it_fieldcat TABLES t_outtab = it_files. 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. " display_log *--------------------------------------------------------------------------------------------------* *& Form validate_input *--------------------------------------------------------------------------------------------------* FORM validate_input . IF p_fsize > 50000. MESSAGE 'Please restrict file size 1 to 50,000' TYPE 'E'. ENDIF. ENDFORM. " validate_input
Followers
Popular Posts
- ABAP - ALV Report example with steps
- ABAP - Step by step tutorial on Smart Forms - Template Node
- ABAP - Sending email with pdf attachment
- SAP Adobe Form - Steps to create simple ADOBE Form and calling it from ABAP Program
- SAP ABAP - CL_ABAP_CHAR_UTILITIES class usage
- ABAP - Multiple value selection from F4 help for SELECT-OPTIONS
- Execute ABAP Report using SUBMIT statement
- ABAP - Select all or Deselect all in ALV or Check box handling in ALV
- Web Dynpro ABAP ALV - ON_CLICK event
- ABAP - Dynamic WHERE clause
Hi,
ReplyDeleteFields sembols is deleted.