Skip to Content

Table of Contents

This document demonstrates how to write an ABAP program to download internal table data into excel file at user command using OLE function modules.

Step 1: Define Internal Table:

First of all define the internal table to store the output data.

DATA : BEGIN OF i_emp OCCURS 100,

          PERNR LIKE pa0002-PERNR,

          VORNA LIKE pa0002-VORNA,

          NACHN LIKE pa0002-NACHN,

         END OF i_emp.

Step 2: Populate Internal Table:

SELECT pernr vorna nachn FROM pa0002 INTO TABLE i_emp.

Step 3: Create the GUI Status

Then, you have to create a button on the screen for user to click on to download the excel file.

First, declare the status of the screen for user to click on.

SET PF-STATUS ‘EXCL’.

Then, Create the menu bar.

Go to T-code SE41 Menu Painter,

Enter your program name: ZEXCEL_DEMO

Select the subobject Status.

Enter the pf-status name ‘EXCL’

Click create button.

Create User Interface:

Here you can maintain the status EXCL of interface ZEXCEL_DEMO.

Expand the Application Toolbar and add new item as Download, press enter.

This will pop-up a new window titled Function Attributes.

screen-1_Layer 1.gif

Set the properties as shown in above picture:

Step 4: Trigger the User Command

User-command is triggered when user clicks on the download button on the screen.

Add the following code into AT USER-COMMAND.

AT USER-COMMAND.

  CASE sy-ucomm.

    WHEN ‘EXCEL’.

      PERFORM download.

      EXIT.

   ENDCASE.

Step 5: Perform Download

DEFINE append_fname.
    clear i_fanames.
    i_fanames = &1.
    append i_fanames.
    clear i_fanames.
  END-OF-DEFINITION.
" Field names
DATA :  BEGIN OF i_fanames OCCURS 0,
            fnames(195),
           END OF i_fanames.
DATA w_file LIKE rlgrap-filename.
" Add Field names
  append_fname 'Personnel No.'.
  append_fname 'First Name'.
  append_fname 'Last Name'.
 CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    CHANGING
      file_name     = w_file
    EXCEPTIONS
      mask_too_long = 1
      OTHERS        = 2.
  CALL FUNCTION 'MS_EXCEL_OLE_STANDARD_DAT'
    EXPORTING
      file_name                 = w_file
      data_sheet_name           = 'EMPLOYEE DATA'
      password_option           = 0
    TABLES
      data_tab                  = i_emp
      fieldnames                = i_fanames
    EXCEPTIONS
      file_not_exist            = 1
      filename_expected         = 2
      communication_error       = 3
      ole_object_method_error   = 4
      ole_object_property_error = 5
      invalid_filename          = 6
      invalid_pivot_fields      = 7
      download_problem          = 8
      OTHERS                    = 9.
  CASE sy-subrc.
    WHEN 1.
      MESSAGE e000(yv01) WITH 'File does not exist'.
    WHEN 2.
      MESSAGE e000(yv01) WITH 'Filename expected'.
    WHEN 3.
      MESSAGE e000(yv01) WITH 'Communication error'.
    WHEN 4.
      MESSAGE e000(yv01) WITH 'OLE object method error'.
    WHEN 5.
      MESSAGE e000(yv01) WITH 'OLE object property error'.
    WHEN 6.
      MESSAGE e000(yv01) WITH 'Invalid filename'.
    WHEN 7.
      MESSAGE e000(yv01) WITH 'Invalid pivot fields'.
    WHEN 8.
      MESSAGE e000(yv01) WITH 'Download problem'.
    WHEN 9.
      MESSAGE e000(yv01) WITH 'Other problem'.
  ENDCASE.
To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Jelena Perfiljeva

    Uhm… Thanks for sharing, but, unfortunately, it’s just not a very good example of development techniques (OCCURS 0, DEFINE when just one command would suffice, etc.). And why would you need GUI status or menu – this is not even referenced anywhere in the code.

    Did you search before posting the blog? I believe this has already been discussed in the forums many times and there are different ways, not just OLE.

    Please don’t let this discourage you, but I feel this just adds no value for the community – sorry!

    (0) 

Leave a Reply