Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Uploading Custom Table data?

Former Member
0 Kudos

Once I have data in a spreadsheet. Is there an SAP transaction for uploading the data into a custom table? Else, I'll write a utility to do this. Anybody have a generic ABAP utility for this?

Thanks.

1 ACCEPTED SOLUTION

FredericGirod
Active Contributor
0 Kudos

I have,

give ten points for the code !

************************************************************************
* ABAP name : ZBTA0001                                                 *
*                                                                      *
* Created by : Frédéric GIROD / Sébastien COLLOMB                      *
*                                                                      *
* Date : 14/03/2005                                                    *
*                                                                      *
* Description : BCxxxx Chargement / Dechargement de tables             *
* transparentes SAP.                                                   *
*                                                                      *
*                                                                      *
************************************************************************
* MODIFICATIONS                                                        *
************************************************************************
* Date * Author * Marking code * Description                           *
************************************************************************
* * * *                                                                *
************************************************************************
REPORT ZBC_SAUVEGARDE_TABLE
       NO STANDARD PAGE HEADING.







*------------------------------- TABLES -------------------------------*
TABLES : dd02t.







*-------------------------------- DATA --------------------------------*
DATA : wt_fieldcat TYPE lvc_t_fcat ,
       ws_fieldcat TYPE lvc_s_fcat ,

BEGIN OF wt_tablist OCCURS 0 ,
  tabname TYPE tabname ,
  ddtext TYPE as4text ,
END OF wt_tablist .







FIELD-SYMBOLS : <wfv_structname> TYPE typename , " Nom de la table
                <wft_table>      TYPE table , " Table interne dyn
                <wfs_structure>  TYPE ANY . " Structure de la table









*-------------------------- SELECTION SCREEN --------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-t01.
PARAMETERS p_table TYPE tabname.
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN SKIP 1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-t02.
PARAMETERS : p_imp RADIOBUTTON GROUP ra1 ,
             p_exp RADIOBUTTON GROUP ra1
                   DEFAULT 'X' ,
             p_clear AS CHECKBOX .
SELECTION-SCREEN END OF BLOCK b2.

SELECTION-SCREEN SKIP 1.

SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text-t03.
PARAMETERS : p_file TYPE localfile
                    OBLIGATORY .
SELECTION-SCREEN SKIP 1.
PARAMETERS : p_excl RADIOBUTTON GROUP ra2 ,
             p_flat RADIOBUTTON GROUP ra2 .
SELECTION-SCREEN END OF BLOCK b3.













*-------------------------------- MAIN --------------------------------*
START-OF-SELECTION.

* Vérification des options de sélection.
  PERFORM p_check.

* Déchargement de la table.
  IF p_exp EQ 'X'.
    PERFORM p_dechargement.

* Chargement de la table.
  ELSE.
    PERFORM p_chargement.

  ENDIF.



END-OF-SELECTION.















*----------------------------------------------------------------------*
* Form P_CHECK.                                                        *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
FORM p_check.



  DATA : wlv_tabname TYPE tabname.


* Verifie que la table existe.
  SELECT tabname
         UP TO 1 ROWS
         INTO wlv_tabname
         FROM dd02t
         WHERE tabname EQ p_table
         AND as4local EQ 'A'.
  ENDSELECT.

  IF sy-subrc NE space.
    WRITE : /3 text-001.
    STOP.
  ENDIF.

* On ne traite que le chargement des tables Z*.
  IF p_imp EQ 'X' AND p_table+0(1) NE 'Z'.
    WRITE : /1 text-007.
    STOP.
  ENDIF.


ENDFORM. " P_CHECK.







*----------------------------------------------------------------------*
* Form P_DECHARGEMENT.                                                 *
*----------------------------------------------------------------------*
* Déchargement de la table.                                            *
*----------------------------------------------------------------------*
FORM p_dechargement.


* Création de la table interne dynamique du type de la table.
  PERFORM p_create_dyntable
          USING p_table.

* Lecture de la table ( et oui un joli select * ! )
  SELECT *
         INTO TABLE <wft_table>
         FROM (p_table).

* Si déchargen en format Excel.
  IF p_excl EQ 'X'.
    CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
      EXPORTING
        i_filename        = p_file
      TABLES
        i_tab_sap_data    = <wft_table>
      EXCEPTIONS
        conversion_failed = 1
        OTHERS            = 2.
    IF sy-subrc NE space.
      WRITE : /3 text-003.
    ENDIF.

