功能:
使用OLE基于输入表名动态下载模板,配合批导函数自动完成内外码转换,可以完成常见的任意自建表批导功能。
界面:
动态下载模板效果:
上传效果:
可以看到已经自动转为内码,并且超长字符串也没问题:
完整源码:
*&---------------------------------------------------------------------*
*& Report ZMAINTAIN_CUSTOM_TABLE
*&---------------------------------------------------------------------*
*& 描述:自建表通用批量维护程序
*& 作者:DeveloperMrMeng
*& 日期:2024.07.25
*&---------------------------------------------------------------------*
REPORT zmaintain_custom_table.
INCLUDE ole2incl.
*----------------------------------------------------------------------*
* 表声明
*----------------------------------------------------------------------*
TABLES:sscrfields.
*----------------------------------------------------------------------*
* 全局变量声明
*----------------------------------------------------------------------*
DATA:
gs_functxt TYPE smp_dyntxt,
gt_fieldcat TYPE lvc_t_fcat,
gs_fieldcat TYPE lvc_s_fcat.
*OLE使用相关参数定义
DATA:
myexcel TYPE ole2_object,
mysheet TYPE ole2_object,
mysheetname TYPE ole2_object,
mycell TYPE ole2_object,
mycell_begin TYPE ole2_object,
mycell_end TYPE ole2_object,
myworkbook TYPE ole2_object,
myrange TYPE ole2_object,
myrange_all TYPE ole2_object,
myborders TYPE ole2_object,
myinterior TYPE ole2_object.
FIELD-SYMBOLS:
<gt_table> TYPE STANDARD TABLE,
<gs_table> TYPE any.
*----------------------------------------------------------------------*
* 选择屏幕定义
*----------------------------------------------------------------------*
SELECTION-SCREEN FUNCTION KEY 1.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE gv_text1. "选择条件
PARAMETERS:
p_tab TYPE dd02l-tabname OBLIGATORY, "表名
p_file TYPE rlgrap-filename MEMORY ID path. "文件路径
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE gv_text2. "更新模式
PARAMETERS:
p_delt RADIOBUTTON GROUP gp1 MODIF ID add, "删除更新
p_over RADIOBUTTON GROUP gp1 DEFAULT 'X' MODIF ID add. "覆盖更新
SELECTION-SCREEN END OF BLOCK b2.
SELECTION-SCREEN:BEGIN OF LINE,
* 说明:
COMMENT 2(60) gv_text3,
END OF LINE.
SELECTION-SCREEN:BEGIN OF LINE,
* 上传模板由自建表动态生成,请勿变更!
COMMENT 3(60) gv_text4,
END OF LINE.
SELECTION-SCREEN:BEGIN OF LINE,
* 客户端及变更信息字段无需填写,系统将自动更新!
COMMENT 3(60) gv_text5,
END OF LINE.
*----------------------------------------------------------------------*
* 初始化事件
*----------------------------------------------------------------------*
INITIALIZATION.
* 屏幕初始化
PERFORM frm_initial_screen.
*----------------------------------------------------------------------*
* 屏幕输出前事件
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
* 更新屏幕
PERFORM frm_modify_screen.
*----------------------------------------------------------------------*
* 选择屏幕搜索帮助
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
* 获取文件路径
PERFORM frm_f4_for_filepath CHANGING p_file.
*----------------------------------------------------------------------*
* 选择屏幕事件
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
* 获取Fiedlcat
PERFORM frm_get_fieldcat.
* 选择屏幕按钮事件
PERFORM frm_process_screenbutton.
*----------------------------------------------------------------------*
* 选择屏幕开始
*----------------------------------------------------------------------*
START-OF-SELECTION.
* 检查输入信息
PERFORM frm_check_input.
* 构建动态内表
PERFORM frm_build_tab.
* 上传文件
PERFORM frm_upload_file.
* 特殊处理
PERFORM frm_special_process.
* ALV输出
PERFORM frm_display_alv.
*&---------------------------------------------------------------------*
*& Form FRM_INITIAL_SCREEN
*&---------------------------------------------------------------------*
* 屏幕初始化
*----------------------------------------------------------------------*
FORM frm_initial_screen .
CLEAR gs_functxt.
gs_functxt-icon_id = icon_export.
gs_functxt-icon_text = '下载批导模板'(006). "下载批导模板
sscrfields-functxt_01 = gs_functxt.
gv_text1 = '选择条件'(001).
gv_text2 = '更新模式'(002).
gv_text3 = '说明:'(003).
gv_text4 = '上传模板由自建表动态生成,请勿变更!'(004).
gv_text5 = '客户端及变更信息字段无需填写,系统将自动更新!'(005).
%_p_tab_%_app_%-text = '表名'(007).
%_p_file_%_app_%-text = '文件路径'(008).
%_p_delt_%_app_%-text = '删除更新'(009).
%_p_over_%_app_%-text = '覆盖更新'(010).
ENDFORM. " FRM_INITIAL_SCREEN
*&---------------------------------------------------------------------*
*& Form FRM_F4_FOR_FILEPATH
*&---------------------------------------------------------------------*
* 获取文件路径
*----------------------------------------------------------------------*
* <--CV_FILE 文件路径
*----------------------------------------------------------------------*
FORM frm_f4_for_filepath CHANGING cv_file TYPE rlgrap-filename.
CALL FUNCTION 'F4_FILENAME'
IMPORTING
file_name = cv_file.
ENDFORM. " FRM_F4_FOR_FILEPATH
*&---------------------------------------------------------------------*
*& Form FRM_PROCESS_SCREENBUTTON
*&---------------------------------------------------------------------*
* 选择屏幕按钮事件
*----------------------------------------------------------------------*
FORM frm_process_screenbutton .
CASE sscrfields-ucomm.
WHEN 'FC01'.
* 下载批导模板
PERFORM frm_download_temp.
WHEN OTHERS.
ENDCASE.
ENDFORM. " FRM_PROCESS_SCREENBUTTON
*&---------------------------------------------------------------------*
*& Form FRM_UPLOAD_FILE
*&---------------------------------------------------------------------*
* 上传文件
*----------------------------------------------------------------------*
FORM frm_upload_file .
* 上传Excel文件至内表
CALL FUNCTION 'ZFM_UPLOAD_XLSX_TO_TAB'
EXPORTING
i_file = p_file
i_begin_row = 3
TABLES
t_result = <gt_table>
EXCEPTIONS
has_error = 1
no_data = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " FRM_UPLOAD_FILE
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
* ALV输出
*----------------------------------------------------------------------*
FORM frm_display_alv .
DATA:
ls_layout TYPE lvc_s_layo,
lt_excluding TYPE slis_t_extab,
lt_event TYPE slis_t_event,
ls_event TYPE slis_alv_event,
lt_event_exit TYPE slis_t_event_exit,
ls_event_exit TYPE slis_event_exit,
ls_ddval TYPE lvc_s_drop,
ls_grid_setting TYPE lvc_s_glay.
FIELD-SYMBOLS:
<ls_fieldcat> TYPE lvc_s_fcat.
* 设置布局控制
ls_layout-zebra = 'X'. "颜色间隔
ls_layout-sel_mode = 'D'. "选择模式
ls_layout-cwidth_opt = 'X'. "列宽自适应
ls_layout-no_rowmark = 'X'. "禁用行选择
* 展示ALV
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'FRM_STATUS_SET'
i_callback_user_command = 'FRM_USER_COMMAND'
is_layout_lvc = ls_layout
it_fieldcat_lvc = gt_fieldcat
it_excluding = lt_excluding
i_grid_settings = ls_grid_setting
* IT_SORT_LVC =
i_save = 'A'
* IS_VARIANT =
it_events = lt_event
it_event_exit = lt_event_exit
TABLES
t_outtab = <gt_table>
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. " FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD_TEMP
*&---------------------------------------------------------------------*
* 下载批导模板
*----------------------------------------------------------------------*
FORM frm_download_temp.
DATA:
lv_full_path TYPE string.
* 获取模板保存路径
PERFORM frm_get_savepath CHANGING lv_full_path.
* 使用ole创建excel模板
PERFORM frm_crt_excel USING lv_full_path.
ENDFORM. " FRM_DOWNLOAD_TEMP
*&---------------------------------------------------------------------*
*& Form FRM_STATUS_SET
*&---------------------------------------------------------------------*
*& GUI状态栏设置
*&---------------------------------------------------------------------*
*& --> RT_EXTAB 排除按钮
*&---------------------------------------------------------------------*
FORM frm_status_set USING rt_extab TYPE slis_t_extab.
DATA ls_extab TYPE slis_extab.
SET PF-STATUS 'STATUS'. "EXCLUDING rt_extab.
ENDFORM. "FRM_STATUS_SET
*&---------------------------------------------------------------------*
*& Form FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*& 用户按钮事件响应
*&---------------------------------------------------------------------*
*& --> R_UCOMM 事件码
*& --> RS_SELFIELD 操作数据字段信息
*&---------------------------------------------------------------------*
FORM frm_user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
DATA:
lo_grid TYPE REF TO cl_gui_alv_grid,
ls_stable TYPE lvc_s_stbl,
lv_valid TYPE char1,
ls_layout TYPE lvc_s_layo,
lv_refresh TYPE char1.
ls_stable-row = 'X'.
ls_stable-col = 'X'.
* 获取当前屏幕实例对象
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lo_grid.
* 刷新变更数据至内表
CALL METHOD lo_grid->check_changed_data
IMPORTING
e_valid = lv_valid
CHANGING
c_refresh = lv_refresh.
CASE r_ucomm.
WHEN 'MAINTAIN'.
* 处理维护按钮
PERFORM frm_process_maintain.
WHEN OTHERS.
ENDCASE.
CHECK r_ucomm <> '&IC1'.
CALL METHOD lo_grid->get_frontend_layout
IMPORTING
es_layout = ls_layout.
ls_layout-cwidth_opt = 'X'.
* 设置列宽自适应
CALL METHOD lo_grid->set_frontend_layout
EXPORTING
is_layout = ls_layout.
* 刷新ALV
CALL METHOD lo_grid->refresh_table_display
EXPORTING
is_stable = ls_stable
EXCEPTIONS
finished = 1
OTHERS = 2.
ENDFORM. "FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*& Form FRM_PROCESS_MAINTAIN
*&---------------------------------------------------------------------*
* 处理维护按钮
*----------------------------------------------------------------------*
FORM frm_process_maintain .
DATA:
lv_error TYPE flag.
IF p_delt = 'X'.
SELECT COUNT(*) FROM (p_tab).
IF sy-subrc = 0.
DELETE FROM (p_tab).
IF sy-subrc <> 0.
lv_error = 'X'.
ENDIF.
ENDIF.
ENDIF.
MODIFY (p_tab) FROM TABLE <gt_table>.
IF sy-subrc <> 0.
lv_error = 'X'.
ENDIF.
IF lv_error <> 'X'.
COMMIT WORK.
MESSAGE '数据更新成功!'(016) TYPE 'S'.
ELSE.
MESSAGE '数据更新失败!'(017) TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
ENDFORM. " FRM_PROCESS_MAINTAIN
*&---------------------------------------------------------------------*
*& Form FRM_SPECIAL_PROCESS
*&---------------------------------------------------------------------*
* 特殊处理
*----------------------------------------------------------------------*
FORM frm_special_process .
FIELD-SYMBOLS:
<erdat> TYPE erdat,
<erzet> TYPE erzet,
<ernam> TYPE ernam,
<aedat> TYPE aedat,
<aezet> TYPE aezet,
<aenam> TYPE aenam.
* 自动更新变更履历字段
LOOP AT <gt_table> ASSIGNING <gs_table>.
ASSIGN COMPONENT 'ERDAT' OF STRUCTURE <gs_table> TO <erdat>.
ASSIGN COMPONENT 'ERZET' OF STRUCTURE <gs_table> TO <erzet>.
ASSIGN COMPONENT 'ERNAM' OF STRUCTURE <gs_table> TO <ernam>.
ASSIGN COMPONENT 'AEDAT' OF STRUCTURE <gs_table> TO <aedat>.
ASSIGN COMPONENT 'AEZET' OF STRUCTURE <gs_table> TO <aezet>.
ASSIGN COMPONENT 'AENAM' OF STRUCTURE <gs_table> TO <aenam>.
IF <erdat> IS ASSIGNED AND
<erzet> IS ASSIGNED AND
<ernam> IS ASSIGNED.
<erdat> = sy-datlo.
<erzet> = sy-timlo.
<ernam> = sy-uname.
IF <aedat> IS ASSIGNED AND
<aezet> IS ASSIGNED AND
<aenam> IS ASSIGNED.
<aedat> = ''.
<aezet> = ''.
<aenam> = ''.
ENDIF.
ELSE.
IF <aedat> IS ASSIGNED AND
<aezet> IS ASSIGNED AND
<aenam> IS ASSIGNED.
<aedat> = sy-datlo.
<aezet> = sy-timlo.
<aenam> = sy-uname.
ENDIF.
ENDIF.
ENDLOOP.
ENDFORM. " FRM_SPECIAL_PROCESS
*&---------------------------------------------------------------------*
*& Form FRM_BUILD_TAB
*&---------------------------------------------------------------------*
* 构建动态内表
*----------------------------------------------------------------------*
FORM frm_build_tab .
DATA:
lo_tab_ref TYPE REF TO data,
lo_line_ref TYPE REF TO data.
CREATE DATA lo_tab_ref TYPE STANDARD TABLE OF (p_tab).
CREATE DATA lo_line_ref TYPE (p_tab).
ASSIGN lo_tab_ref->* TO <gt_table>.
ASSIGN lo_line_ref->* TO <gs_table>.
ENDFORM. " FRM_BUILD_TAB
*&---------------------------------------------------------------------*
*& Form FRM_CHECK_INPUT
*&---------------------------------------------------------------------*
* 检查输入信息
*----------------------------------------------------------------------*
FORM frm_check_input .
DATA ls_dd02l TYPE dd02l.
IF p_tab+0(1) <> 'Z' AND p_tab+0(1) <> 'Y'.
MESSAGE '仅能处理客户命名空间(Z../Y..)的表!'(011) TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
IF p_file IS INITIAL.
MESSAGE '文件路径为空,请先选择上传文件!'(012) TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
SELECT SINGLE *
INTO ls_dd02l
FROM dd02l
WHERE tabname = p_tab
AND as4local = 'A'.
IF sy-subrc <> 0.
MESSAGE '数据库表不存在或未激活!'(013) TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " FRM_CHECK_INPUT
*&---------------------------------------------------------------------*
*& Form FRM_GET_SAVEPATH
*&---------------------------------------------------------------------*
*& 获取保存路径
*&---------------------------------------------------------------------*
*& <-- cv_path
*&---------------------------------------------------------------------*
FORM frm_get_savepath CHANGING cv_path TYPE string.
* 获取保存路径
CALL METHOD cl_gui_frontend_services=>directory_browse
CHANGING
selected_folder = cv_path
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
* 路径为空则退出
IF cv_path IS INITIAL.
MESSAGE '已取消下载!'(014) TYPE 'S'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. "FRM_GET_SAVEPATH
*&---------------------------------------------------------------------*
*& Form FRM_CRT_EXCEL
*&---------------------------------------------------------------------*
*& 使用ole创建excel模板
*&---------------------------------------------------------------------*
*& --> UV_PATH 保存路径
*&---------------------------------------------------------------------*
FORM frm_crt_excel USING uv_path TYPE string.
DATA:
lt_value TYPE STANDARD TABLE OF char2000,
ls_value LIKE LINE OF lt_value,
lv_total TYPE i.
uv_path = |{ uv_path }\\{ p_tab }.xlsx|.
* 创建excel对象
CREATE OBJECT myexcel 'excel.application'.
* 设置后台执行 0后台 1前台
SET PROPERTY OF myexcel 'visible' = 0.
* 创建工作簿对象
CALL METHOD OF
myexcel
'Workbooks' = myworkbook.
* 添加空白工作簿
CALL METHOD OF
myworkbook
'Add'.
FREE OBJECT myworkbook.
* 创建Sheet
CALL METHOD OF
myexcel
'Worksheets' = mysheet
EXPORTING
#1 = 1.
* 激活当前Sheet
CALL METHOD OF
mysheet
'Activate'.
LOOP AT gt_fieldcat INTO gs_fieldcat.
* 第一行:标题行
PERFORM frm_crt_value USING gs_fieldcat-scrtext_l CHANGING ls_value.
ENDLOOP.
ls_value = shift_right( val = ls_value sub = cl_abap_char_utilities=>horizontal_tab ).
APPEND ls_value TO lt_value.
CLEAR ls_value.
LOOP AT gt_fieldcat INTO gs_fieldcat.
* 第二行:字段名
PERFORM frm_crt_value USING gs_fieldcat-fieldname CHANGING ls_value.
ENDLOOP.
ls_value = shift_right( val = ls_value sub = cl_abap_char_utilities=>horizontal_tab ).
APPEND ls_value TO lt_value.
CLEAR ls_value.
lv_total = lines( gt_fieldcat ).
* 单元格填充
PERFORM frm_fill_cell USING 1 1 lt_value.
* 全选
CALL METHOD OF
mysheet
'Columns' = myrange_all.
* 自动适配列宽
CALL METHOD OF
myrange_all
'AutoFit'.
* 范围起始单元格
CALL METHOD OF
mysheet
'Cells' = mycell_begin
EXPORTING
#1 = 1
#2 = 1.
* 范围截止单元格
CALL METHOD OF
mysheet
'Cells' = mycell_end
EXPORTING
#1 = 2
#2 = lv_total.
* 获取范围对象
CALL METHOD OF
mysheet
'Range' = myrange
EXPORTING
#1 = mycell_begin
#2 = mycell_end.
* 选中范围
CALL METHOD OF
myrange
'Select'.
* 变更颜色
GET PROPERTY OF myrange 'Interior' = myinterior.
SET PROPERTY OF myinterior 'ColorIndex' = 15.
* 设置框线
GET PROPERTY OF myrange 'Borders' = myborders.
SET PROPERTY OF myborders 'Weight' = 2.
* 定位到第三行第一列
CALL METHOD OF
mysheet
'Cells' = mycell
EXPORTING
#1 = 3
#2 = 1.
* 选中单元格
CALL METHOD OF
mycell
'Select'.
* 切换至首页
CALL METHOD OF
myexcel
'Worksheets' = mysheet
EXPORTING
#1 = 1.
* 激活当前Sheet
CALL METHOD OF
mysheet
'Activate'.
FREE OBJECT mysheet.
* 获取活动的工作簿
GET PROPERTY OF myexcel 'ActiveWorkbook' = myworkbook.
* 另存为
CALL METHOD OF
myworkbook
'SaveAs'
EXPORTING
#1 = uv_path "保存路径
#2 = 51. "XlFileFormat:默认为*.xls 51为*.xlsx
* 保存并关闭
CALL METHOD OF
myworkbook
'Close'
EXPORTING
#1 = 'True'. "覆盖时弹窗确认
FREE OBJECT:
myworkbook.
* 退出EXCEL
CALL METHOD OF
myexcel
'Quit'.
* 释放对象
FREE OBJECT mycell_begin.
FREE OBJECT mycell_end.
FREE OBJECT mycell.
FREE OBJECT myrange.
FREE OBJECT myinterior.
FREE OBJECT myborders.
FREE OBJECT mysheet.
FREE OBJECT myexcel.
MESSAGE '下载成功!'(015) TYPE 'S'.
ENDFORM. "frm_crt_excel
*&---------------------------------------------------------------------*
*& Form frm_crt_value
*&---------------------------------------------------------------------*
*& 内容拼接
*&---------------------------------------------------------------------*
*& --> UV_FIELD
*& <-- CS_VALUE
*&---------------------------------------------------------------------*
FORM frm_crt_value USING VALUE(uv_field)
CHANGING cs_value TYPE char2000.
* TAB键分隔拼接
cs_value = cs_value && uv_field && cl_abap_char_utilities=>horizontal_tab.
ENDFORM. "frm_crt_value
*&---------------------------------------------------------------------*
*& Form FRM_FILL_CELL
*&---------------------------------------------------------------------*
*& 内容填充
*&---------------------------------------------------------------------*
*& --> UC_ROW
*& --> UV_COL
*& --> UT_VALUE
*&---------------------------------------------------------------------*
FORM frm_fill_cell USING VALUE(uc_row)
VALUE(uv_col)
VALUE(ut_value).
DATA lv_rc TYPE i.
* 将内容复制进粘贴板
CALL METHOD cl_gui_frontend_services=>clipboard_export
IMPORTING
data = ut_value
CHANGING
rc = lv_rc
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
* 选中单元格
CALL METHOD OF
mysheet
'Cells' = mycell
EXPORTING
#1 = uc_row
#2 = uv_col.
* 内容粘贴
CALL METHOD OF
mycell
'PasteSpecial'.
CALL METHOD OF
mysheet
'Cells' = mycell
EXPORTING
#1 = 3
#2 = 1.
CALL METHOD OF
mycell
'Select'.
FREE OBJECT mycell.
ENDFORM. "frm_fill_cell
*&---------------------------------------------------------------------*
*& Form FRM_GET_FIELDCAT
*&---------------------------------------------------------------------*
* 获取Fiedlcat
*----------------------------------------------------------------------*
FORM frm_get_fieldcat .
CLEAR gt_fieldcat.
* 设置字段控制
CALL FUNCTION 'ZFM_LVC_FIELDCATLOG_MERGE'
EXPORTING
i_structure_name = p_tab
CHANGING
ct_fieldcat = gt_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
ENDFORM. " FRM_GET_FIELDCAT
*&---------------------------------------------------------------------*
*& Form FRM_MODIFY_SCREEN
*&---------------------------------------------------------------------*
* 更新屏幕
*----------------------------------------------------------------------*
FORM frm_modify_screen .
LOOP AT SCREEN.
IF screen-group1 = 'ADD'.
* screen-active = 0.
screen-input = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ENDFORM. " FRM_MODIFY_SCREEN
引用函数:
上传XLSX使用如下函数:
处理Fieldcat使用如下函数:
FUNCTION zfm_lvc_fieldcatlog_merge.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" VALUE(I_STRUCTURE_NAME) TYPE DD02L-TABNAME OPTIONAL
*" VALUE(I_LANGUAGE) TYPE SY-LANGU OPTIONAL
*" CHANGING
*" VALUE(CT_FIELDCAT) TYPE LVC_T_FCAT OPTIONAL
*" EXCEPTIONS
*" INCONSISTENT_INTERFACE
*" PROGRAM_ERROR
*"----------------------------------------------------------------------
DATA:
lt_dd03t TYPE STANDARD TABLE OF dd03t,
ls_dd03t TYPE dd03t,
lt_dd03m TYPE STANDARD TABLE OF dd03m,
ls_dd03m TYPE dd03m,
lv_langu TYPE sy-langu.
FIELD-SYMBOLS:
<ls_fieldcat> TYPE lvc_s_fcat.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
* I_BUFFER_ACTIVE =
i_structure_name = i_structure_name
* I_CLIENT_NEVER_DISPLAY = 'X'
i_bypassing_buffer = 'X'
* I_INTERNAL_TABNAME =
CHANGING
ct_fieldcat = ct_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF i_language IS INITIAL.
lv_langu = sy-langu.
ELSE.
lv_langu = i_language.
ENDIF.
CASE sy-subrc.
WHEN 0.
* 获取内置类型描述
SELECT *
INTO TABLE lt_dd03t
FROM dd03t
WHERE tabname = i_structure_name
AND ddlanguage = lv_langu
AND as4local = 'A'.
LOOP AT lt_dd03t INTO ls_dd03t.
READ TABLE ct_fieldcat ASSIGNING <ls_fieldcat>
WITH KEY fieldname = ls_dd03t-fieldname.
IF sy-subrc = 0.
<ls_fieldcat>-reptext = ls_dd03t-ddtext.
<ls_fieldcat>-scrtext_s = ls_dd03t-ddtext.
<ls_fieldcat>-scrtext_m = ls_dd03t-ddtext.
<ls_fieldcat>-scrtext_l = ls_dd03t-ddtext.
ENDIF.
ENDLOOP.
* 如果指定语言,则覆盖描述为指定语言对应描述
IF i_language IS NOT INITIAL.
* 获取标准数据元素描述
SELECT *
INTO TABLE lt_dd03m
FROM dd03m
WHERE tabname = i_structure_name
AND ddlanguage = lv_langu
AND fldstat = 'A'
AND rollstat = 'A'
AND domstat = 'A'.
LOOP AT lt_dd03m INTO ls_dd03m.
READ TABLE ct_fieldcat ASSIGNING <ls_fieldcat>
WITH KEY fieldname = ls_dd03m-fieldname.
IF sy-subrc = 0.
<ls_fieldcat>-reptext = ls_dd03m-ddtext.
<ls_fieldcat>-scrtext_s = ls_dd03m-scrtext_s.
<ls_fieldcat>-scrtext_m = ls_dd03m-scrtext_m.
<ls_fieldcat>-scrtext_l = ls_dd03m-scrtext_l.
ENDIF.
ENDLOOP.
ENDIF.
WHEN 1.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
RAISING inconsistent_interface.
WHEN 2.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
RAISING program_error.
WHEN OTHERS.
ENDCASE.
ENDFUNCTION.
以上。