ALV中调用Excel, 丢掉前面的0问题解决
程序员文章站
2022-03-11 12:44:40
...
Bob 发表于 ABAP 分类,标签: ALV, Excel
在sdn中看到有人问这个问题, 于是做个简单的例子来说明如何避免, 或者说解决这个问题.
1. 写个小程序, 用alv来显示表vbak的内容.
2. 问题现象重现
执行上面的程序,效果如下:
3. fieldcat设定
[color=red][size=medium]设置如下就可以显示前导零的问题[/size][/color]
4. 问题解决效果
在sdn中看到有人问这个问题, 于是做个简单的例子来说明如何避免, 或者说解决这个问题.
1. 写个小程序, 用alv来显示表vbak的内容.
*&---------------------------------------------------------------------*
*& Report ZALV_INPLACE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zalv_inplace.
*&---------------------------------------------------------------------*
* TABLES
*&---------------------------------------------------------------------*
TABLES:vbak.
*&---------------------------------------------------------------------*
*-->TYPE DEFINITION
*&---------------------------------------------------------------------*
TYPES: BEGIN OF ty_tab.
INCLUDE TYPE vbak.
TYPES:END OF ty_tab.
*&---------------------------------------------------------------------*
* SELECTION-SCREEN
*&---------------------------------------------------------------------*
SELECTION-SCREEN: BEGIN OF BLOCK b01 WITH FRAME TITLE text-b02.
SELECT-OPTIONS s_vbeln for vbak-vbeln.
SELECTION-SCREEN: END OF BLOCK b01.
*&---------------------------------------------------------------------*
*-->CLASS DEFINITION DEFERRED
*&---------------------------------------------------------------------*
CLASS lcl_layout DEFINITION DEFERRED.
CLASS lcl_fieldcat DEFINITION DEFERRED.
CLASS lcl_data DEFINITION DEFERRED.
CLASS lcl_event DEFINITION DEFERRED.
*&---------------------------------------------------------------------*
*--> Global data definitions for ALV
*&---------------------------------------------------------------------*
*--- ALV Grid instance reference
DATA ref_alv TYPE REF TO cl_gui_alv_grid .
*--- Custom container instance reference
DATA ref_con TYPE REF TO cl_gui_custom_container .
*--Data for screen command
DATA:
save_ok LIKE sy-ucomm,
ok_code LIKE sy-ucomm.
DATA: var TYPE disvariant.
*&---------------------------------------------------------------------*
*-->CLASS DATA DEFINITION
*&---------------------------------------------------------------------*
DATA ref_layout TYPE REF TO lcl_layout.
DATA ref_fieldcat TYPE REF TO lcl_fieldcat.
DATA ref_data TYPE REF TO lcl_data.
DATA ref_event TYPE REF TO lcl_event.
DATA: itab TYPE TABLE OF vbak.
*&---------------------------------------------------------------------*
*-->CLASS DEFINITION
*&---------------------------------------------------------------------*
CLASS lcl_layout DEFINITION.
PUBLIC SECTION.
DATA: s_layout TYPE lvc_s_layo .
METHODS: prepare_layout.
ENDCLASS. "lcl_layout DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_fieldcat DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_fieldcat DEFINITION.
PUBLIC SECTION.
DATA: it_fieldcat TYPE lvc_t_fcat.
METHODS: prepare_fieldcat.
PRIVATE SECTION.
DATA: s_fieldcat TYPE lvc_s_fcat.
ENDCLASS. "lcl_fieldcat DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_data DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_data DEFINITION.
PUBLIC SECTION.
DATA:itab TYPE TABLE OF ty_tab.
data:itab_del type TABLE OF ty_tab.
TYPES: ttab TYPE TABLE OF ty_tab.
METHODS: prepare_data.
* METHODS: update_color CHANGING itab TYPE ttab.
PRIVATE SECTION.
DATA: rs_data TYPE REF TO ty_tab.
ENDCLASS. "lcl_data DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_event DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event DEFINITION.
PUBLIC SECTION.
METHODS: handle_before_user_command FOR EVENT before_user_command
OF cl_gui_alv_grid IMPORTING e_ucomm.
METHODS:handle_user_command
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm sender.
ENDCLASS. "lcl_event DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_event IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event IMPLEMENTATION.
METHOD handle_before_user_command.
CASE e_ucomm.
WHEN '&VEXCEL'.
BREAK-POINT.
ENDCASE.
ENDMETHOD. "handle_data_changed_finished
METHOD handle_user_command.
ENDMETHOD. "handle_data_changed_finished
ENDCLASS. "lcl_event IMPLEMENTATION
*&---------------------------------------------------------------------*
*-->CLASS IMPLEMENTATOIN
*&---------------------------------------------------------------------*
CLASS lcl_layout IMPLEMENTATION.
METHOD prepare_layout.
s_layout-smalltitle ='X' .
s_layout-grid_title ='SIH Project Development List'(099).
s_layout-sel_mode ='A' .
ENDMETHOD. "prepare_layout
ENDCLASS. "lcl_layout IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_fieldcat IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_fieldcat IMPLEMENTATION.
METHOD prepare_fieldcat.
FIELD-SYMBOLS: <fcat> TYPE lvc_s_fcat.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'VBAK'
CHANGING
ct_fieldcat = it_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
loop at it_fieldcat ASSIGNING <fcat>.
case <fcat>-fieldname.
when 'VBELN'.
* <fcat>-
endcase.
endloop.
ENDMETHOD. "prepare_fieldcat
ENDCLASS. "lcl_fieldcat IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_data IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_data IMPLEMENTATION.
METHOD prepare_data.
*--->Get data
SELECT * FROM VBAK INTO CORRESPONDING FIELDS OF TABLE itab
WHERE VBELN IN S_VBELN.
ENDMETHOD. "prepare_data
"update_color
ENDCLASS. "lcl_data IMPLEMENTATION
START-OF-SELECTION.
* Call alv grid screen
CALL SCREEN 9000.
*&---------------------------------------------------------------------*
*& Module INIT_SCREEN OUTPUT
*&---------------------------------------------------------------------*
MODULE init_screen OUTPUT.
SET PF-STATUS 'STAT'.
PERFORM frm_display_alv.
ENDMODULE. " INIT_SCREEN OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND INPUT
*&---------------------------------------------------------------------*
MODULE user_command INPUT.
data: la type c.
save_ok = ok_code.
CASE save_ok.
WHEN 'BACK' OR 'EXIT' OR 'CANC'.
LEAVE to SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
*& Form frm_display_alv
*&---------------------------------------------------------------------*
FORM frm_display_alv .
* Local data definition
DATA: var TYPE disvariant. "Variant
IF ref_alv IS INITIAL.
*-->Create customer container
CREATE OBJECT ref_con
EXPORTING
container_name = 'ALV_CON'.
*-->Create alv grid
CREATE OBJECT ref_alv
EXPORTING
i_parent = ref_con.
*-->Create fieldcatlog for alv
CREATE OBJECT ref_fieldcat.
CALL METHOD ref_fieldcat->prepare_fieldcat.
*-->Set layout property
CREATE OBJECT ref_layout.
CALL METHOD ref_layout->prepare_layout.
*-->Get data.
CREATE OBJECT ref_data.
CALL METHOD ref_data->prepare_data.
*-->Register event
CREATE OBJECT ref_event.
SET HANDLER ref_event->handle_before_user_command FOR ref_alv.
SET HANDLER ref_event->handle_user_command FOR ref_alv.
* Register edit events
CALL METHOD ref_alv->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter.
*-->Display data
var-report = sy-repid.
CALL METHOD ref_alv->set_table_for_first_display
EXPORTING
is_variant = var
i_save = 'A'
i_default = 'X'
is_layout = ref_layout->s_layout
CHANGING
it_outtab = ref_data->itab
it_fieldcatalog = ref_fieldcat->it_fieldcat
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
ELSE.
*-->Refresh display
CALL METHOD ref_alv->refresh_table_display.
ENDIF.
ENDFORM. " frm_display_alv
2. 问题现象重现
执行上面的程序,效果如下:
3. fieldcat设定
[color=red][size=medium]设置如下就可以显示前导零的问题[/size][/color]
when 'VBELN'.
<fcat>-NO_CONVEXT = 'X'.
4. 问题解决效果
上一篇: 函数文档——python学习之路
下一篇: HTML5网络拓扑图性能优化的图文详解