ABAP 导入excel示例程序
批量导入程序,需要使用到导入模板,首先需要创建程序,之后是需要创建excel导入模板,并且需要将excel导入模板上传到SAP系统里面,之后才可以下载下来进行导入。
创建程序
ABAP 批量导入excel,右击创建程序:
*&---------------------------------------------------------------------*
*& Report z_test_import_excel
*&---------------------------------------------------------------------*
*& TYPE:Report
*& Description:批量导入excel测试
*& Creator:刘洪裕
*& Created On:2024年2月19日
*&---------------------------------------------------------------------*
* Change History
* ---------- ------------- ------------- -------------
* Version Date Change By Description
* ---------- ------------- ------------- -------------
* N 2024.2.19 Hongyu.Liu 创建
*&---------------------------------------------------------------------*
Report z_test_import_excel NO STANDARD PAGE HEADING MESSAGE-ID zfimf01.
TABLES: sscrfields.
"定义相关变量
*TYPES: BEGIN OF ty_data,
* werks TYPE zfi_mfwb-werks, "工厂
* zmj_matnr TYPE zfi_mfwb-zmj_matnr, "模具料号
* zcp_matnr TYPE zfi_mfwb-zcp_matnr, "挤压产出成品料号
* zdmczl TYPE zfi_mfwb-zdmczl, "单套模具预计产出重量
* zsfty TYPE zfi_mfwb-zsfty, "是否通用
* zwhrj TYPE zfi_mfwb-zwhrj, "维护日期
* zwhr TYPE zfi_mfwb-zwhr, "维护人
*END OF ty_data.
*DATA gs_data TYPE ty_data. "工作区
*DATA gt_data TYPE TABLE OF ty_data. "内表
*&---------------------------------------------------------------------*
*& 定义相关变量
*&---------------------------------------------------------------------*
TYPES: BEGIN OF ts_output, "展示结构
"INCLUDE STRUCTURE zfi_mfwb.
werks TYPE zfi_mfwb-werks, "工厂
zxlh TYPE zfi_mfwb-zxlh, "序列号
zmj_matnr TYPE zfi_mfwb-zmj_matnr, "模具料号
zcp_matnr TYPE zfi_mfwb-zcp_matnr, "挤压产出成品料号
"zdmczl TYPE p DECIMALS 2 , "单套模具预计产出重量,两位小数
zdmczl TYPE zfi_mfwb-zdmczl, "单套模具预计产出重量
"zsfty TYPE zfi_mfwb-zsfty, "是否通用
zsfty TYPE abap_bool, "是否通用
zwhrj TYPE zfi_mfwb-zwhrj, "维护日期
zwhr TYPE zfi_mfwb-zwhr, "维护人
zsfbf TYPE zfi_mfwb-zsfbf, "是否报废
type TYPE bapiret2-type,
message TYPE char100, "bapiret2-message,
icon TYPE text20,
slbox TYPE c,
celltab TYPE lvc_t_styl, "设置单元格编辑状态
END OF ts_output.
TYPES: BEGIN OF ty_data,"导入数据结构
werks TYPE zfi_mfwb-werks, "工厂
zxlh TYPE zfi_mfwb-zxlh, "序列号
zmj_matnr TYPE zfi_mfwb-zmj_matnr, "模具料号
zcp_matnr TYPE zfi_mfwb-zcp_matnr, "挤压产出成品料号
"zdmczl TYPE p DECIMALS 2 , "单套模具预计产出重量,两位小数
zdmczl TYPE zfi_mfwb-zdmczl, "单套模具预计产出重量
"zdmczl LIKE vbap-ntgew, "单套模具预计产出重量
zsfty TYPE zfi_mfwb-zsfty, "是否通用
zwhrj TYPE zfi_mfwb-zwhrj, "维护日期
zwhr TYPE zfi_mfwb-zwhr, "维护人
END OF ty_data.
*DATA: is_data TYPE ty_data. "导入结构的工作区
*DATA: it_data TYPE TABLE OF ty_data. "导入结构的工作表
CONSTANTS:cos_template TYPE w3objid VALUE 'ZFIMFWB01',
cos_filename TYPE string VALUE '模具分摊维护表导入模板.xlsx'.
DATA: gs_layout TYPE lvc_s_layo,
gt_fieldcat TYPE lvc_t_fcat,
gs_fieldcat TYPE lvc_s_fcat,
gs_variant TYPE disvariant. "屏幕变式
DATA: gt_output TYPE STANDARD TABLE OF ts_output WITH DEFAULT KEY,
gs_output TYPE ts_output.
"DATA: gt_input TYPE TABLE OF zfi_mfwb. "内表结构与zfi_mfwb一致
DATA: gt_input TYPE TABLE OF ty_data. "导入模板数据
DATA: gt_output_old TYPE STANDARD TABLE OF ts_output WITH DEFAULT KEY."被修改前的数据
DATA: gt_bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE. "用来存储屏幕字段参数值,传递录屏参数
DATA: gt_messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE. "记录执行BDC过程中产生的消息
DATA: gv_mode TYPE c VALUE 'N'.
* 用于 修改要提示,删除要提示
DATA:gv_edit_flag TYPE c, "X 表示修改过
gv_save_flag TYPE c, "X 表示已经保存。
gv_check_flag TYPE c, "X 表示已经保存。
gv_sref_flag TYPE c, "X 表示刷新。
gv_line TYPE int4. "行数
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handle_data_changed
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed,
handle_data_changed_finished
FOR EVENT data_changed_finished OF cl_gui_alv_grid
IMPORTING
e_modified
et_good_cells.
ENDCLASS.
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_data_changed.
* PERFORM FRM_DATA_CHANGED
* USING ER_DATA_CHANGED.
ENDMETHOD.
METHOD handle_data_changed_finished.
* PERFORM FRM_DATA_CHANGED_FINISH
* USING E_MODIFIED ET_GOOD_CELLS.
ENDMETHOD.
ENDCLASS.
*&---------------------------------------------------------------------*
*& 选择屏幕
*&---------------------------------------------------------------------*
SELECTION-SCREEN FUNCTION KEY 1. "这是在屏幕的执行按钮后的位置添加一个功能按钮
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE TEXT-001.
PARAMETERS p_file TYPE rlgrap-filename MODIF ID m1.
SELECTION-SCREEN END OF BLOCK blk1.
*&---------------------------------------------------------------------*
*& 定义相关变量
*&---------------------------------------------------------------------*
FORM frm_init_data.
DATA: ls_textfield TYPE smp_dyntxt. "菜单制作器:动态文本的程序接口
ls_textfield-icon_id = '@49@'.
ls_textfield-icon_text = '下载导入模板'.
sscrfields-functxt_01 = ls_textfield.
ENDFORM.
*&---------------------------------------------------------------------*
*& 屏幕事件
*&---------------------------------------------------------------------*
INITIALIZATION. "初始化
"选择屏幕添加按钮
PERFORM frm_init_data.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. "按选择帮助<F4>键时触发该事件
PERFORM frm_get_file_name CHANGING p_file.
AT SELECTION-SCREEN. "在此事件里可以进行对用户输入进行完整性进行检查。
* 处理选择屏幕的按钮 下载模板
PERFORM frm_screen_button_event.
" AT SELECTION SCREEN OUTPUT事件(也就是PBO) ,在这个事件里你可以通过修改系统默认screen内表修改屏幕的某些属性
AT SELECTION-SCREEN OUTPUT.
* 修改选择屏幕的元素的状态
PERFORM modify_selction_screen_status.
START-OF-SELECTION. "仅在选择屏幕处理结束之后触发,即当用户点击“执行”按钮时
PERFORM frm_check_authority.
PERFORM batch_input.
PERFORM alv_display USING 'X'.
*&---------------------------------------------------------------------*
*& Form frm_get_file_name:获取文件名称
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& <-- P_FILE
*&---------------------------------------------------------------------*
FORM frm_get_file_name CHANGING cv_file.
DATA: lt_filetable TYPE filetable, "表类型
ls_filetable TYPE file_table, "结构类型
lv_rc TYPE i.
FREE lt_filetable.
CLEAR: lv_rc.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
* window_title =
default_extension = cl_gui_frontend_services=>filetype_excel
* default_filename =
file_filter = cl_gui_frontend_services=>filetype_excel
* with_encoding =
* initial_directory =
* multiselection =
CHANGING
file_table = lt_filetable
rc = lv_rc
* user_action =
* file_encoding =
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5
.
READ TABLE lt_filetable INTO ls_filetable INDEX 1.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
IF sy-subrc EQ 0.
cv_file = ls_filetable-filename.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_screen_button_event:点击下载模板按钮
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_screen_button_event .
CASE sscrfields-ucomm.
WHEN 'FC01'.
PERFORM frm_download_template USING cos_template cos_filename.
WHEN OTHERS.
ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form modify_selction_screen_status
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM modify_selction_screen_status .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_check_authority
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_check_authority .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form batch_input
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM batch_input .
* EXCEL数据到内表
PERFORM file_data_input.
* 转换内表
PERFORM file_data_convert.
* 检查数据
PERFORM file_data_check.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form alv_display:展示alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> P_
*&---------------------------------------------------------------------*
FORM alv_display USING u_edit TYPE abap_bool .
PERFORM set_alv_layout.
PERFORM build_fieldcat USING u_edit.
PERFORM call_alv_func.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_download_template:下载模板
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> COS_TEMPLATE
*& --> COS_FILENAME
*&---------------------------------------------------------------------*
FORM frm_download_template USING u_objid TYPE w3objid
u_file_name TYPE string.
DATA: ls_key TYPE wwwdatatab,
lv_filename TYPE string,
lv_path TYPE string,
lv_fullpath TYPE string.
"判断模板是否存在
SELECT SINGLE * FROM wwwdata
WHERE relid EQ 'MI'
AND objid EQ @u_objid
INTO CORRESPONDING FIELDS OF @ls_key.
IF sy-subrc NE 0.
* 模板&1不存在
MESSAGE s007 WITH u_objid.
RETURN.
ENDIF.
* 调用函数打开文件选择框
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
* window_title =
default_extension = cl_gui_frontend_services=>filetype_excel
default_file_name = u_file_name
* with_encoding =
* file_filter =
* initial_directory =
* prompt_on_overwrite = 'X'
CHANGING
filename = lv_filename
path = lv_path
fullpath = lv_fullpath
* user_action =
* file_encoding =
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
invalid_default_file_name = 4
OTHERS = 5
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CHECK lv_fullpath NE ''.
* 下载SMW0模版
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = ls_key
destination = CONV localfile( lv_fullpath ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form file_data_input:EXCEL数据到内表
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM file_data_input .
* 数据EXCEL-》内表
PERFORM frm_excel_to_table TABLES gt_input USING p_file 2.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form file_data_convert:转换内表
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM file_data_convert .
* 添加前导0
LOOP AT gt_input ASSIGNING FIELD-SYMBOL(<lfs_input>).
<lfs_input>-zmj_matnr = |{ <lfs_input>-zmj_matnr ALPHA = IN }|.
<lfs_input>-zcp_matnr = |{ <lfs_input>-zcp_matnr ALPHA = IN }|.
ENDLOOP.
gt_output = CORRESPONDING #( gt_input ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form file_data_check:检查数据
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM file_data_check .
IF gt_output IS NOT INITIAL.
LOOP AT gt_output ASSIGNING FIELD-SYMBOL(<ls_output>).
CLEAR:<ls_output>-type,
<ls_output>-message.
IF <ls_output>-werks IS INITIAL.
<ls_output>-type = 'E'.
<ls_output>-message = '工厂不能为空;'.
ENDIF.
IF <ls_output>-zmj_matnr IS INITIAL.
<ls_output>-type = 'E'.
<ls_output>-message = <ls_output>-message && '模具料号不能为空;'.
ENDIF.
IF <ls_output>-zcp_matnr IS INITIAL.
<ls_output>-type = 'E'.
<ls_output>-message = <ls_output>-message && '挤压产出成品料号不能为空;'.
ENDIF.
IF <ls_output>-type <> 'E'. "如果关键值不为空
<ls_output>-type = 'N'. "将类型设置为N
ENDIF.
PERFORM set_light CHANGING <ls_output>. "设置提示灯
ENDLOOP.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_excel_to_table
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> GT_INPUT
* -->U_FILE 文件
* -->U_START_ROW 文件内的数据从哪行开始取
*&---------------------------------------------------------------------*
FORM frm_excel_to_table TABLES et_input TYPE STANDARD TABLE
USING u_file TYPE rlgrap-filename
u_start_row TYPE i.
DATA: lt_intern TYPE STANDARD TABLE OF zalsmex_tabline.
FREE: lt_intern.
*DATA FILENAME TYPE RLGRAP-FILENAME.
*DATA I_BEGIN_COL TYPE I.
*DATA I_BEGIN_ROW TYPE I.
*DATA I_END_COL TYPE I.
*DATA I_END_ROW TYPE I.
*DATA INTERN TYPE STANDARD TABLE OF ZALSMEX_TABLINE.
CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = u_file
i_begin_col = 1
i_begin_row = 2
i_end_col = 99
i_end_row = 65536
TABLES
intern = lt_intern
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3
.
IF sy-subrc NE 0.
ENDIF.
SORT lt_intern BY row col.
LOOP AT lt_intern INTO DATA(ls_intern).
"下面检查是否是数字
AT NEW row. "如果字段row及row的左侧全部字段的数据,与上一行数据不一致,则执行代码。
APPEND INITIAL LINE TO et_input ASSIGNING FIELD-SYMBOL(<ls_input>). "创建一个新行,并分配指针
ENDAT.
ASSIGN COMPONENT ls_intern-col OF STRUCTURE <ls_input> TO FIELD-SYMBOL(<lv_val>)."就是在每次循环中,都会分配一个结构的字段,是根据索引来决定的
IF <lv_val> IS ASSIGNED.
IF ls_intern-col EQ 5. "如果是第5列(是否通用)
<lv_val> = ls_intern-value.
ENDIF.
TRY.
<lv_val> = ls_intern-value.
CATCH cx_sy_conversion_no_number INTO DATA(lr_cx_sy_conversion_no_number).
* 行&1列&2值&3,&4
MESSAGE e003 WITH ls_intern-row ls_intern-col ls_intern-value lr_cx_sy_conversion_no_number->get_text( ).
CATCH cx_sy_conversion_overflow INTO DATA(lr_cx_sy_conversion_overflow).
* 行&1列&2值&3,&4
MESSAGE e003 WITH ls_intern-row ls_intern-col ls_intern-value lr_cx_sy_conversion_overflow->get_text( ).
CATCH cx_root INTO DATA(lr_cx_root).
* 行&1列&2值&3,&4
MESSAGE e003 WITH ls_intern-row ls_intern-col ls_intern-value lr_cx_root->get_text( ).
ENDTRY.
UNASSIGN <lv_val>. "取消分配
ENDIF.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form set_alv_layout:设置alv的layout
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM set_alv_layout .
CLEAR: gs_layout,gs_variant.
gs_layout-zebra = 'X'. "斑马线
gs_layout-cwidth_opt = 'X'. "自动列宽
gs_layout-box_fname = 'SLBOX'. "选择框字段名 "用户在ALV控件的行选中,对应的内存中【SLBOX】字段会被打上X
gs_variant-report = sy-repid.
gs_variant-handle = '01'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form build_fieldcat
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> U_EDIT
*&---------------------------------------------------------------------*
FORM build_fieldcat USING e_edit TYPE abap_bool.
IF gt_fieldcat IS INITIAL. "如果gt_fieldcat是初始状态
APPEND VALUE #( fieldname = 'ICON' ref_field = 'ID' ref_table = 'ICON' scrtext_l = '状态' ) TO gt_fieldcat[].
APPEND VALUE #( fieldname = 'MESSAGE' ref_field = 'MESSAGE' ref_table = 'BAPIRET2' scrtext_l = '' ) TO gt_fieldcat[].
ENDIF.
* DATA CT_FIELDCAT TYPE LVC_T_FCAT.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
* I_BUFFER_ACTIVE = I_BUFFER_ACTIVE
i_structure_name = 'ZFI_MFWB'
* I_CLIENT_NEVER_DISPLAY = 'X'
* I_BYPASSING_BUFFER = I_BYPASSING_BUFFER
* I_INTERNAL_TABNAME = I_INTERNAL_TABNAME
CHANGING
ct_fieldcat = gt_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
* 循环字段,修改显示的列名
CLEAR gs_fieldcat.
LOOP AT gt_fieldcat INTO gs_fieldcat.
IF gs_fieldcat-fieldname = 'ZMJ_MATNR'.
gs_fieldcat-coltext = '模具料号'.
ELSEIF gs_fieldcat-fieldname = 'ZXLH'.
gs_fieldcat-coltext = '序列号'.
ELSEIF gs_fieldcat-fieldname = 'ZCP_MATNR'.
gs_fieldcat-coltext = '挤出产出成品料号'.
ELSEIF gs_fieldcat-fieldname = 'ZDMCZL'.
gs_fieldcat-coltext = '单套模具预计产出重量'.
gs_fieldcat-edit = 'X'."可编辑
* gs_fieldcat-edit = ''."不可编辑
* IF p_file IS INITIAL. "如果不是导入文件的操作,则设置重量为编辑
* gs_fieldcat-edit = 'X'."可编辑
* ENDIF.
ELSEIF gs_fieldcat-fieldname = 'ZSFTY'.
gs_fieldcat-coltext = '是否通用'.
gs_fieldcat-edit = 'X'."可编辑
ELSEIF gs_fieldcat-fieldname = 'ZWHRJ'.
gs_fieldcat-coltext = '维护日期'.
ELSEIF gs_fieldcat-fieldname = 'ZWHR'.
gs_fieldcat-coltext = '维护人'.
ELSEIF gs_fieldcat-fieldname = 'ZSFBF'.
gs_fieldcat-coltext = '是否报废'.
"gs_fieldcat-edit = 'X'."可编辑
ENDIF.
MODIFY gt_fieldcat FROM gs_fieldcat.
CLEAR gs_fieldcat.
ENDLOOP.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form call_alv_func:调用alv显示函数
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM call_alv_func .
DATA: lt_event TYPE slis_t_event,
ls_event TYPE slis_alv_event.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'SET_PF_STATUS'
i_callback_user_command = 'USER_COMMAND'
* I_CALLBACK_TOP_OF_PAGE = ' '
* i_callback_html_top_of_page = 'FRM_TOP_OF_PAGE'
is_layout_lvc = gs_layout
it_fieldcat_lvc = gt_fieldcat
i_save = 'U'
is_variant = gs_variant
it_events = lt_event
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER = E_EXIT_CAUSED_BY_CALLER
* ES_EXIT_CAUSED_BY_USER = ES_EXIT_CAUSED_BY_USER
TABLES
t_outtab = gt_output
EXCEPTIONS
program_error = 1
OTHERS = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM FRM_TOP_OF_PAGE
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
FORM frm_top_of_page USING p_cl_dd TYPE REF TO cl_dd_document.
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM SET_PF_STATUS
*&---------------------------------------------------------------------*
*& TEXT
*&---------------------------------------------------------------------*
*& --> P1 TEXT
*& <-- P2 TEXT
*&---------------------------------------------------------------------*
FORM set_pf_status USING u_t_extab TYPE slis_t_extab.
SET PF-STATUS 'STANDARD' EXCLUDING u_t_extab.
SET TITLEBAR 'STANDARD'.
ENDFORM.
*&---------------------------------------------------------------------*
*& FORM USER_COMMAND
*&---------------------------------------------------------------------*
FORM user_command USING u_v_ucomm LIKE sy-ucomm
u_s_selfield TYPE slis_selfield.
DATA: lr_grid TYPE REF TO cl_gui_alv_grid,
lv_error TYPE char1,
lv_confirm TYPE c.
DATA: lt_mod_data TYPE lvc_t_modi,
ls_mod_data TYPE lvc_s_modi.
DATA: lo_data TYPE REF TO cl_alv_changed_data_protocol.
*DATA IR_SALV_FULLSCREEN_ADAPTER TYPE REF TO CL_SALV_FULLSCREEN_ADAPTER.
*DATA ET_EXCLUDING TYPE SLIS_T_EXTAB.
*DATA E_REPID TYPE SY-REPID.
*DATA E_CALLBACK_PROGRAM TYPE SY-REPID.
*DATA E_CALLBACK_ROUTINE TYPE SLIS_FORMNAME.
*DATA E_GRID TYPE REF TO CL_GUI_ALV_GRID.
*DATA ET_FIELDCAT_LVC TYPE LVC_T_FCAT.
*DATA ER_TRACE TYPE REF TO CL_ALV_TRACE.
*DATA E_FLG_NO_HTML TYPE C.
*DATA ES_LAYOUT_KKBLO TYPE KKBLO_LAYOUT.
*DATA ES_SEL_HIDE TYPE SLIS_SEL_HIDE_ALV.
*DATA ET_EVENT_EXIT TYPE KKBLO_T_EVENT_EXIT.
*DATA ER_FORM_TOL TYPE REF TO CL_SALV_FORM.
*DATA ER_FORM_EOL TYPE REF TO CL_SALV_FORM.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
* EXPORTING
* IR_SALV_FULLSCREEN_ADAPTER = IR_SALV_FULLSCREEN_ADAPTER
IMPORTING
e_GRID = lr_grid.
lr_grid->check_changed_data( ).
CASE u_v_ucomm.
WHEN 'SAVE'.
PERFORM file_data_check.
PERFORM refresh_alv.
PERFORM user_command_import_check CHANGING lv_error.
CHECK lv_error = ''.
PERFORM check_user_decision USING '确认' '确定保存?' CHANGING lv_confirm.
CHECK lv_confirm EQ abap_true.
PERFORM user_command_import.
gv_save_flag = 'X'.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
CLEAR: gv_edit_flag,gv_save_flag.
WHEN OTHERS.
ENDCASE.
PERFORM refresh_alv.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form refresh_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM refresh_alv .
DATA: lr_grid TYPE REF TO cl_gui_alv_grid,
ls_stable TYPE lvc_s_stbl.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lr_grid.
lr_grid->get_frontend_layout(
IMPORTING
es_layout = DATA(ls_layout)
).
ls_layout-cwidth_opt = abap_true.
lr_grid->set_frontend_layout( is_layout = ls_layout ).
ls_stable-row = 'X'.
ls_stable-col = 'X'.
lr_grid->refresh_table_display(
EXPORTING
is_stable = ls_stable
EXCEPTIONS
finished = 1
OTHERS = 2 ).
IF sy-subrc <> 0.
MESSAGE ID sy-msgid
TYPE sy-msgty
NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form user_command_import_check
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& <-- LV_ERROR
*&---------------------------------------------------------------------*
FORM user_command_import_check CHANGING c_error TYPE char1.
CLEAR c_error.
IF lines( gt_output ) = 0.
MESSAGE s000 WITH '导入数据为空' DISPLAY LIKE 'E'.
c_error = 'X'.
EXIT.
ENDIF.
IF NOT line_exists( gt_output[ slbox = 'X' ] ).
MESSAGE s000 WITH '至少选中一行数据' DISPLAY LIKE 'E'.
c_error = 'X'.
EXIT.
ENDIF.
IF line_exists( gt_output[ type = 'E' slbox = 'X' ] ).
MESSAGE s000 WITH '数据检查不通过,不可导入' DISPLAY LIKE 'E'.
c_error = 'X'.
EXIT.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form check_user_decision:确认用户决定
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> P_
*& --> P_
*& <-- LV_CONFIRM
*&---------------------------------------------------------------------*
FORM check_user_decision USING u_title
u_question
CHANGING ch_confirm.
DATA: lv_answer TYPE char1.
CLEAR: ch_confirm.
CLEAR: lv_answer.
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
titlebar = u_title
text_question = u_question
default_button = '2'
display_cancel_button = ''
IMPORTING
answer = lv_answer
EXCEPTIONS
text_not_found = 1
OTHERS = 2.
IF sy-subrc NE 0.
ENDIF.
IF lv_answer EQ '1'.
ch_confirm = abap_true.
ELSE.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form user_command_import
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM user_command_import .
DATA: gs_input TYPE zfi_mfwb. "工作区结构与zfi_mfwb一致
LOOP AT gt_output ASSIGNING FIELD-SYMBOL(<lfs_output>) WHERE slbox = abap_true.
MOVE-CORRESPONDING <lfs_output> TO gs_input.
gs_input-mandt = sy-mandt. "当前集团号
gs_input-zwhrj = sy-datum. "当前日期
gs_input-zwhr = sy-uname. "当前登录用户名
INSERT zfi_mfwb FROM gs_input.
IF sy-subrc <> 0.
<lfs_output>-type = 'E'.
IF sy-subrc = 4.
<lfs_output>-message = '导入失败!数据库已存在此条数据'.
ENDIF.
ENDIF.
IF sy-subrc = 0.
<lfs_output>-type = 'S'.
<lfs_output>-message = '导入成功!'.
ENDIF.
PERFORM set_light CHANGING <lfs_output>. "设置提示灯
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form USER_COMMAND_EXIT_CHECK
*&---------------------------------------------------------------------*
*& 如果没保存就退出 弹框提示
*&---------------------------------------------------------------------*
*& <-- LV_ERROR
*&---------------------------------------------------------------------*
FORM user_command_exit_check CHANGING c_error.
DATA: lv_confirm TYPE c.
* IF p_cre = 'X'.
* IF NOT line_exists( gt_output[ type = 'E' ] ).
* gv_edit_flag = 'X'.
* ENDIF.
* ENDIF.
IF gv_edit_flag = 'X' AND gv_save_flag = '' .
PERFORM check_user_decision USING '确认'
'还没保存,确定退出?'
CHANGING lv_confirm.
IF lv_confirm EQ abap_true.
ELSE.
c_error = 'X'.
EXIT.
ENDIF.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form set_light:设置提示灯
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& <-- <LS_OUTPUT>
*&---------------------------------------------------------------------*
FORM set_light CHANGING ch_output TYPE ts_output.
IF ch_output-type EQ 'E'. "失败状态
ch_output-icon = 'ICON_RED_LIGHT'.
ELSEIF ch_output-type EQ 'S'. "成功状态
ch_output-icon = 'ICON_GREEN_LIGHT'.
ELSEIF ch_output-type EQ 'N'. "空状态
ch_output-icon = 'ICON_YELLOW_LIGHT'.
ENDIF.
CALL FUNCTION 'ICON_CREATE'
EXPORTING
name = ch_output-icon
IMPORTING
result = ch_output-icon
EXCEPTIONS
icon_not_found = 1
outputfield_too_short = 2
OTHERS = 3.
IF sy-subrc <> 0.
ENDIF.
ENDFORM.
使用的结构
双击结构名称,结构的字段如下:
文章来源:https://www.toymoban.com/news/detail-834032.html
上传下载模板
文章来源地址https://www.toymoban.com/news/detail-834032.html
到了这里,关于ABAP 导入Excel表示例程序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!