This document demonstrates how to write an ABAP program to download internal table data into excel file at user command using OLE function modules.
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.
SELECT pernr vorna nachn FROM pa0002 INTO TABLE i_emp.
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.
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.
Set the properties as shown in above picture:
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
5 | |
3 | |
3 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 |