* Si fichier plat.
  ELSEIF p_flat EQ 'X'.

    CALL FUNCTION 'WS_DOWNLOAD'
      EXPORTING
        filename = p_file
        filetype = 'DAT'
      TABLES
        data_tab = <wft_table>
      EXCEPTIONS
        OTHERS   = 10.
    IF sy-subrc NE space.
      WRITE : /3 text-003.
    ENDIF.

  ENDIF.

ENDFORM. " P_DECHARGEMENT.









*----------------------------------------------------------------------*
* Form P_CHARGEMENT.                                                   *
*----------------------------------------------------------------------*
* Chargement.                                                          *
*----------------------------------------------------------------------*
FORM p_chargement.


  DATA : wlv_count_col TYPE i ,
         wlv_flag_row TYPE kcd_ex_row_n ,
         wlt_file TYPE TABLE OF alsmex_tabline
                  WITH HEADER LINE ,
         wlv_char1 TYPE char1.

  FIELD-SYMBOLS : <wlfv_field> TYPE ANY.


* Création de la table interne dynamique du type de la table.
  PERFORM p_create_dyntable
          USING p_table.

* Lecture du fichier.
* Si déchargen en format Excel.
  IF p_excl EQ 'X'.

*   Seek number of column.
    DESCRIBE TABLE wt_fieldcat LINES wlv_count_col.

*   Function to read the Microsoft Excel file.
    CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename                = p_file
        i_begin_col             = '1'
        i_begin_row             = '1'
        i_end_col               = wlv_count_col
        i_end_row               = '10000'
      TABLES
        intern                  = wlt_file
      EXCEPTIONS
        inconsistent_parameters = 1
        upload_ole              = 2
        OTHERS                  = 3.
    IF sy-subrc NE space.
      WRITE : /3 text-003.
      STOP.
    ENDIF.

*   Set data.
    LOOP AT wlt_file.

      IF wlt_file-row NE wlv_flag_row.
        MOVE wlt_file-row TO wlv_flag_row.
        IF wlt_file-row NE 1.
          APPEND <wfs_structure> TO <wft_table>.
          CLEAR <wfs_structure>.
        ENDIF.
      ENDIF.

      READ TABLE wt_fieldcat
           INTO ws_fieldcat
           INDEX wlt_file-col.

      CHECK sy-subrc EQ space.
      ASSIGN COMPONENT ws_fieldcat-fieldname
             OF STRUCTURE <wfs_structure>
             TO <wlfv_field>.
      CATCH SYSTEM-EXCEPTIONS conversion_errors = 1.
        MOVE wlt_file-value TO <wlfv_field>.
      ENDCATCH.

      IF sy-subrc EQ 1.
        " A FAIRE
      ENDIF.

    ENDLOOP.

*   Append last time.
    APPEND <wfs_structure> TO <wft_table>.


* Si fichier plat.
  ELSEIF p_flat EQ 'X'.

    CALL FUNCTION 'WS_UPLOAD'
      EXPORTING
        filename = p_file
        filetype = 'DAT'
      TABLES
        data_tab = <wft_table>
      EXCEPTIONS
        OTHERS   = 10.

    IF sy-subrc NE space.
      WRITE : /3 text-003.
      STOP.
    ENDIF.

  ENDIF.





* Si vide la table avant.
  IF p_clear EQ 'X'.

*   Verification avant la suppression de la table.
    CALL FUNCTION 'POPUP_TO_CONFIRM'
      EXPORTING
        titlebar       = text-004
        text_question  = text-005
      IMPORTING
        answer         = wlv_char1
      EXCEPTIONS
        text_not_found = 1
        OTHERS         = 2.
    IF wlv_char1 EQ '2'.
      STOP.
    ENDIF.

*   On supprime tout.
    DELETE FROM (p_table) CLIENT SPECIFIED
           WHERE mandt EQ sy-mandt.

    IF sy-subrc NE space.
      WRITE : /3 text-003.
      STOP.
    ENDIF.

  ENDIF.



* Chargement de la base.
  MODIFY (p_table) FROM TABLE <wft_table>.
  IF sy-subrc NE space.
    WRITE : /3 text-003.
    STOP.
  ENDIF.



ENDFORM. " P_CHARGEMENT.









*----------------------------------------------------------------------*
* Form P_CREATE_DYNTABLE.                                              *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
FORM p_create_dyntable
     USING wpv_tabname TYPE tabname.


  DATA : wlt_table TYPE REF TO data ,
         wls_table TYPE REF TO data .


