Dear ABAPer's in this post i'm going to explain how to download SAP report (SE38) data into MS-Excel file using one class and function module.
The class is "CL_RSAN_UT_FILES=>F4" and
the function module is "MS_EXCEL_OLE_STANDARD_DAT".
It's very easy to down load data into excel file by using these class and function module. Refer this post for more details and source code. For more details click on read more.
Dear ABAPer's in this post i'm going to explain how to download SAP report (SE38) data into MS-Excel file using class "CL_RSAN_UT_FILES=>F4" and function module "MS_EXCEL_OLE_STANDARD_DAT". It's very easy to down load data into excel file by using these class and function module. Refer this post for more details and source code.
Try this code in your editor and debug to understand this code.
******************** Source Code Begins Here **********************************
REPORT ZREP_EXCEL NO STANDARD PAGE HEADING.
********* TYPE DECLARATIONS FOR MARD TABLE
TYPES: BEGIN OF TY_MARD,
MATNR TYPE MARD-MATNR,
WERKS TYPE MARD-WERKS,
LGORT TYPE MARD-LGORT,
PSTAT TYPE MARD-PSTAT,
ERSDA TYPE MARD-ERSDA,
END OF TY_MARD.
******** TYPE DECLARATIONS FOR OUTPUT TABLE
TYPES : BEGIN OF TY_OUT,
MATNR(20) TYPE C,
WERKS(20) TYPE C,
LGORT(20) TYPE C,
PSTAT(20) TYPE C,
ERSDA(20) TYPE C,
END OF TY_OUT.
******** TYPE DECLARATIONS FOR EXCEL FILE COLUMN HEADINGS
TYPES : BEGIN OF TY_FIELD,
FLD(20) TYPE C,
END OF TY_FIELD.
*******I_MARD, WA_MARD DECLARATIONS
DATA : I_MARD TYPE TABLE OF TY_MARD.
DATA : WA_MARD TYPE TY_MARD.
******* I_OUT TABLE DECLARATIONS
DATA : I_OUT TYPE TABLE OF TY_OUT.
DATA : WA_OUT LIKE LINE OF I_OUT.
******* I_FIELD WA_FIELD DECLARATIONS
DATA : I_FIELD TYPE TABLE OF TY_FIELD.
DATA : WA_FIELD TYPE TY_FIELD.
****** TABLE FOR SELECT OPTIONS
TABLES : MARD.
****** SELECT OPTIONS FOR MATNR
SELECT-OPTIONS SO_MATNR FOR MARD-MATNR.
****** PARAMETER FOR FILE SELECTION
PARAMETERS P_FILE TYPE STRING.
****** VARIABLE FOR STORING FILE NAME
DATA : V_FILE TYPE RLGRAP-FILENAME.
**** F4 FUNCTIONALITY FOR SELECTING FILE
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
******** CLASS FOR SELECTING EXCEL FILE NAME
CALL METHOD CL_RSAN_UT_FILES=>F4
EXPORTING
I_APPLSERV = SPACE
I_TITLE = 'PC FILE PATH.......'
I_GUI_EXTENSION = 'XLS'
I_GUI_EXT_FILTER = SPACE
CHANGING
C_FILE_NAME = P_FILE.
START-OF-SELECTION.
****** GET THE DATA FROM DATA BASE TABLE TO INTERNAL TABLE I_MARD.
SELECT MATNR WERKS LGORT PSTAT ERSDA FROM MARD INTO TABLE I_MARD
WHERE MATNR IN SO_MATNR.
****** MOVING DATA TO OUTPUT TABLE
LOOP AT I_MARD INTO WA_MARD.
WA_OUT-MATNR = WA_MARD-MATNR.
WA_OUT-WERKS = WA_MARD-WERKS.
WA_OUT-LGORT = WA_MARD-LGORT.
WA_OUT-PSTAT = WA_MARD-PSTAT.
WA_OUT-ERSDA = WA_MARD-ERSDA.
APPEND WA_OUT TO I_OUT.
ENDLOOP.
******* CREATING COLUMN HEADINGS FOR EXCEL FILES
WA_FIELD-FLD = 'MATERIAL NUM'.
APPEND WA_FIELD TO I_FIELD.
CLEAR WA_FIELD.
WA_FIELD-FLD = 'PLANT LOCATION'.
APPEND WA_FIELD TO I_FIELD.
CLEAR WA_FIELD.
WA_FIELD-FLD = 'STORAGE LOCATION'.
APPEND WA_FIELD TO I_FIELD.
CLEAR WA_FIELD.
WA_FIELD-FLD = 'MAINTANENCE STATUS'.
APPEND WA_FIELD TO I_FIELD.
CLEAR WA_FIELD.
WA_FIELD-FLD = 'CREATED DATE'.
APPEND WA_FIELD TO I_FIELD.
CLEAR WA_FIELD.
****** CALL FUNCTION MODULE TO DOWNLOAD REPORT DATA INTO EXCEL FILE
V_FILE = P_FILE.
CALL FUNCTION 'MS_EXCEL_OLE_STANDARD_DAT'
EXPORTING
FILE_NAME = V_FILE
PASSWORD = '1234'
PASSWORD_OPTION = 1
TABLES
DATA_TAB = I_OUT
FIELDNAMES = I_FIELD
EXCEPTIONS
FILE_NOT_EXIST = 1
FILENAME_EXPECTED = 2
COMMUNICATION_ERROR = 3
OLE_OBJECT_METHOD_ERROR = 4
OLE_OBJECT_PROPERTY_ERROR = 5
INVALID_PIVOT_FIELDS = 6
DOWNLOAD_PROBLEM = 7
OTHERS = 8.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSEIF SY-SUBRC = 0.
****** DISPLAYING SUCCESS MESSAGE AFTER SUCCESSFULL PEOCESSING
WRITE : / 'DATA SUCCESSFULLY DOWNLOADED TO EXCEL FILE'.
ENDIF.
-> Save Activate Execute the code.
The class is "CL_RSAN_UT_FILES=>F4" and
the function module is "MS_EXCEL_OLE_STANDARD_DAT".
It's very easy to down load data into excel file by using these class and function module. Refer this post for more details and source code. For more details click on read more.
SAP Report Data to Excel File Downloading Real Time Example
Dear ABAPer's in this post i'm going to explain how to download SAP report (SE38) data into MS-Excel file using class "CL_RSAN_UT_FILES=>F4" and function module "MS_EXCEL_OLE_STANDARD_DAT". It's very easy to down load data into excel file by using these class and function module. Refer this post for more details and source code.
Try this code in your editor and debug to understand this code.
******************** Source Code Begins Here **********************************
REPORT ZREP_EXCEL NO STANDARD PAGE HEADING.
********* TYPE DECLARATIONS FOR MARD TABLE
TYPES: BEGIN OF TY_MARD,
MATNR TYPE MARD-MATNR,
WERKS TYPE MARD-WERKS,
LGORT TYPE MARD-LGORT,
PSTAT TYPE MARD-PSTAT,
ERSDA TYPE MARD-ERSDA,
END OF TY_MARD.
******** TYPE DECLARATIONS FOR OUTPUT TABLE
TYPES : BEGIN OF TY_OUT,
MATNR(20) TYPE C,
WERKS(20) TYPE C,
LGORT(20) TYPE C,
PSTAT(20) TYPE C,
ERSDA(20) TYPE C,
END OF TY_OUT.
******** TYPE DECLARATIONS FOR EXCEL FILE COLUMN HEADINGS
TYPES : BEGIN OF TY_FIELD,
FLD(20) TYPE C,
END OF TY_FIELD.
*******I_MARD, WA_MARD DECLARATIONS
DATA : I_MARD TYPE TABLE OF TY_MARD.
DATA : WA_MARD TYPE TY_MARD.
******* I_OUT TABLE DECLARATIONS
DATA : I_OUT TYPE TABLE OF TY_OUT.
DATA : WA_OUT LIKE LINE OF I_OUT.
******* I_FIELD WA_FIELD DECLARATIONS
DATA : I_FIELD TYPE TABLE OF TY_FIELD.
DATA : WA_FIELD TYPE TY_FIELD.
****** TABLE FOR SELECT OPTIONS
TABLES : MARD.
****** SELECT OPTIONS FOR MATNR
SELECT-OPTIONS SO_MATNR FOR MARD-MATNR.
****** PARAMETER FOR FILE SELECTION
PARAMETERS P_FILE TYPE STRING.
****** VARIABLE FOR STORING FILE NAME
DATA : V_FILE TYPE RLGRAP-FILENAME.
**** F4 FUNCTIONALITY FOR SELECTING FILE
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
******** CLASS FOR SELECTING EXCEL FILE NAME
CALL METHOD CL_RSAN_UT_FILES=>F4
EXPORTING
I_APPLSERV = SPACE
I_TITLE = 'PC FILE PATH.......'
I_GUI_EXTENSION = 'XLS'
I_GUI_EXT_FILTER = SPACE
CHANGING
C_FILE_NAME = P_FILE.
START-OF-SELECTION.
****** GET THE DATA FROM DATA BASE TABLE TO INTERNAL TABLE I_MARD.
SELECT MATNR WERKS LGORT PSTAT ERSDA FROM MARD INTO TABLE I_MARD
WHERE MATNR IN SO_MATNR.
****** MOVING DATA TO OUTPUT TABLE
LOOP AT I_MARD INTO WA_MARD.
WA_OUT-MATNR = WA_MARD-MATNR.
WA_OUT-WERKS = WA_MARD-WERKS.
WA_OUT-LGORT = WA_MARD-LGORT.
WA_OUT-PSTAT = WA_MARD-PSTAT.
WA_OUT-ERSDA = WA_MARD-ERSDA.
APPEND WA_OUT TO I_OUT.
ENDLOOP.
******* CREATING COLUMN HEADINGS FOR EXCEL FILES
WA_FIELD-FLD = 'MATERIAL NUM'.
APPEND WA_FIELD TO I_FIELD.
CLEAR WA_FIELD.
WA_FIELD-FLD = 'PLANT LOCATION'.
APPEND WA_FIELD TO I_FIELD.
CLEAR WA_FIELD.
WA_FIELD-FLD = 'STORAGE LOCATION'.
APPEND WA_FIELD TO I_FIELD.
CLEAR WA_FIELD.
WA_FIELD-FLD = 'MAINTANENCE STATUS'.
APPEND WA_FIELD TO I_FIELD.
CLEAR WA_FIELD.
WA_FIELD-FLD = 'CREATED DATE'.
APPEND WA_FIELD TO I_FIELD.
CLEAR WA_FIELD.
****** CALL FUNCTION MODULE TO DOWNLOAD REPORT DATA INTO EXCEL FILE
V_FILE = P_FILE.
CALL FUNCTION 'MS_EXCEL_OLE_STANDARD_DAT'
EXPORTING
FILE_NAME = V_FILE
PASSWORD = '1234'
PASSWORD_OPTION = 1
TABLES
DATA_TAB = I_OUT
FIELDNAMES = I_FIELD
EXCEPTIONS
FILE_NOT_EXIST = 1
FILENAME_EXPECTED = 2
COMMUNICATION_ERROR = 3
OLE_OBJECT_METHOD_ERROR = 4
OLE_OBJECT_PROPERTY_ERROR = 5
INVALID_PIVOT_FIELDS = 6
DOWNLOAD_PROBLEM = 7
OTHERS = 8.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSEIF SY-SUBRC = 0.
****** DISPLAYING SUCCESS MESSAGE AFTER SUCCESSFULL PEOCESSING
WRITE : / 'DATA SUCCESSFULLY DOWNLOADED TO EXCEL FILE'.
ENDIF.
-> Save Activate Execute the code.
Previous Posts:
For More Real Time Interview Questions See Below Posts
Any Suggestions, Queries, regarding this post drop a comment.
"You found the information helpful and want to say thanks? Your donation is enough to inspire us to do more. Thanks a bunch!"
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.