ALV中调用Excel inplace时没能传递数据实例解决

  在很多用到ALV的项目开发中,会有调用Excel inplace功能直接在excel中查看数据(sy-ucomm = &VEXCEL), 可是当我们点该按钮是, excel是成功出现了,可是alv grid的数据却没有写到excel中.

  下面通过一则简单的实例程序,来介绍问题的解决:

*&-------------------- -------------------------------------------------*
*& Report  ZLC_ALV_EXCEL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZLC_ALV_EXCEL.
TABLES MARC.
TYPE-POOLS:SLIS.
DATA: BEGIN OF IT_MARC OCCURS 0,
      MATNR LIKE MARC-MATNR,
      PSTAT LIKE MARC-PSTAT,

      LVORM LIKE MARC-LVORM,
      BWTTY LIKE MARC-BWTTY,
      XCHAR LIKE MARC-XCHAR,
      MMSTA LIKE MARC-MMSTA,
      END   OF  IT_MARC.
DATA:  IT_FIELDCAT   TYPE  SLIS_T_FIELDCAT_ALV,
       IT_EVENTS     TYPE  SLIS_T_EVENT.
SELECT MATNR PSTAT LVORM BWTTY XCHAR MMSTA
INTO TABLE IT_MARC
FROM MARC.

START-OF-SELECTION.
  PERFORM  FRM_FIELDCAT_SET.
  PERFORM  FRM_ENVENT_SET.
  PERFORM  FRM_ALV_DISPLAY.
*&---------------------------------------------------------------------*
*&      Form  FRM_FIELDCAT_SET
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_FIELDCAT_SET .
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      I_PROGRAM_NAME         = SY-REPID
      i_structure_name       = 'ZLC_MARC'
    CHANGING
      CT_FIELDCAT            = IT_FIELDCAT
    EXCEPTIONS
      INCONSISTENT_INTERFACE = 1
      PROGRAM_ERROR          = 2
      OTHERS                 = 3.
ENDFORM.                    " FRM_FIELDCAT_SET
*&---------------------------------------------------------------------*
*&      Form  FRM_ENVENT_SET
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_ENVENT_SET .
 DATA PW_EVENT TYPE SLIS_ALV_EVENT.
  CLEAR PW_EVENT.
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    EXPORTING
      I_LIST_TYPE     = 1
    IMPORTING
      ET_EVENTS       = IT_EVENTS
    EXCEPTIONS
      LIST_TYPE_WRONG = 1
      OTHERS          = 2.
  PW_EVENT-FORM = 'FRM_USER_COMMAND'.
  MODIFY IT_EVENTS FROM PW_EVENT TRANSPORTING FORM
                  WHERE NAME = SLIS_EV_USER_COMMAND.
  PW_EVENT-FORM = 'FRM_SET_PFSTAT'.
  MODIFY IT_EVENTS FROM PW_EVENT TRANSPORTING FORM
                   WHERE NAME = SLIS_EV_PF_STATUS_SET.
ENDFORM.                    " FRM_ENVENT_SET
*&---------------------------------------------------------------------*
*&      FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_USER_COMMAND USING R_UCOMM TYPE SY-UCOMM           "#EC CALLED
                            R_SELFIELD TYPE SLIS_SELFIELD.
*  IF W_F2CODE = CNS_WCLI.
*    READ TABLE   IT_DATA
*    INTO         IW_DATA
*    INDEX        R_SELFIELD-TABINDEX
*    TRANSPORTING KUNNR .
*    EXPORT  W_KUNNR FROM IW_DATA-KUNNR TO MEMORY ID CNS_KUNNR.
*    LEAVE PROGRAM.
*  ENDIF.
ENDFORM.                    "FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*&      Form  FRM_ALV_DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_ALV_DISPLAY .
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM = SY-REPID
      IT_FIELDCAT        = IT_FIELDCAT
      IT_EVENTS          = IT_EVENTS

      I_DEFAULT          = 'X'

    TABLES
      T_OUTTAB           = IT_MARC
    EXCEPTIONS
      PROGRAM_ERROR      = 1
      OTHERS             = 2.

ENDFORM.                    " FRM_ALV_DISPLAY
*&---------------------------------------------------------------------*
*&      Form  FRM_SET_PFSTAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*

FORM FRM_SET_PFSTAT USING EXTAB TYPE SLIS_T_EXTAB.          "#EC CALLED
  SET PF-STATUS  'PF_002'.
ENDFORM.                    " FRM_SET_PFSTAT

 

此实例得到结果:

       

通过点击 导出EXCEL,一开始是没有数据。解决办法:在菜单栏 工具---》宏----》安全性---》可靠发行商下勾选‘信任对于visual Basic项目  ’的访问。

 

大功告成!

 

你可能感兴趣的:(SAP)