* On assigne le nom de la table.
  IF <wfv_structname> IS ASSIGNED.
    UNASSIGN <wfv_structname>.
  ENDIF.
  ASSIGN wpv_tabname TO <wfv_structname>.


* Recherche de la description du dictionnaire.
  REFRESH wt_fieldcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = <wfv_structname>
      i_bypassing_buffer     = 'X'
    CHANGING
      ct_fieldcat            = wt_fieldcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.

  IF sy-subrc NE space.
    WRITE : /1 text-003.
    STOP.
  ENDIF.



* Création de la table interne dynamique.
  IF <wft_table> IS ASSIGNED.
    UNASSIGN <wft_table>.
  ENDIF.

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = wt_fieldcat
    IMPORTING
      ep_table        = wlt_table.

  ASSIGN wlt_table->* TO <wft_table>.



* Création de la structure du type de la table.
  IF <wfs_structure> IS ASSIGNED.
    UNASSIGN <wfs_structure>.
  ENDIF.
  CREATE DATA wls_table LIKE LINE OF <wft_table>.
  ASSIGN wls_table->* TO <wfs_structure>.


ENDFORM. " P_CREATE_DYNTABLE.






*------------------------------- EVENTS -------------------------------*
* Au démarage de l'application.
INITIALIZATION.

* On desactive la zone vider la table, elle n'est active que pour
* le chargement.
  LOOP AT SCREEN.
    IF screen-name EQ 'P_CLEAR'.
      MOVE '0' TO screen-active.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.

* A l'événement rafraichissement de l'écran.
AT SELECTION-SCREEN OUTPUT.

  LOOP AT SCREEN.
* Si on charge alors on active la zone P_CLEAR.
    IF p_imp EQ 'X'.
      IF screen-name EQ 'P_CLEAR'.
        MOVE '1' TO screen-active.
        MODIFY SCREEN.
      ENDIF.
    ELSE.
      IF screen-name EQ 'P_CLEAR'.
        MOVE '0' TO screen-active.
        MODIFY SCREEN.
      ENDIF.
    ENDIF.
  ENDLOOP.

* Evenement F4 sur P_FILE.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    IMPORTING
      file_name = p_file.

4 REPLIES 4

FredericGirod
Active Contributor
0 Kudos

I have,

give ten points for the code !

************************************************************************
* ABAP name : ZBTA0001                                                 *
*                                                                      *
* Created by : Frédéric GIROD / Sébastien COLLOMB                      *
*                                                                      *
* Date : 14/03/2005                                                    *
*                                                                      *
* Description : BCxxxx Chargement / Dechargement de tables             *
* transparentes SAP.                                                   *
*                                                                      *
*                                                                      *
************************************************************************
* MODIFICATIONS                                                        *
************************************************************************
* Date * Author * Marking code * Description                           *
************************************************************************
* * * *                                                                *
************************************************************************
REPORT ZBC_SAUVEGARDE_TABLE
       NO STANDARD PAGE HEADING.







*------------------------------- TABLES -------------------------------*
TABLES : dd02t.







*-------------------------------- DATA --------------------------------*
DATA : wt_fieldcat TYPE lvc_t_fcat ,
       ws_fieldcat TYPE lvc_s_fcat ,

BEGIN OF wt_tablist OCCURS 0 ,
  tabname TYPE tabname ,
  ddtext TYPE as4text ,
END OF wt_tablist .







FIELD-SYMBOLS : <wfv_structname> TYPE typename , " Nom de la table
                <wft_table>      TYPE table , " Table interne dyn
                <wfs_structure>  TYPE ANY . " Structure de la table









*-------------------------- SELECTION SCREEN --------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-t01.
PARAMETERS p_table TYPE tabname.
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN SKIP 1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-t02.
PARAMETERS : p_imp RADIOBUTTON GROUP ra1 ,
             p_exp RADIOBUTTON GROUP ra1
                   DEFAULT 'X' ,
             p_clear AS CHECKBOX .
SELECTION-SCREEN END OF BLOCK b2.

SELECTION-SCREEN SKIP 1.

SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text-t03.
PARAMETERS : p_file TYPE localfile
                    OBLIGATORY .
SELECTION-SCREEN SKIP 1.
PARAMETERS : p_excl RADIOBUTTON GROUP ra2 ,
             p_flat RADIOBUTTON GROUP ra2 .
SELECTION-SCREEN END OF BLOCK b3.













