Hi,
Sample program to upload data from an excel sheet to an internal table and then again download it back to an excel sheet.
*Selection screen
PARAMETER:p_file TYPE localfile.
*Data declaration
DATA:i_intern TYPE STANDARD TABLE OF alsmex_tabline,
wa_intern TYPE alsmex_tabline,
v_col_count TYPE i VALUE '1'.
TYPES:BEGIN OF ty_final,
surname TYPE char35,
firstname TYPE char35,
salary TYPE int4,
month TYPE char10,
END OF ty_final.
DATA:i_final TYPE STANDARD TABLE OF ty_final,
wa_final TYPE ty_final.
*AT SELECTION-SCREEN------------------------------------------------------------------*
AT SELECTION-SCREEN ON p_file.
*Validate filename
PERFORM validate_filename USING p_file.
*F4 help to help user select file
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file .
PERFORM get_file CHANGING p_file.
START-OF-SELECTION.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_file
i_begin_col = '1'
i_begin_row = '1'
i_end_col = '4'
i_end_row = '4'
TABLES
intern = i_intern
* EXCEPTIONS
* INCONSISTENT_PARAMETERS = 1
* UPLOAD_OLE = 2
* OTHERS = 3
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
END-OF-SELECTION.
LOOP AT i_intern INTO wa_intern.
CASE v_col_count.
WHEN '1'.
wa_final-surname = wa_intern-value.
WHEN '2'.
wa_final-firstname = wa_intern-value.
WHEN '3'.
wa_final-salary = wa_intern-value.
WHEN '4'.
wa_final-month = wa_intern-value.
ENDCASE.
v_col_count = v_col_count + 1.
AT END OF row.
APPEND wa_final TO i_final.
CLEAR wa_final.
v_col_count = 1.
ENDAT.
ENDLOOP.
CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
EXPORTING
* I_FIELD_SEPERATOR =
* I_LINE_HEADER =
i_filename = C:\Documents and Settings\Administrator\Desktop\book2.xls'
* I_APPL_KEEP = ' '
TABLES
i_tab_sap_data = i_final
* CHANGING
* I_TAB_CONVERTED_DATA =
* EXCEPTIONS
* CONVERSION_FAILED = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
BREAK-POINT.
*&---------------------------------------------------------------------*
*& Form get_file
*&---------------------------------------------------------------------*
* Get File Name
*&---------------------------------------------------------------------*
FORM get_file CHANGING fp_file TYPE ibipparms-path.
*Call Function F4_FILENAME To Get The Filepath
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = sy-cprog
dynpro_number = sy-dynnr
IMPORTING
file_name = p_file.
ENDFORM. "get_file
*&---------------------------------------------------------------------*
*& Form validate_filename
*&---------------------------------------------------------------------*
* Validate filename
*----------------------------------------------------------------------*
* -->FP_FILE text
*----------------------------------------------------------------------*
FORM validate_filename USING fp_file TYPE ibipparms-path.
DATA :l_v_flen TYPE i, "File Length
l_v_fpath TYPE i, "Filepath Length without extension
l_v_ext TYPE char4, "File extension
l_v_ext_c TYPE char4, "For Lowercase Value
l_v_ext_s TYPE char4, "For Uppercase Value
l_v_file_exist TYPE char1, "File Exist or Not Indicator
l_v_file TYPE string. "File name
*Check Given File Exist or Not
l_v_file = fp_file.
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = l_v_file
RECEIVING
result = l_v_file_exist
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc <> 0.
IF sy-msgid IS NOT INITIAL.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
*Unexpected error while trying to read the file
MESSAGE 'Unexpected error while trying to read the file' type 'E').
ENDIF.
ENDIF.
IF l_v_file_exist IS INITIAL.
* Specified file does not exist
MESSAGE 'Specified file does not exist' type 'E'.
ENDIF.
*Assign extension of Excel file
l_v_ext_s = '.xls'.
l_v_ext_c = '.XLS'.
*Check given filename is an Excel File
l_v_flen = STRLEN( fp_file ).
l_v_fpath = l_v_flen - 4.
l_v_ext = fp_file+l_v_fpath(4).
IF l_v_ext NE l_v_ext_s AND l_v_ext NE l_v_ext_c.
*Enter a valid file type
MESSAGE 'Enter a valid file type' type 'E'.
ENDIF.
ENDFORM. "validate_filename
No comments:
Post a Comment