Posted: Sun Feb 17, 2008 2:53 pm Post subject: Convert XLS to TXT
Instead of using GUI_UPLOAD you can use 'TEXT_CONVERT_XLS_TO_SAP'.
If you have to upload either text file or excel file you can use both of the above.
Code:
REPORT z_xls_to_txt.
TYPE-POOLS: TRUXS.
CONSTANTS: c_xls(3) VALUE 'XLS'.
DATA: lv_file(128), lv_ext(3).
DATA: BEGIN OF i_file_data OCCURS 0,
col1 TYPE text60,
col2 TYPE text60,
col3 TYPE text60,
col4 TYPE text60,
col5 TYPE text60,
col6 TYPE text60,
END OF i_file_data.
PARAMETER: p_local TYPE rlgrap-filename MODIF ID m2.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_local.
*F4 help to browse file in local server
PERFORM select_local_file.
START-OF-SELECTION.
*Get file extension
SPLIT p_local AT '.' INTO lv_file lv_ext.
TRANSLATE lv_ext TO UPPER CASE.
CLEAR i_file_data.
REFRESH i_file_data.
IF lv_ext = c_xls.
*IF file is excel get data from excel sheet
PERFORM get_xls_file.
ELSE.
*Else get file from gui download
PERFORM get_txt_file.
ENDIF. "lv_ext
*---------------------------------------------------------------------*
* FORM get_xls_file *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM get_xls_file.
DATA: i_raw TYPE TRUXS_T_TEXT_DATA.
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
i_field_seperator = 'X'
i_tab_raw_data = i_raw
i_filename = p_local
TABLES
i_tab_converted_data = i_file_data[]
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF. " sy-subrc
ENDFORM. "get_xls_file
*---------------------------------------------------------------------*
* FORM get_txt_file *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM get_txt_file.
DATA: lv_string TYPE string. " To get file path
CLEAR lv_string.
lv_string = p_local. " Get file path
*---------------------------------------------------------------------*
* FORM select_local_file *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM select_local_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = sy-cprog
dynpro_number = sy-dynnr
field_name = space
IMPORTING
file_name = p_local.
ENDFORM. "select_local_file
Now i_file_data contains the field values. You can move to your internal table which is declared according to the actual data types. For this, you need to loop thru i_file_data and move specific column values to the internal table.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.