*-------------------------------- MAIN --------------------------------*
START-OF-SELECTION.

* Vérification des options de sélection.
  PERFORM p_check.

* Déchargement de la table.
  IF p_exp EQ 'X'.
    PERFORM p_dechargement.

* Chargement de la table.
  ELSE.
    PERFORM p_chargement.

  ENDIF.



END-OF-SELECTION.















*----------------------------------------------------------------------*
* Form P_CHECK.                                                        *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
FORM p_check.



  DATA : wlv_tabname TYPE tabname.


* Verifie que la table existe.
  SELECT tabname
         UP TO 1 ROWS
         INTO wlv_tabname
         FROM dd02t
         WHERE tabname EQ p_table
         AND as4local EQ 'A'.
  ENDSELECT.

  IF sy-subrc NE space.
    WRITE : /3 text-001.
    STOP.
  ENDIF.

* On ne traite que le chargement des tables Z*.
  IF p_imp EQ 'X' AND p_table+0(1) NE 'Z'.
    WRITE : /1 text-007.
    STOP.
  ENDIF.


ENDFORM. " P_CHECK.







*----------------------------------------------------------------------*
* Form P_DECHARGEMENT.                                                 *
*----------------------------------------------------------------------*
* Déchargement de la table.                                            *
*----------------------------------------------------------------------*
FORM p_dechargement.


* Création de la table interne dynamique du type de la table.
  PERFORM p_create_dyntable
          USING p_table.

* Lecture de la table ( et oui un joli select * ! )
  SELECT *
         INTO TABLE <wft_table>
         FROM (p_table).

* Si déchargen en format Excel.
  IF p_excl EQ 'X'.
    CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
      EXPORTING
        i_filename        = p_file
      TABLES
        i_tab_sap_data    = <wft_table>
      EXCEPTIONS
        conversion_failed = 1
        OTHERS            = 2.
    IF sy-subrc NE space.
      WRITE : /3 text-003.
    ENDIF.

* Si fichier plat.
  ELSEIF p_flat EQ 'X'.

    CALL FUNCTION 'WS_DOWNLOAD'
      EXPORTING
        filename = p_file
        filetype = 'DAT'
      TABLES
        data_tab = <wft_table>
      EXCEPTIONS
        OTHERS   = 10.
    IF sy-subrc NE space.
      WRITE : /3 text-003.
    ENDIF.

  ENDIF.

ENDFORM. " P_DECHARGEMENT.









*----------------------------------------------------------------------*
* Form P_CHARGEMENT.                                                   *
*----------------------------------------------------------------------*
* Chargement.                                                          *
*----------------------------------------------------------------------*
FORM p_chargement.


  DATA : wlv_count_col TYPE i ,
         wlv_flag_row TYPE kcd_ex_row_n ,
         wlt_file TYPE TABLE OF alsmex_tabline
                  WITH HEADER LINE ,
         wlv_char1 TYPE char1.

  FIELD-SYMBOLS : <wlfv_field> TYPE ANY.


* Création de la table interne dynamique du type de la table.
  PERFORM p_create_dyntable
          USING p_table.

* Lecture du fichier.
* Si déchargen en format Excel.
  IF p_excl EQ 'X'.

*   Seek number of column.
    DESCRIBE TABLE wt_fieldcat LINES wlv_count_col.

*   Function to read the Microsoft Excel file.
    CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename                = p_file
        i_begin_col             = '1'
        i_begin_row             = '1'
        i_end_col               = wlv_count_col
        i_end_row               = '10000'
      TABLES
        intern                  = wlt_file
      EXCEPTIONS
        inconsistent_parameters = 1
        upload_ole              = 2
        OTHERS                  = 3.
    IF sy-subrc NE space.
      WRITE : /3 text-003.
      STOP.
    ENDIF.

*   Set data.
    LOOP AT wlt_file.

      IF wlt_file-row NE wlv_flag_row.
        MOVE wlt_file-row TO wlv_flag_row.
        IF wlt_file-row NE 1.
          APPEND <wfs_structure> TO <wft_table>.
          CLEAR <wfs_structure>.
        ENDIF.
      ENDIF.

      READ TABLE wt_fieldcat
           INTO ws_fieldcat
           INDEX wlt_file-col.

      CHECK sy-subrc EQ space.
      ASSIGN COMPONENT ws_fieldcat-fieldname
             OF STRUCTURE <wfs_structure>
             TO <wlfv_field>.
      CATCH SYSTEM-EXCEPTIONS conversion_errors = 1.
        MOVE wlt_file-value TO <wlfv_field>.
      ENDCATCH.

      IF sy-subrc EQ 1.
        " A FAIRE
      ENDIF.

    ENDLOOP.

