ALV OO Grid Show , DownLoad, CSV ,Excel...
*----------------------------------------------------------------------
* Program ID/Name: ZW_ALV_GRID_STD Date Written: 20100630
* Author's Name: Lilo.Zhu Last Update:
* Program Title:
* Project Name: XXXX
* Version: 1.0
*----------------------------------------------------------------------
* Description: (New Program For a Standard OO ALV Grid Format, Dialog)
*----------------------------------------------------------------------
* Change History
*----------------------------------------------------------------------
* Date | Programmer | Corr. # | Description
* 20100630 | Lilo.Zhu | | New Create
* | | |
* | | |
*-----------------------------------------------------------------------
REPORT ZW_ALV_GRID_STD.
*------TABLES------
TABLES: EKKO.
*------TYPE DEFINITION------
TYPES: BEGIN OF TP_TAB.
INCLUDE STRUCTURE EKKO.
TYPES: ROWCOLOR(4) TYPE C.
TYPES: CELLCOLOR TYPE LVC_T_SCOL.
TYPES: EXPO TYPE C.
TYPES: END OF TP_TAB.
*------SELECTION-SCREEN------
SELECTION-SCREEN: BEGIN OF BLOCK B00 WITH FRAME TITLE TEXT-B00.
SELECT-OPTIONS: S_EBELN FOR EKKO-EBELN.
SELECT-OPTIONS: S_BUKRS FOR EKKO-BUKRS.
SELECTION-SCREEN: END OF BLOCK B00.
SELECTION-SCREEN: BEGIN OF BLOCK B01 WITH FRAME TITLE TEXT-B01.
PARAMETERS P_ALV RADIOBUTTON GROUP G1 DEFAULT 'X' USER-COMMAND C1.
PARAMETERS P_DWN RADIOBUTTON GROUP G1.
*->Server Path and File Name
PARAMETERS: P_SVPATH LIKE RLGRAP-FILENAME MODIF ID PTH MEMORY ID PAT,
P_SVFILE LIKE RLGRAP-FILENAME NO-DISPLAY.
SELECTION-SCREEN: END OF BLOCK B01.
DATA: G_SVFILE LIKE RLGRAP-FILENAME,
G_PATH TYPE CHAR600.
*------CLASS DEFINITION DEFERRED------
CLASS LCL_LAYOUT DEFINITION DEFERRED.
CLASS LCL_FIELDCAT DEFINITION DEFERRED.
CLASS LCL_DATA DEFINITION DEFERRED.
*------>GLOBAL DATA DEFINITIONS FOR ALV
*------ALV GRID INSTANCE REFERENCE------
DATA REF_ALV TYPE REF TO CL_GUI_ALV_GRID.
*------CUSTOMER 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.
*------>CLASS DEFINITION------
*------CLASS LCL_LAYOUT DEFINITION------
CLASS LCL_LAYOUT DEFINITION.
PUBLIC SECTION.
DATA: S_LAYOUT TYPE LVC_S_LAYO.
METHODS:PREPARE_LAYOUT.
ENDCLASS.
*------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.
*------CLASS LCL_DATA DEFINITION------
CLASS LCL_DATA DEFINITION.
PUBLIC SECTION.
DATA:IT_EKKO TYPE TABLE OF TP_TAB.
METHODS:PREPARE_DATA.
PRIVATE SECTION.
DATA:RS_DATA TYPE REF TO TP_TAB.
ENDCLASS.
*------>CLASS IMPLEMENTATION------
*------CLASS LCL_LAYOUT IMPLEMENTATION------
CLASS LCL_LAYOUT IMPLEMENTATION.
METHOD PREPARE_LAYOUT.
*->Fisrst Column Exception Flag
* S_LAYOUT-EXCP_FNAME = 'EXPO'.
S_LAYOUT-CTAB_FNAME = 'CELLCOLOR'.
S_LAYOUT-INFO_FNAME = 'ROWCOLOR'.
ENDMETHOD.
ENDCLASS.
*------CLASS LCL_FIELDCAT IMPLEMENTATION------
CLASS LCL_FIELDCAT IMPLEMENTATION.
METHOD PREPARE_FIELDCAT.
**---User Maintained FieldCat---
* DATA: LS_FCAT TYPE LVC_S_FCAT.
* LS_FCAT-FIELDNAME = 'EBELN'.
* LS_FCAT-INTTYPE = 'C'.
* LS_FCAT-OUTPUTLEN = '10'.
* LS_FCAT-COLTEXT = 'Pur.Doc'.
* LS_FCAT-SELTEXT = 'Purchase.Doc'.
* APPEND LS_FCAT TO IT_FIELDCAT.
**---Automatic Maintained FieldCat
* CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
* EXPORTING
* I_STRUCTURE_NAME = 'EKKO'
* CHANGING
* CT_FIELDCAT = IT_FIELDCAT
* EXCEPTIONS
* INCONSISTENT_INTERFACE = 1
* PROGRAM_ERROR = 2
* OTHERS = 3.
*
* IF SY-SUBRC <> 0.
**--->USER CHECK-POINT.
* ENDIF.
*
**---UP + Semiautomatic Maintained FieldCat---
* DATA: LS_FCAT TYPE LVC_S_FCAT.
* LOOP AT IT_FIELDCAT INTO LS_FCAT.
* CASE LS_FCAT-FIELDNAME.
* WHEN 'EBELN'.
**->Field Lenght
* LS_FCAT-OUTPUTLEN = '10'.
* LS_FCAT-COLTEXT = 'PUR.Doc'.
* MODIFY IT_FIELDCAT FROM LS_FCAT.
* ENDCASE.
* ENDLOOP.
*---Maintained Use Micro Define FieldCatlog---
data: LS_FIELDCAT TYPE LVC_S_FCAT. "ALV Data Field Catalog
"ls_fieldcata type slis_fieldcat_alv. "ALV Display 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_TABLE = &2.
LS_FIELDCAT-REF_FIELD = &3.
LS_FIELDCAT-SCRTEXT_S = &4.
LS_FIELDCAT-SCRTEXT_M = &5.
LS_FIELDCAT-SCRTEXT_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'.
ENDMETHOD.
ENDCLASS.
*------CLASS LCL_DATA IMPLEMENTATION------
CLASS LCL_DATA IMPLEMENTATION.
METHOD PREPARE_DATA.
*---GET DATA---
SELECT A~EBELN A~BUKRS
FROM EKKO AS A
INTO CORRESPONDING FIELDS OF TABLE IT_EKKO
WHERE A~EBELN IN S_EBELN
AND A~BUKRS IN S_BUKRS.
*---SET ROW COLOR---
READ TABLE IT_EKKO REFERENCE INTO RS_DATA INDEX 2.
RS_DATA->ROWCOLOR = 'C110'.
*---SET CELL COLOR---
DATA:RS_CELL TYPE LVC_S_SCOL.
READ TABLE IT_EKKO REFERENCE INTO RS_DATA INDEX 4.
RS_CELL-FNAME = 'BUKRS'.
RS_CELL-COLOR-COL = '6'.
RS_CELL-COLOR-INT = '1'.
APPEND RS_CELL TO RS_DATA->CELLCOLOR.
ENDMETHOD.
ENDCLASS.
*------>AT SELECTION SCREEN------
AT SELECTION-SCREEN OUTPUT.
PERFORM MODIFY_SCREEN.
*------>START-OF-SELECTION------
START-OF-SELECTION.
IF P_ALV ='X'.
*---CALL ALV GIRD SCREEN.
CALL SCREEN 9000.
ELSE.
PERFORM DOWNLOAD_TO_SERVER.
ENDIF.
*------FORM MODIFY_SCREEN------
FORM MODIFY_SCREEN.
LOOP AT SCREEN.
IF P_ALV ='X'.
IF SCREEN-GROUP1 ='PTH'.
SCREEN-ACTIVE = '0'.
SCREEN-INVISIBLE = '1'.
MODIFY SCREEN.
ENDIF.
ENDIF.
ENDLOOP.
ENDFORM.
*------>Module INIT_SCREEN OUTPUT------
MODULE INIT_SCREEN OUTPUT.
SET PF-STATUS 'STAT'.
PERFORM FRM_DISPLAY_ALV.
ENDMODULE.
*------>Module USER_COMMAND OUTPUT-----
MODULE USER_COMMAND INPUT.
SAVE_OK = OK_CODE.
CASE SAVE_OK.
WHEN 'BACK' OR 'EXIT' OR 'CANC'.
LEAVE PROGRAM.
ENDCASE.
ENDMODULE.
*------>Form FRM_DISPLAY_ALV------
FORM FRM_DISPLAY_ALV .
*->Local Data Definition
DATA: VAR TYPE DISVARIANT. "Variant
IF REF_ALV IS INITIAL.
*---Instance Customer Container ->CL_GUI_CUSTOMER_CONTAINER
CREATE OBJECT REF_CON
EXPORTING
CONTAINER_NAME ='CON_ALV'.
*---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.
*---Dispaly Data---
CLEAR VAR.
CALL METHOD REF_ALV->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_VARIANT = VAR
I_SAVE = SPACE
I_DEFAULT = 'X'
IS_LAYOUT = REF_LAYOUT->S_LAYOUT
CHANGING
IT_OUTTAB = REF_DATA->IT_EKKO
IT_FIELDCATALOG = REF_FIELDCAT->IT_FIELDCAT
EXCEPTIONS
INVALID_PARAMETER_COMBINATION = 1
PROGRAM_ERROR = 2
TOO_MANY_LINES = 3
OTHERS = 4.
ELSE.
CALL METHOD REF_ALV->REFRESH_TABLE_DISPLAY.
ENDIF.
ENDFORM.
*------>FORM DOWNLOAD_TO_SERVER------
FORM DOWNLOAD_TO_SERVER.
FIELD-SYMBOLS: <FS>.
DATA:L_FIELD(40) TYPE C.
DATA:L_STRING(450) TYPE C.
*---Get Data---
CREATE OBJECT REF_DATA.
CALL METHOD REF_DATA->PREPARE_DATA.
DATA: IT_TAB1 TYPE STANDARD TABLE OF TP_TAB WITH HEADER LINE.
IT_TAB1[] = REF_DATA->IT_EKKO.
TRY.
CONCATENATE 'Report_' SY-DATUM '_'
SY-UZEIT+0(2) SY-UZEIT+2(2) '.csv' INTO P_SVFILE.
DATA:BEGIN OF IT_OUT OCCURS 0,
PUR_NO(30), "PUR.DOC Number
END OF IT_OUT.
* DATA:IT_OUT TYPE STANDARD TABLE OF TP_OUT WITH HEADER LINE.
LOOP AT IT_TAB1.
* MOVE-CORRESPONDING IT_TAB1 TO IT_OUT.
IT_OUT-PUR_NO = IT_TAB1-EBELN.
APPEND IT_OUT.
CLEAR: IT_OUT.
ENDLOOP.
CONCATENATE P_SVPATH P_SVFILE INTO G_SVFILE.
OPEN DATASET G_SVFILE FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC = 0.
CLEAR L_STRING.
CONCATENATE 'PUR_NUMBER'
'BUKRS'
INTO L_STRING
SEPARATED BY ','.
TRANSFER L_STRING TO G_SVFILE.
LOOP AT IT_OUT.
CLEAR L_STRING.
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE IT_OUT 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.
ENDFORM.