场景:ABAP SM30添加导入excel的功能按钮
需求提出:SM30维护试图时可以导入excel数据:
第一步,为已创建的维护试图添加导入按钮:
找到对应的status,如图:
进入tcode:SE41 找到并进入上图对应的status,(注意:此处的维护status应该是EULG),点击更改!
新增导入按钮
至此按钮添加完毕
第二步,添加对应事件处理:
按钮事件一般在PAI处理就行了: 添加一个新MODULE。
UPLOAD内部代码
代码语言:javascript复制MODULE upload INPUT.
DATA: lt_itab TYPE STANDARD TABLE OF ****,
ls_wa LIKE LINE OF lt_itab.
DATA: lt_tab TYPE filetable,
ls_file_table TYPE file_table,
ls_file LIKE LINE OF lt_file_table,
lv_rc TYPE i,
lv_user_action TYPE i.
DATA: lt_data TYPE alsmex_tabline OCCURS 0 WITH HEADER LINE.
DATA lv_file TYPE rlgrap-filename.
CONSTANTS: lc_default_extension TYPE string VALUE 'xlsx',
lc_file_filter TYPE string VALUE '*.xlsx'.
CONSTANTS: lv_beign_row TYPE i VALUE 1,
lv_end_row TYPE i VALUE 3000,
lv_begin_col TYPE i VALUE 1,
lv_end_col TYPE i VALUE 2.
CASE function.
WHEN 'IMPORT'.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = 'Select File'
* default_filename = '*.XLSX'
file_filter = 'Excel Files (*.xlsx)|*.xlsx|Excel Files (*.xls)|*.xls'
initial_directory = '/' "初始化的目录
* multiselection = 'X'
CHANGING
file_table = lt_tab
rc = lv_rc.
IF sy-subrc <> 0 AND sy-subrc <> 3.
MESSAGE e000(****) WITH '选择文件出错!'(007).
ENDIF.
READ TABLE lt_tab INTO ls_file_table INDEX 1.
lv_file = ls_file_table.
IF ls_file_table-filename = ''.
MESSAGE S000(****) WITH '未选择文件!' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = lv_file
i_begin_col = lv_begin_col
i_begin_row = lv_beign_row
i_end_col = lv_end_col
i_end_row = lv_end_row
"如果需要自定义sheet name,百度搜索自建方法,很简单,教程很多,这里不做赘述
TABLES
intern = lt_data "此处放上面定义的Excel专用内表
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF lt_data[] IS INITIAL .
MESSAGE s000(****) WITH '文件无数据或文件格式出错,请重新选择文件!' DISPLAY LIKE 'E'.
SORT lt_data BY row col ASCENDING.
ENDIF.
LOOP AT lt_data. "将上面定义的excel专用内表赋值给alv出力内表
CASE lt_data-col.
WHEN 1.
ls_wa-pcon_dec_code = lt_data-value.
WHEN 2.
ls_wa-pcon_ran_dec = lt_data-value.
APPEND ls_wa TO lt_itab."对于这个例子,excel表有3列,append要加在最后一列.
CLEAR ls_wa.
ENDCASE.
ENDLOOP.
MODIFY **** FROM TABLE lt_itab.
IF sy-subrc = 0.
MESSAGE s000(***) WITH '导入并更新****成功'.
COMMIT WORK.
ELSE.
MESSAGE s000(***) WITH '导入并更新****失败'.
ROLLBACK WORK.
ENDIF.
ENDCASE.
ENDMODULE. " UPLOAD INPUT
结语:
按照上述步骤导入就可以了,代码可能略显粗糙,主要提供思路不是最终版,我的代码有个缺点,这种方法导入就直接更新底表了,查看数据需要重新进此维护试图,期待大神改进方法,把导入的数据放入”新条目“的内表中去用标准保存。