*   Append last time.
    APPEND <wfs_structure> TO <wft_table>.


* Si fichier plat.
  ELSEIF p_flat EQ 'X'.

    CALL FUNCTION 'WS_UPLOAD'
      EXPORTING
        filename = p_file
        filetype = 'DAT'
      TABLES
        data_tab = <wft_table>
      EXCEPTIONS
        OTHERS   = 10.

    IF sy-subrc NE space.
      WRITE : /3 text-003.
      STOP.
    ENDIF.

  ENDIF.





* Si vide la table avant.
  IF p_clear EQ 'X'.

*   Verification avant la suppression de la table.
    CALL FUNCTION 'POPUP_TO_CONFIRM'
      EXPORTING
        titlebar       = text-004
        text_question  = text-005
      IMPORTING
        answer         = wlv_char1
      EXCEPTIONS
        text_not_found = 1
        OTHERS         = 2.
    IF wlv_char1 EQ '2'.
      STOP.
    ENDIF.

*   On supprime tout.
    DELETE FROM (p_table) CLIENT SPECIFIED
           WHERE mandt EQ sy-mandt.

    IF sy-subrc NE space.
      WRITE : /3 text-003.
      STOP.
    ENDIF.

  ENDIF.



* Chargement de la base.
  MODIFY (p_table) FROM TABLE <wft_table>.
  IF sy-subrc NE space.
    WRITE : /3 text-003.
    STOP.
  ENDIF.



ENDFORM. " P_CHARGEMENT.









*----------------------------------------------------------------------*
* Form P_CREATE_DYNTABLE.                                              *
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
FORM p_create_dyntable
     USING wpv_tabname TYPE tabname.


  DATA : wlt_table TYPE REF TO data ,
         wls_table TYPE REF TO data .


* On assigne le nom de la table.
  IF <wfv_structname> IS ASSIGNED.
    UNASSIGN <wfv_structname>.
  ENDIF.
  ASSIGN wpv_tabname TO <wfv_structname>.


* Recherche de la description du dictionnaire.
  REFRESH wt_fieldcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = <wfv_structname>
      i_bypassing_buffer     = 'X'
    CHANGING
      ct_fieldcat            = wt_fieldcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.

  IF sy-subrc NE space.
    WRITE : /1 text-003.
    STOP.
  ENDIF.



* Création de la table interne dynamique.
  IF <wft_table> IS ASSIGNED.
    UNASSIGN <wft_table>.
  ENDIF.

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = wt_fieldcat
    IMPORTING
      ep_table        = wlt_table.

  ASSIGN wlt_table->* TO <wft_table>.



* Création de la structure du type de la table.
  IF <wfs_structure> IS ASSIGNED.
    UNASSIGN <wfs_structure>.
  ENDIF.
  CREATE DATA wls_table LIKE LINE OF <wft_table>.
  ASSIGN wls_table->* TO <wfs_structure>.


ENDFORM. " P_CREATE_DYNTABLE.






*------------------------------- EVENTS -------------------------------*
* Au démarage de l'application.
INITIALIZATION.

* On desactive la zone vider la table, elle n'est active que pour
* le chargement.
  LOOP AT SCREEN.
    IF screen-name EQ 'P_CLEAR'.
      MOVE '0' TO screen-active.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.

* A l'événement rafraichissement de l'écran.
AT SELECTION-SCREEN OUTPUT.

  LOOP AT SCREEN.
* Si on charge alors on active la zone P_CLEAR.
    IF p_imp EQ 'X'.
      IF screen-name EQ 'P_CLEAR'.
        MOVE '1' TO screen-active.
        MODIFY SCREEN.
      ENDIF.
    ELSE.
      IF screen-name EQ 'P_CLEAR'.
        MOVE '0' TO screen-active.
        MODIFY SCREEN.
      ENDIF.
    ENDIF.
  ENDLOOP.

* Evenement F4 sur P_FILE.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    IMPORTING
      file_name = p_file.

0 Kudos

Beautiful! Thanks!

0 Kudos

Hi Fred,

I need the full code of for exporting data ..

I am not able to understand the cooments ,

Thanks,

Siva.

Former Member
0 Kudos

ALSM_EXCEL_TO_INTERNAL_TABLE

use this FM to upload from excel to internal table...