SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Convert XLS to TXT



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Conversion
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sun Feb 17, 2008 2:53 pm    Post subject: Convert XLS to TXT Reply with quote

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

  CALL FUNCTION 'GUI_UPLOAD'
       EXPORTING
            filename                = lv_string
            filetype                = 'ASC'
            has_field_separator     = 'X'
            read_by_line            = 'X'
       TABLES
            data_tab                = i_file_data[]
       EXCEPTIONS
            file_open_error         = 1
            file_read_error         = 2
            no_batch                = 3
            gui_refuse_filetransfer = 4
            invalid_type            = 5
            no_authority            = 6
            unknown_error           = 7
            bad_data_format         = 8
            header_not_allowed      = 9
            separator_not_allowed   = 10
            header_too_long         = 11
            unknown_dp_error        = 12
            access_denied           = 13
            dp_out_of_memory        = 14
            disk_full               = 15
            dp_timeout              = 16
            OTHERS                  = 17.
  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_txt_file

*---------------------------------------------------------------------*
*       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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Conversion All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
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.