Friday, June 29, 2012

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 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.




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


TYPESBEGIN 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(20TYPE C,
  WERKS(20TYPE C,
  LGORT(20TYPE C,
  PSTAT(20TYPE C,
  ERSDA(20TYPE C,
  END OF TY_OUT.



******** TYPE DECLARATIONS FOR EXCEL FILE COLUMN HEADINGS

TYPES : BEGIN OF TY_FIELD,
   FLD(20TYPE 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.









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.

Categories

ABAP (1) ABAP Interview Questions (112) ABAP Open SQL Statements (1) ABAP Syntax Rules (6) ABAP WORKBENCH (2) ABAP-Interview-Questions (52) ALE IDOC (6) ALE IDOC Interview Questions (6) ale-idoc (6) ALE-IDOC-Interview-Questions (19) ALV Interview Questions (5) ALV-Interview-Questions (22) BADI (2) BAPI (1) BAPI Interview Questions (1) BAPI-Interview-Questions (14) BDC (6) BDC Interview Questions (6) BDC-Interview-Questions (9) big data (2) big data interview questions (1) Classical Reports Interview Question (3) Classical-Reports-Interview-Questions (22) Conditional Statements (1) Cross Applications (3) Cross-Applications (14) Data Dictionary (22) Data Type Questins (1) Data types (1) Data-Dictionary (48) Data-Type-Questins (6) Dialog programming (5) Dialog Programming Interview Questions (4) Dialog-Programming (30) DOMAIN Interview Questions (1) Domain-Interview-Questions (8) Function Module (2) hadoop (2) hadoop interview questions (2) hdfs (1) IDoc Tutorials (6) Interactive Report Interview Questions (4) Interactive-Reports-Interview-Questions (22) Internal Tables (1) interview questions (1) Lock Object Interview Questions (1) Lock-Objects-Interview-Questions (10) Logical Database (1) Modularization Interview Questions (4) Modularization-Interview-Questions (25) Module Pool Programming (5) Module-Pool-Programming (39) modules in sap (1) Object Oriented ABAP (19) Object Oriented ABAP Interview Questions (15) object-oriented-abap (2) Object-Oriented-ABAP-Interview-Questions (34) OOABAP (9) Reports (14) Reports Interview Questions (9) Reports-Interview-Questions (19) RFC (1) RFC Interview Questions (1) RFC-Interview-Questions (14) RICEF (1) RICEF Objects (1) SAP (4) SAP ABAP (4) SAP ABAP Interview Questions (42) SAP ABAP Introduction (46) SAP ABAP Message Types (2) SAP BADI Interview Questions (2) SAP Basics (71) SAP Books (2) SAP Certification (1) SAP CONSULTANTS (5) SAP CRM (1) SAP ENHANCEMENTS (3) SAP EXITS (2) SAP EXITS ( SAP ENHANCEMENTS ) Interview Questions (1) SAP Free Books (1) SAP HR (2) SAP Lock Object (1) sap modules (2) SAP Open SQL Statements (1) SAP R/3 Architecture (4) SAP Search help (1) SAP Smartforms (1) SAP Smartforms Interview Questions (2) SAP Tables (5) SAP Tcodes (10) SAP Views (1) SAP Webdynpro ABAP (12) SAP Work Processors (2) SAP Workflow (3) SAP-BADI-Interview-Questions (11) SAP-Enhancements (39) SAP-Exits (39) SAP-Exits-Enhancements-Interview Questions (3) SAP-HANA (1) SAP-HANA-Interview-Questions (1) SAP-Smartforms-Interview-Questions (2) SAP-Workflow (3) Scripts (3) Scripts Interview Questions (2) Scripts-Interview-Questions (32) Search Help Interview Questions (1) Search-Help-Interview-Questions (9) Smartforms (1) Table Maintenance Generator (1) Table-Maintenance-Generator (4) Tables in SAP (2) Tables Interview Questions (3) Tables-Interview-Questions (3) Type Group Interview Questions (1) Type-Group-Interview-Questions (7) Variable Declaration (1) Views Interview Questions (1) Views-Interview-Questions (5) Webdynpro (12) what is big data (1)

Protected Blog

 
This blog is not affiliated to SAP AG |SAP is trademark of SAP AG |The information collected from various sources use information with your own risk.