Sie sind auf Seite 1von 3

Upload any Database Table

This is a global program to upload any database table in SAP environment.

Simply select the Path of the file and Table name.

Helpful Source Code:-

REPORT zupload_table1.

TYPE-POOLS: slis.

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,


<dyn_wa>,
<dyn_field>.

DATA: it_fldcat TYPE lvc_t_fcat,


wa_it_fldcat TYPE lvc_s_fcat.

TYPE-POOLS : abap.

DATA: new_table TYPE REF TO data,


new_line TYPE REF TO data.

DATA: xcel TYPE TABLE OF alsmex_tabline WITH HEADER LINE.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text .


PARAMETERS: p_file TYPE rlgrap�filename DEFAULT �c:Test.csv�,
*parameters: p_flds type i.
p_table TYPE dd02l�tabname OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.


DATA :
pt_file TYPE filetable,
wa TYPE LINE OF filetable,
user_action TYPE i.

CALL METHOD cl_gui_frontend_services=>file_open_dialog


EXPORTING
window_title = �Data Select�
default_filename = �
multiselection = � �
CHANGING
file_table = pt_file
rc = user_action
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3.

READ TABLE pt_file INTO wa INDEX 1.


p_file = wa�filename.

START-OF-SELECTION.

* Add X number of fields to the dynamic itab cataelog


* BREAK-POINT.

CALL FUNCTION �LVC_FIELDCATALOG_MERGE�


EXPORTING
i_structure_name = p_table
CHANGING
ct_fieldcat = it_fldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 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.

* Create dynamic internal table and assign to FS


CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fldcat
IMPORTING
ep_table = new_table.

ASSIGN new_table->* TO <dyn_table>.

* Create dynamic work area and assign to FS


CREATE DATA new_line LIKE LINE OF <dyn_table>.
ASSIGN new_line->* TO <dyn_wa>.
DATA :lt_raw TYPE truxs_t_text_data.

CALL FUNCTION �TEXT_CONVERT_XLS_TO_SAP�


EXPORTING
* I_FIELD_SEPERATOR =
i_line_header = �X�
i_tab_raw_data = lt_raw
i_filename = p_file
TABLES
i_tab_converted_data = <dyn_table>
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy�subrc <> 0.
* Implement suitable error handling here
ENDIF.

** Upload the excel


* CALL FUNCTION �ALSM_EXCEL_TO_INTERNAL_TABLE�
* EXPORTING
* filename = p_file
* i_begin_col = �1�
* i_begin_row = �1�
* i_end_col = �200�
* i_end_row = �60000�
* TABLES
* intern = xcel
* EXCEPTIONS
* inconsistent_parameters = 1
* upload_ole = 2
* OTHERS = 3.

** Reformt to dynamic internal table


* LOOP AT xcel.
* ASSIGN COMPONENT xcel-col OF STRUCTURE <dyn_wa> TO <dyn_field>.
* IF sy-subrc = 0.
* <dyn_field> = xcel-value.
* ENDIF.
*
* AT END OF row.
* APPEND <dyn_wa> TO <dyn_table>.
* CLEAR <dyn_wa>.
* ENDAT.
* ENDLOOP.

MODIFY (p_table) FROM TABLE <dyn_table>.


IF sy�subrc = 0.
WRITE �Data uploaded successfully�.
ELSE.
WRITE �Data uploaded Error�.
ENDIF.

Das könnte Ihnen auch gefallen