Screenshot # 1
Solution : Using File type as 'DBF' , We easily got Field value in MS.Excel leading with Zero's , Like Material Number from MARA Table .
‘DBF’ :
Data is downloaded in dBase format. Since in this format the data types of the individual columns are stored as well, you can often avoid import problems, for example, into Microsoft Excel, especially when interpreting numeric values.
Screenshot # 2
Data Declaration
TYPES: BEGIN OF TY_FINAL ,
MBRSH LIKE MARA-MBRSH,
MTART LIKE MARA-MTART,
MATNR LIKE MARA-MATNR,
MEINS LIKE MARA-MEINS,
MATKL LIKE MARA-MATKL,
END OF TY_FINAL.
DATA: I_FINAL TYPE STANDARD TABLE OF TY_FINAL INITIAL SIZE 0, " Final internal Table
WA_FINAL TYPE TY_FINAL. " Work Area
DATA : BEGIN OF INT_HEAD OCCURS 0,
FILED1(20) TYPE C, " Header Data
END OF INT_HEAD.
DATA: LO_GUI TYPE REF TO CL_GUI_FRONTEND_SERVICES,
LV_TITLE TYPE STRING,
LV_FOLDER TYPE STRING, "
LV_DIR TYPE STRING, " Select Folder Location
V_FILETYPE TYPE STRING, " File Type '.xls'
V_FILENAME TYPE STRING,
LV_FILENAME TYPE STRING.
Selection Screen
**SELECTION SCREEN DETAILS
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-002.
PARAMETER P_FILE LIKE RLGRAP-FILENAME . " Input File Name with Specific Path like 'C:\Temp\Test.Xls'
SELECTION-SCREEN END OF BLOCK B1.
AT SELECTION-SCREEN.
IF P_FILE IS INITIAL.
MESSAGE S368(00) WITH 'Please input filename'. STOP.
ENDIF.
"F4 Help getting Excel File Name with Comlete Path
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
PERFORM F_BROWSE CHANGING P_FILE.
START-OF-SELECTION.
PERFORM DATA_RETRIVEL. " Data Retrive
PERFORM HEADER_DATA. " Excel Technical Field Names
PERFORM DOWLOAD. " Download Excel File to the Specific Location
F4 Help getting Excel File Name with Complete Path using Class 'CL_GUI_FRONTEND_SERVICES' and Method 'DIRECTORY_BROWSE'.
FORM F_BROWSE CHANGING FC_FILE.
DATA: LO_GUI TYPE REF TO CL_GUI_FRONTEND_SERVICES,
LV_TITLE TYPE STRING,
LV_FOLDER TYPE STRING,
LV_DIR TYPE STRING.
CREATE OBJECT LO_GUI.
LV_TITLE = 'Define download location'.
LV_FOLDER = 'C:\TEMP\'.
CALL METHOD LO_GUI->DIRECTORY_BROWSE
EXPORTING
WINDOW_TITLE = LV_TITLE "Title of the browser window
INITIAL_FOLDER = LV_FOLDER "Browse begins here
CHANGING
SELECTED_FOLDER = LV_DIR. "Folder Selected By User
FC_FILE = LV_DIR.
ENDFORM. "f_browse
Data Retrival
FORM DATA_RETRIVEL .
SELECT MARA~MATKL MARA~MATNR MARA~MBRSH MARA~MEINS MARA~MTART
INTO CORRESPONDING FIELDS OF TABLE I_FINAL
FROM MARA UP TO 10 ROWS
WHERE MBRSH EQ 'M'
AND MTART EQ 'ERSA'.
ENDFORM. "DATA_RETRIVEL
Header Field Name
FORM HEADER_DATA .
INT_HEAD-FILED1 = 'MBRSH'.
APPEND INT_HEAD.
CLEAR INT_HEAD.
INT_HEAD-FILED1 = 'MTART'.
APPEND INT_HEAD.
CLEAR INT_HEAD.
INT_HEAD-FILED1 = 'MATNR'.
APPEND INT_HEAD.
CLEAR INT_HEAD.
INT_HEAD-FILED1 = 'MEINS'.
APPEND INT_HEAD.
CLEAR INT_HEAD.
INT_HEAD-FILED1 = 'MATKL'.
APPEND INT_HEAD.
CLEAR INT_HEAD.
ENDFORM. " HEADER_DATA
Transfer data to ExcelSheet (i.e. .Xls File)
FORM DOWLOAD .
V_FILETYPE = '.xls'. " Excel File Extentation .xls
V_FILENAME = P_FILE. " File Name
CONCATENATE P_FILE V_FILETYPE INTO LV_FILENAME.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME = LV_FILENAME " Excel File with path and extentation like "C:\Temp\Test.Xls
FILETYPE = 'DBF'
WRITE_FIELD_SEPARATOR = ' '
TABLES
DATA_TAB = I_FINAL " Final Internal table with Data
FIELDNAMES = INT_HEAD " Technical Field Name
EXCEPTIONS
FILE_WRITE_ERROR = 1
NO_BATCH = 2
GUI_REFUSE_FILETRANSFER = 3
INVALID_TYPE = 4
NO_AUTHORITY = 5
UNKNOWN_ERROR = 6
HEADER_NOT_ALLOWED = 7
SEPARATOR_NOT_ALLOWED = 8
FILESIZE_NOT_ALLOWED = 9
HEADER_TOO_LONG = 10
DP_ERROR_CREATE = 11
DP_ERROR_SEND = 12
DP_ERROR_WRITE = 13
UNKNOWN_DP_ERROR = 14
ACCESS_DENIED = 15
DP_OUT_OF_MEMORY = 16
DISK_FULL = 17
DP_TIMEOUT = 18
FILE_NOT_FOUND = 19
DATAPROVIDER_EXCEPTION = 20
CONTROL_FLUSH_ERROR = 21
OTHERS = 22.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " DOWLOAD
Output:
Screenshot # 3
Note: File Name : Test without .Xls Extension ( Extension already mentation in Source Code )
File Location : ‘C:\Temp\’
Screenshot # 4
Reference : "GUI_DOWNLOAD" Function Module Documentation
Source Code : How to Download Leading With Zeros from SAP to Excel Sheet using GUI_Download Function Module - A...
Regard's
Smruti
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
3 | |
3 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 |