帮我用abap语言 写一个通用内表从Excel上传数据的程序
时间: 2024-06-03 20:07:53 浏览: 177
以下是一个简单的示例程序,用于从Excel上传数据到通用内表:
REPORT z_upload_excel_data.
TYPES: BEGIN OF ty_data,
field1 TYPE string,
field2 TYPE string,
field3 TYPE string,
END OF ty_data.
DATA: gt_data TYPE STANDARD TABLE OF ty_data.
DATA: gv_filename TYPE string,
gv_filetype TYPE string,
gv_filepath TYPE string.
PARAMETERS: p_file TYPE rlgrap-filename.
* 获取文件信息
CALL FUNCTION 'WS_UPLOAD'
EXPORTING
filename = p_file
filetype = 'ASC'
IMPORTING
filelength = gv_filelength
filetype = gv_filetype
filename = gv_filename
filepath = gv_filepath
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
header_not_allowed = 8
separator_not_allowed = 9
header_too_long = 10
unknown_dp_error = 11
access_denied = 12
dp_out_of_memory = 13
disk_full = 14
dp_timeout = 15
OTHERS = 16.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
* 读取Excel数据
DATA: gt_excel_data TYPE STANDARD TABLE OF alsmex_tabline.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = gv_filename
i_begin_col = 1
i_begin_row = 2
TABLES
intern = gt_excel_data
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.
* 将Excel数据转换为通用内表
LOOP AT gt_excel_data ASSIGNING FIELD-SYMBOL(<fs_excel_data>).
DATA(lv_data) = <fs_excel_data>-value.
SPLIT lv_data AT cl_abap_char_utilities=>horizontal_tab
INTO TABLE DATA(lt_fields).
IF sy-tabix = 1.
CONTINUE. " 跳过表头行
ENDIF.
APPEND VALUE ty_data(
field1 = lt_fields[1]
field2 = lt_fields[2]
field3 = lt_fields[3]
)
TO gt_data.
ENDLOOP.
* 输出上传的数据
LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<fs_data>).
WRITE: / <fs_data>-field1, <fs_data>-field2, <fs_data>-field3.
ENDLOOP.
阅读全文