ALV에서 Excel을 호출하여 앞의 0 문제를 해결합니다.

11221 단어 Excel
ABAP 카테고리에 게시된 Bob, 태그: ALV, Excel
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 설정
아래와 같이 설정하면 전도 0의 문제를 표시할 수 있습니다
when 'VBELN'. 
            <fcat>-NO_CONVEXT = 'X'.

4. 문제 해결 효과

좋은 웹페이지 즐겨찾기