Ver la Versión Completa : Como Cargar un Archivo en Excel a una Tabla Interna en SAP
jeanficetola
08/07/09, 16:14:28
Buenos Dias,
Por favor , alguien tendra un codigo ejemplo de como cargar un archivo en excel a una tabla interna en SAP
gracias y saludos
patrigir
08/07/09, 16:24:54
REPORT ZCARGA_CONV_CUENTAS NO STANDARD PAGE HEADING LINE-SIZE 110.
************************************************************************
* Definición variables y tablas
************************************************************************
DATA i_dynfields LIKE dynpread OCCURS 1 WITH HEADER LINE.
DATA: BEGIN OF i_table OCCURS 0,
l_line(100) type c.
DATA: END OF i_table.
************************************************************************
* Definition of parameters and ranges of selection.
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME title text-001.
PARAMETERS: p_file type localfile OBLIGATORY.
SELECTION-SCREEN END OF BLOCK block1.
************************************************************************
* AT SELECTION-SCREEN
************************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
*subroutine that allows to look for the wished file
PERFORM seleccionar_fichero USING p_file.
************************************************************************
* START-OF-SELECTION. *
************************************************************************
START-OF-SELECTION.
* Carga de datos de la tabla con origen en fichero
perform f_carga_fichero tables i_table
using p_file.
* El contenido de la tabla se pone en la tabla ZPAT_PROVA
perform f_actualizar_tabla tables i_table.
*&---------------------------------------------------------------------*
*& Form SELECCIONAR_FICHERO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_P_FILE text
*----------------------------------------------------------------------*
FORM SELECCIONAR_FICHERO USING P_P_FILE.
CALL FUNCTION 'F4_FILENAME'
IMPORTING
file_name = p_p_file.
ENDFORM. " SELECCIONAR_FICHERO
*&---------------------------------------------------------------------*
*& Form F_CARGA_FICHERO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_I_TABLE text
* -->P_P_FILE text
*----------------------------------------------------------------------*
FORM F_CARGA_FICHERO TABLES i_table STRUCTURE i_table
USING P_P_FILE.
data: v_files type string.
v_files = p_p_file.
refresh i_table.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = v_files
filetype = 'ASC'
TABLES
data_tab = i_table
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.
write: /'Not good'.
endif.
ENDFORM. " F_CARGA_FICHERO
*&---------------------------------------------------------------------*
*& Form F_ACTUALIZAR_TABLA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_I_TABLE_L_LINE text
*----------------------------------------------------------------------*
FORM F_ACTUALIZAR_TABLA TABLES P_I_TABLE STRUCTURE i_table.
data: C_TAB TYPE C VALUE cl_abap_char_utilities=>horizontal_tab.
DATA: I_TAB TYPE ZCONV_CUENTAS,
l_tabix type sy-tabix.
loop at P_I_TABLE.
l_tabix = sy-tabix.
split p_i_table-l_line at c_tab into
i_tab-zpcvu
i_tab-ztext
i_tab-zcaes.
i_tab-mandt = sy-mandt.
modify zconv_cuentas from i_tab.
if sy-subrc ne 0.
exit.
endif.
endloop.
ENDFORM. " F_ACTUALIZAR_TABLA
patrigir
08/07/09, 16:26:00
el archivo excel pásalo a .txt !! me olvidé
jeanficetola
08/07/09, 17:14:07
Tendras un codigo con tecnologia OLE
gracias
DavidXD_XD
08/07/09, 17:21:10
Hola, puedes usar la funcion ALSM_EXCEL_TO_INTERNAL_TABLE y la carga lo hago de la siguiente forma:
Se cargara los datos del excel dentro de la tabla interna me->gt_itab, xsiacaso la estructura de la tabla interna (tab_itab) no tiene importancia, puesto que con la rutina del metodo TRANSFORM_DATA se adapta a cualquier estructura, luego los metodos los puedes reemplazar por FORM normales :D
*----------------------------------------------------------------------*
* CLASS lcl_file DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_file DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING i_path TYPE char128,
build_file.
PRIVATE SECTION.
METHODS: upload_file,
transform_data.
DATA: g_path TYPE rlgrap-filename,
gt_itab TYPE tab_itab,
gt_excel TYPE TABLE OF alsmex_tabline.
ENDCLASS.
CLASS lcl_file IMPLEMENTATION.
*----------------------------------------------------------------------*
* Constructor : Inicializando Variables Privadas *
*----------------------------------------------------------------------*
METHOD constructor.
* Inicializando Variables: pasamos la ruta del archivo excel
g_path = i_path.
ENDMETHOD.
*----------------------------------------------------------------------*
* Levantando archivo excel a tabla interna *
*----------------------------------------------------------------------*
METHOD build_file.
* Levantando el archivo Excel
CALL METHOD upload_file.
* Transformando la informacion
CALL METHOD transform_data.
ENDMETHOD.
*----------------------------------------------------------------------*
* Levantando archivo excel a tabla interna *
*----------------------------------------------------------------------*
METHOD upload_file.
* Variables Locales
DATA: l_start_col TYPE i VALUE 1,
l_start_row TYPE i VALUE 2,
l_end_col TYPE i VALUE 12,
l_end_row TYPE i VALUE 65536.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = me->g_path
i_begin_col = l_start_col
i_begin_row = l_start_row
i_end_col = l_end_col
i_end_row = l_end_row
TABLES
intern = me->gt_excel
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.
ENDMETHOD.
*----------------------------------------------------------------------*
* Transformando la informacion levantada *
*----------------------------------------------------------------------*
METHOD transform_data.
* Variables Locales
DATA: l_cont TYPE i.
FIELD-SYMBOLS: <fs_excel> LIKE LINE OF me->gt_excel,
<fs_itab> LIKE LINE OF me->gt_itab,
<fs> TYPE any.
* Aqui agrupamos cada celda del excel y llenamos la tabla interna
LOOP AT me->gt_excel ASSIGNING <fs_excel>.
AT NEW row.
APPEND INITIAL LINE TO me->gt_itab ASSIGNING <fs_itab>.
ENDAT.
l_cont = <fs_excel>-col.
ASSIGN COMPONENT l_cont OF STRUCTURE <fs_itab> TO <fs>.
<fs> = <fs_excel>-value.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
Espero te pueda ayudar :cool:
www.mundosap.com 2006 - Spain
software crm, crm on demand, software call center, crm act, crm solutions, crm gratis, crm web