ALV Grid Display ,Top-Of-Page, Variant, DownLoad...
程序说明: User Function REUSE_ALV_GRID_DISPLAY Show Screen data.
1. 有TO-OF-PAGE , 有Title 标题,有Logo
2. 有variant ,可根据ALV Layout Variant 自定义download 文件,跟显示的alv数据一样。
3. WS_Dowload
4. Open Dataset Download
*----------------------------------------------------------------------
* Program ID/Name: ZW_ALV_SIMPLE_01 Date Written: 201XXXXXX
* Author's Name: Lilo.Zhu Last Update:
* Program Title:
* Project Name: XXXX
* Version: 1.0
*----------------------------------------------------------------------
* Description: (A Simple ALV Program Introduction ALV Coding Part)
*----------------------------------------------------------------------
* Change History
*----------------------------------------------------------------------
* Date | Programmer | Corr. # | Description
* 201XXXXXX | Lilo.Zhu | | New Create
* | | |
* | | |
*-----------------------------------------------------------------------
REPORT ZW_ALV_SIMPLE_01.
*------TYPE-POOLS------
TYPE-POOLS: SLIS.
*------TABLES------
TABLES: EKKO. "Purchase Document
*------ALV COMPLEMENT------
*->LAYOUT
DATA: S_LAYOUT TYPE SLIS_LAYOUT_ALV.
*->FIELD
DATA: IT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
S_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
*->EVENT
DATA: IT_EVENT TYPE SLIS_T_EVENT,
S_EVENT TYPE SLIS_ALV_EVENT.
*->TOP OF PAGE TITLE INFORMATION
DATA: IT_LIST_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER.
*->VARINAT
DATA: G_EXIT(1) TYPE C,
G_SAVE(1) TYPE C VALUE 'A',
GX_VARIANT LIKE DISVARIANT,
G_VARIANT LIKE DISVARIANT.
*------FOR DISPLAY VARIANT USAGE ------
DATA: BEGIN OF GT_ALV_FC OCCURS 100,
FLD_NAME(30) TYPE C,
COL_POS TYPE I,
END OF GT_ALV_FC.
DATA: G_REPID LIKE SY-REPID.
DATA: I_FCOL TYPE SY-TITLE OCCURS 0 WITH HEADER LINE, "FIELD NAME
G_SVFILE LIKE RLGRAP-FILENAME, "Server Paht Filename
G_FILENAME LIKE RLGRAP-FILENAME. "physical filename for OPEN DS
*------DEFINE INNER TABLE------
TYPES: BEGIN OF TP_TAB,
EBELN LIKE EKKO-EBELN,
BUKRS LIKE EKKO-BUKRS,
END OF TP_TAB.
DATA: IT_TAB TYPE TABLE OF TP_TAB WITH HEADER LINE.
SELECTION-SCREEN BEGIN OF BLOCK B001 WITH FRAME TITLE TEXT-001.
PARAMETER: P_VAR LIKE DISVARIANT-VARIANT.
SELECTION-SCREEN END OF BLOCK B001.
SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-002.
PARAMETERS: P_ALV RADIOBUTTON GROUP R1 DEFAULT 'X' USER-COMMAND C1.
PARAMETERS: RB_WSDL RADIOBUTTON GROUP R1 .
PARAMETERS: P_WSFILE LIKE RLGRAP-FILENAME
DEFAULT 'C:/DATA/SO.TXT'.
PARAMETERS: RB_OPEN RADIOBUTTON GROUP R1.
PARAMETERS: P_SVPATH LIKE RLGRAP-FILENAME.
PARAMETERS: P_SVFILE LIKE RLGRAP-FILENAME.
SELECTION-SCREEN END OF BLOCK B2.
*------INITIALIZATION------
INITIALIZATION.
G_REPID = SY-REPID.
G_VARIANT-USERNAME = SY-UNAME.
PERFORM FRM_ALV_EVENT.
PERFORM FRM_VARIANT_INIT.
GX_VARIANT = G_VARIANT.
CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
EXPORTING
I_SAVE = G_SAVE
CHANGING
CS_VARIANT = GX_VARIANT
EXCEPTIONS
NOT_FOUND = 2.
IF SY-SUBRC = 0.
P_VAR = GX_VARIANT-VARIANT.
ENDIF.
*------AT SELECTION-SCREEN------
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_VAR.
CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
EXPORTING
IS_VARIANT = G_VARIANT
I_SAVE = G_SAVE
IMPORTING
E_EXIT = G_EXIT
ES_VARIANT = GX_VARIANT
EXCEPTIONS
NOT_FOUND = 2.
IF SY-SUBRC = 2.
MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
IF G_EXIT = SPACE.
P_VAR = GX_VARIANT-VARIANT.
ENDIF.
ENDIF.
At Selection-Screen On Value-Request For P_WSFILE.
Call Function 'KD_GET_FILENAME_ON_F4'
Exporting
Mask = '*.TXT'
Static = 'X'
Changing
File_Name = P_WSFILE.
*------START-OF-SELECTIN------
START-OF-SELECTION.
PERFORM FRM_GET_DATA.
PERFORM FRM_ALV_VAR_FIELDCAT. "VARINAT FIELDCAT
PERFORM FRM_VARIANT.
PERFORM FRM_ALV_LAYOUT.
PERFORM FRM_ALV_FIELDCAT.
IF P_ALV = 'X'.
* PERFORM FRM_GET_DATA.
* PERFORM FRM_ALV_VAR_FIELDCAT. "VARINAT FIELDCAT
PERFORM FRM_ALV_SHOW.
ELSEIF RB_WSDL = 'X'.
G_FILENAME = P_WSFILE.
PERFORM FORMATTING_FILE.
PERFORM WS_DOWNLOAD.
ELSEIF RB_OPEN = 'X'.
DATA: L_SUBRC LIKE SY-SUBRC.
PERFORM EXPORT_FILE_TO_SERVER CHANGING L_SUBRC.
IF L_SUBRC NE 0.
MESSAGE E999(b1) WITH 'Error in exporting file'.
ENDIF.
ENDIF.
*&---------------------------------------------------------------------*
*& Form FRM_ALV_LAYOUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_ALV_LAYOUT .
S_LAYOUT-ZEBRA = 'X'.
ENDFORM. " FRM_ALV_LAYOUT
*&---------------------------------------------------------------------*
*& Form FRM_ALV_FIELDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_ALV_FIELDCAT .
*---Maintained Use Micro Define FieldCatlog---
data: LS_FIELDCAT TYPE slis_fieldcat_alv. "ALV Data Field Catalog
* Macro: _APPEND_FIELDCAT
* &1 - Field Name
* &2 - Reference Table
* &3 - Reference Table Field (when Field Name is different)
* &4 - Short Field Label
* &5 - Medium Field Label
* &6 - Long Field Label
* &7 - Text Field Name
* &8 - Reference Field with Quantity Unit
* &9 - Hotspot
DEFINE F1.
clear: LS_FIELDCAT. "LS_FIELDCAT.
LS_FIELDCAT-FIELDNAME = &1.
LS_FIELDCAT-REF_TABNAME = &2.
LS_FIELDCAT-REF_FIELDNAME = &3.
LS_FIELDCAT-SELTEXT_S = &4.
LS_FIELDCAT-SELTEXT_M = &5.
LS_FIELDCAT-SELTEXT_L = &6.
* LS_FIELDCAT-TXT_FIELD = &7.
* LS_FIELDCAT-QFIELDNAME = &8.
* LS_FIELDCAT-HOTSPOT = &9.
APPEND LS_FIELDCAT TO IT_FIELDCAT.
END-OF-DEFINITION.
F1 'EBELN' 'EKKO' 'EBELN' 'Pur.Doc#' 'Pur.Doc Numer' 'Purchase Document Numer'.
F1 'BUKRS' 'EKKO' 'BUKRS' 'Pur.XXX' 'Pur.XXX' 'Purchase XXX'.
ENDFORM. " FRM_ALV_FIELDCAT
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_GET_DATA .
SELECT EBELN BUKRS
FROM EKKO
INTO CORRESPONDING FIELDS OF TABLE IT_TAB.
ENDFORM. " FRM_GET_DATA
*&---------------------------------------------------------------------*
*& Form FRM_ALV_SHOW
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_ALV_SHOW .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = G_REPID "Trigger Event Must Define
IS_LAYOUT = S_LAYOUT
IT_FIELDCAT = IT_FIELDCAT[]
IT_EVENTS = IT_EVENT[]
I_CALLBACK_USER_COMMAND = 'PROCESS_DOUBLECLICK'
I_SAVE = 'X'
IS_VARIANT = G_VARIANT
TABLES
T_OUTTAB = IT_TAB.
ENDFORM. " FRM_ALV_SHOW
*&---------------------------------------------------------------------*
*& Form FRM_ALV_EVENT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_ALV_EVENT.
DATA: LS_EVENT TYPE SLIS_ALV_EVENT.
*
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
I_LIST_TYPE = 0
IMPORTING
ET_EVENTS = IT_EVENT.
** REGISTER FORM FOR TOP_OF_PAGE EVENT.
READ TABLE IT_EVENT WITH KEY NAME = SLIS_EV_TOP_OF_PAGE
INTO LS_EVENT.
IF SY-SUBRC = 0.
MOVE 'TOP_OF_PAGE' TO LS_EVENT-FORM.
APPEND LS_EVENT TO IT_EVENT.
ENDIF.
ENDFORM. " EVENTTAB_BUILD
*&---------------------------------------------------------------------*
*& Form TOP_OF_PAGE
*&---------------------------------------------------------------------*
* CALL Function Must After by List Header Define
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM TOP_OF_PAGE.
**GT_LIST_TOP_OF_PAGE
DATA: LS_LINE TYPE SLIS_LISTHEADER.
DATA: L_LINE(132).
* LIST HEADING LINE: TYPE H
CLEAR: IT_LIST_TOP_OF_PAGE.
CLEAR LS_LINE.
LS_LINE-TYP = 'H'.
LS_LINE-INFO = 'C_TITLE'.
APPEND LS_LINE TO IT_LIST_TOP_OF_PAGE.
* STATUS LINE: TYPE S
CLEAR LS_LINE.
LS_LINE-TYP = 'S'.
LS_LINE-KEY = TEXT-100.
LS_LINE-INFO = 'SO_HEADER-VBELN'.
APPEND LS_LINE TO IT_LIST_TOP_OF_PAGE.
CLEAR LS_LINE.
L_LINE = SY-REPID.
LS_LINE-TYP = 'S'.
LS_LINE-KEY = TEXT-110.
LS_LINE-INFO = SY-REPID.
APPEND LS_LINE TO IT_LIST_TOP_OF_PAGE.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
* i_logo = 'HTMLCNTL_TESTHTM2_SAPLOGO'
I_LOGO = 'ENJOYSAP_LOGO'
IT_LIST_COMMENTARY = IT_LIST_TOP_OF_PAGE.
ENDFORM.
*------PROCESS DOUBLE CLICK------
FORM PROCESS_DOUBLECLICK USING R_UCOMM LIKE SY-UCOMM RS_SELFIELD TYPE SLIS_SELFIELD.
IF R_UCOMM = '&IC1'. "CHECK Double Click
READ TABLE IT_TAB INDEX RS_SELFIELD-TABINDEX.
IF SY-SUBRC = 0.
SET PARAMETER ID 'BES' FIELD IT_TAB-EBELN.
* SET PARAMETER ID 'BUK' FIELD IT-EKKO-BUKRS.
CALL TRANSACTION 'ME2N' AND SKIP FIRST SCREEN.
ENDIF.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_VARIANT_INIT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_VARIANT_INIT .
CLEAR G_VARIANT.
G_VARIANT-REPORT = SY-REPID.
G_VARIANT-USERNAME = SY-UNAME.
ENDFORM. " FRM_VARIANT_INIT
*&---------------------------------------------------------------------*
*& Form FRM_VARIANT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_VARIANT .
IF NOT P_VAR IS INITIAL.
MOVE G_VARIANT TO GX_VARIANT.
MOVE P_VAR TO GX_VARIANT-VARIANT.
CALL FUNCTION 'REUSE_ALV_VARIANT_EXISTENCE'
EXPORTING
I_SAVE = G_SAVE
CHANGING
CS_VARIANT = GX_VARIANT.
G_VARIANT = GX_VARIANT.
ELSE.
PERFORM FRM_VARIANT_INIT.
ENDIF.
ENDFORM. " FRM_VARIANT
*&---------------------------------------------------------------------*
*& Form FRM_ALV_VAR_FIELDCAT
*&---------------------------------------------------------------------*
* Control DownLoad Use, Field Column
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_ALV_VAR_FIELDCAT .
DATA: L_LTDXKEY TYPE LTDXKEY,
L_LTDXDATA TYPE TABLE OF LTDXDATA WITH HEADER LINE,
LT_DISCARD TYPE TABLE OF LTDXDATA WITH HEADER LINE.
IF P_VAR IS INITIAL.
RETURN.
ENDIF.
SELECT SINGLE REPORT VARIANT FROM LTDX INTO
(L_LTDXKEY-REPORT,L_LTDXKEY-VARIANT)
WHERE RELID = 'LT'
AND REPORT = G_REPID
AND VARIANT = P_VAR.
L_LTDXKEY-TYPE = 'F'.
BREAK GARR_NG.
CALL FUNCTION 'LT_DBDATA_READ_FROM_LTDX'
EXPORTING
I_TOOL = 'LT'
IS_VARKEY = L_LTDXKEY
TABLES
T_DBFIELDCAT = L_LTDXDATA
* T_DBSORTINFO =
* T_DBFILTER =
* T_DBLAYOUT =
EXCEPTIONS
NOT_FOUND = 1
WRONG_RELID = 2
OTHERS = 3
.
LOOP AT L_LTDXDATA WHERE PARAM = 'NO_OUT' AND
VALUE = 'X'.
APPEND L_LTDXDATA TO LT_DISCARD.
ENDLOOP.
LOOP AT LT_DISCARD.
DELETE L_LTDXDATA WHERE KEY1 = LT_DISCARD-KEY1.
ENDLOOP.
LOOP AT L_LTDXDATA WHERE PARAM = 'COL_POS'.
CLEAR GT_ALV_FC.
GT_ALV_FC-FLD_NAME = L_LTDXDATA-KEY1.
GT_ALV_FC-COL_POS = L_LTDXDATA-VALUE.
APPEND GT_ALV_FC.
ENDLOOP.
SORT GT_ALV_FC BY COL_POS.
ENDFORM. " FRM_ALV_VAR_FIELDCAT
*&---------------------------------------------------------------------*
*& Form FORMATTING_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FORMATTING_FILE .
LOOP AT GT_ALV_FC.
CASE GT_ALV_FC-FLD_NAME.
WHEN 'EBELN'.
APPEND 'Purchase.Doc' TO I_FCOL.
WHEN 'BUKRS'.
APPEND 'Company' TO I_FCOL.
ENDCASE.
ENDLOOP.
ENDFORM. " FORMATTING_FILE
*&---------------------------------------------------------------------*
*& Form WS_DOWNLOAD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM WS_DOWNLOAD .
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
* BIN_FILESIZE = ' '
CODEPAGE = 'IBM'
FILENAME = G_FILENAME
FILETYPE = 'DAT'
* MODE = ' '
* WK1_N_FORMAT = ' '
* WK1_N_SIZE = ' '
* WK1_T_FORMAT = ' '
* WK1_T_SIZE = ' '
* COL_SELECT = ' '
* COL_SELECTMASK = ' '
* NO_AUTH_CHECK = ' '
* IMPORTING
* FILELENGTH =
TABLES
DATA_TAB = IT_TAB
FIELDNAMES = I_FCOL
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_WRITE_ERROR = 2
INVALID_FILESIZE = 3
INVALID_TYPE = 4
NO_BATCH = 5
UNKNOWN_ERROR = 6
INVALID_TABLE_WIDTH = 7
GUI_REFUSE_FILETRANSFER = 8
CUSTOMER_ERROR = 9
OTHERS = 10
.
IF SY-SUBRC <> 0.
SKIP 1.
WRITE:/1 'File download failed. File directory and name:'(T06),
G_FILENAME.
ELSE.
SKIP.
WRITE:/1 'File download successful. File directory and name:'(T07),
G_FILENAME.
ENDIF.
ENDFORM. " WS_DOWNLOAD
*&---------------------------------------------------------------------*
*& Form EXPORT_FILE_TO_SERVER
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_L_SUBRC text
*----------------------------------------------------------------------*
FORM EXPORT_FILE_TO_SERVER CHANGING P_SUBRC.
FIELD-SYMBOLS: <FS>.
DATA: L_FIELD(200) TYPE C,
L_STRING(3000) TYPE C.
P_SUBRC = 1.
* TRY.
* CONCATENATE P_SVPATH P_SVFILE INTO G_SVFILE.
* OPEN DATASET G_SVFILE FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
* IF SY-SUBRC = 0.
** Build header
* CONCATENATE 'PUR.DOC'
* 'Company'
* INTO L_STRING
**---EXCEL---
** SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
**---CSV---
*SEPARATED BY ','.
* TRANSFER L_STRING TO G_SVFILE.
*
*
* LOOP AT IT_TAB.
* CLEAR L_STRING.
* DO.
* ASSIGN COMPONENT SY-INDEX OF STRUCTURE IT_TAB TO <FS>.
* IF SY-SUBRC NE 0.
* EXIT.
* ENDIF.
* WRITE <FS> TO L_FIELD.
* REPLACE ALL OCCURRENCES OF '''' IN L_FIELD WITH ''''''.
* SHIFT L_FIELD LEFT DELETING LEADING SPACE.
* IF SY-INDEX = 1.
**---EXCEL---
** L_STRING = L_FIELD.
**---CSV---
**-e.g 1 -> "1" file in csv.
* L_STRING = L_FIELD.
* ELSE.
**---EXCEL---
** CONCATENATE L_STRING L_FIELD INTO L_STRING
** SEPARATED BY
** CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
**---CSV---
* CONCATENATE L_STRING L_FIELD INTO L_STRING
* SEPARATED BY ','.
*
* ENDIF.
* ENDDO.
* TRANSFER L_STRING TO G_SVFILE.
* ENDLOOP.
* CLOSE DATASET G_SVFILE.
*
* WRITE:/ 'Export is successful.'.
* WRITE:/ 'Temporary file created on server:', G_SVFILE.
* ELSE.
* MESSAGE E999(b1) WITH: 'Export is failed!'.
* ENDIF.
* CATCH CX_ROOT.
* WRITE:/ 'Temporary file writing error on server.'.
* MESSAGE E999(b1) WITH: 'Temporary file writing error on server!'.
* ENDTRY.
*------User Variant Download File------
DATA: TMP_FC TYPE SLIS_FIELDCAT_ALV,
LEN TYPE I,
WA TYPE TP_TAB.
*---Header Line---
IF GT_ALV_FC IS INITIAL.
LOOP AT IT_FIELDCAT INTO TMP_FC.
LEN = STRLEN( L_STRING ).
*---CSV---
CONCATENATE '"' TMP_FC-SELTEXT_L '"' ',' INTO L_STRING+LEN.
ENDLOOP.
ELSE.
LOOP AT GT_ALV_FC.
READ TABLE IT_FIELDCAT INTO TMP_FC
WITH KEY FIELDNAME = GT_ALV_FC-FLD_NAME.
LEN = STRLEN( L_STRING ).
CONCATENATE '"' TMP_FC-SELTEXT_L '"' ',' INTO L_STRING+LEN.
ENDLOOP.
ENDIF.
TRANSFER L_STRING TO G_SVFILE.
*---Body Content Line---
LOOP AT IT_TAB INTO WA.
CLEAR L_STRING.
IF GT_ALV_FC IS INITIAL.
LOOP AT IT_FIELDCAT INTO TMP_FC.
*->Writing out each field sequentially into a 'Line'
ASSIGN COMPONENT TMP_FC-FIELDNAME OF STRUCTURE WA TO <FS>.
IF SY-SUBRC <> 0.
*->Remove Extra Comma at the end of line.
lEN = STRLEN( L_STRING ) - 1.
WRITE '' TO L_STRING+LEN.
EXIT.
ENDIF.
LEN = STRLEN( L_STRING ).
WRITE <FS> TO L_FIELD.
CONDENSE L_FIELD.
CONCATENATE '"' L_FIELD '"' ',' INTO L_STRING+LEN.
ENDLOOP.
ELSE.
LOOP AT GT_ALV_FC.
ASSIGN COMPONENT GT_ALV_FC-FLD_NAME OF STRUCTURE WA TO <FS>.
IF SY-SUBRC <> 0.
LEN = STRLEN( L_STRING ) - 1.
WRITE '' TO L_STRING+LEN.
EXIT.
ENDIF.
LEN = STRLEN( L_STRING ).
WRITE <FS> TO L_FIELD.
CONDENSE L_FIELD.
CONCATENATE '"L_FIELD"' ',' INTO L_STRING+LEN.
ENDLOOP.
ENDIF.
TRANSFER L_STRING TO G_SVFILE.
ENDLOOP.
CLOSE DATASET G_SVFILE.
ENDFORM.