Technical Articles
Uploading the Excel files to OneDrive/ Share Folder using ABAP Program
Hi All,
I am going to explain how do we upload the local excel files to the one Drive or share folder through the Report.
Some one would be confused to upload the files into the share folder. so I am trying to explain some better way.
Step1: We can see the Excel file in the local file folder as below.
I.e.. This PC > Desktop > Emp1
Step2: Before going to write the program we need to get the one drive or Share Folder path.
Note: Please observe there is no File in this OneDrive folder
I.e.. OneDrive > MOURI Tech > CHEP
Copy the URL in notepad
Give the file name you would like to save in One Drive.
“C:\Users\sreeramg\OneDrive – MOURI Tech\CHEP\Employee1.CSV”
Step3: Write the Report for save file in OneDrive:
REPORT zupload_share_point8.
** Data Declarations
DATA: gv_filename TYPE string,
lt_data_tab TYPE TABLE OF x255,
lv_bin_size TYPE i,
lv_file TYPE string.
** Parameters
PARAMETERS : gv_name TYPE localfile.
** Selection screen value request
AT SELECTION-SCREEN ON VALUE-REQUEST FOR gv_name.
CALL FUNCTION ‘KD_GET_FILENAME_ON_F4’
EXPORTING
static = ‘X’
CHANGING
file_name = gv_name.
IF sy–subrc <> 0.
* Implement suitable error handling here
ENDIF.
START-OF-SELECTION.
gv_filename = gv_name.
** upload the file data from desktop/localfile
CALL FUNCTION ‘GUI_UPLOAD‘
EXPORTING
filename = gv_filename
filetype = ‘BIN’
IMPORTING
filelength = lv_bin_size
TABLES
data_tab = lt_data_tab .
lv_file = ‘C:\Users\sreeramg\OneDrive – MOURI Tech\CHEP\Employee1.csv’.
*** Download or save the file in to the share folder
CALL FUNCTION ‘GUI_DOWNLOAD‘
EXPORTING
filename = lv_file
filetype = ‘BIN’
write_field_separator = ‘X’
TABLES
data_tab = lt_data_tab
* fieldnames = it_header
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.
* Implement suitable error handling here
ENDIF.
IF sy–subrc = 0.
WRITE: / ‘Successfully uploaded to One Drive’.
ENDIF.
Step4: Execute the Report and select the Excel file which you like to upload.
Execute again
Successfully uploaded the file in OneDrive.
We can check the OneDrive folder and we can see the saved Excel file.
Hope This log will be helpful..!
Thanks & Regards,
Sreeram G.
Would be great if you could put the code into the proper code tags, so it shows with proper formatting.
So, why would you do this instead of just dragging and drop in windows explorer?
I see one big mistake in your code:
You are saving file (GUI_DOWNLOAD) as filetype 'ASC' (and with write_field_separator), but you read file (GUI_UPLOAD) as filetype = 'BIN'. This might result in unreadable file! (I am surprised it even works for you). I would recommend to use 'BIN' for both. Do not forget you will need to provide file size in GUI_DOWNLOAD, if you work with binary files.
Also at the end this program is no different from reading and saving file to any other local folder (they are both folders in C:\ drive as you see).
I suspect the author might not have checked the file content. Just seeing the file at destination is not a sign of victory. I've had many issues back in the days when working with files and we had to use old FMs.
Just like Matthew, I'm confused why would anyone need to do this using ABAP... Is this just to demonstrate some coding technique? If so, then sorry to say but this code is seriously outdated. I started working with SAP in 2005 and GUI global class already existed back then.
Not to throw shade but we had some file upload/download code examples in ABAP: An Introduction book. And even though the book is already 4 years old and our examples are primitive (again, they were used purely for concept demonstration), they use classes and look more modern. Code samples are available for free to anyone on GitHub.