Professional Documents
Culture Documents
July 29, 2015November 10, 2016 Sab125ABAP Sample, Excel, File, Internal Table
i_line_header: defines if the file has a header line ( column for title)
i_filename : set the filename
i_tab_converted_data : is the internal table containing data from excel file.
Here a sample ABAP Program to upload excel file to internal table in sap. It will
take an Excel file as input and import the content to an ABAP internal Table.
Call to Function F4_FILENAME: This function will help select the Excel file
from your local driver.
Call to the magical standard SAP TEXT_CONVERT_XLS_TO_SAP.
Look above for the Function TEXT_CONVERT_XLS_TO_SAP signature and description.
These function will return the data in Excel into SAP internal table.
Display Excel content with a sample Write Structure.
Do whatever you want with the data.
Here a sampe ABAP Report ready to be copied and which, hopefully, will help you as
a skeleton for your Excel File processing in SAP.
In fact, this ABAP code will
upload an Excel File for Local ( using the F4_FILENAME to select the excel file
)
and transfer all the excel data to ABAP internal Table using
TEXT_CONVERT_XLS_TO_SAP ( to be displayed later ).
TYPE-POOLS: truxs.
* At selection screen
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
field_name = 'P_FILE'
IMPORTING
file_name = p_file.
START-OF-SELECTION.
" Convert Excel Data to SAP internal Table Data
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
* I_FIELD_SEPERATOR =
i_line_header = p_head
i_tab_raw_data = it_raw " WORK TABLE
i_filename = p_file
TABLES
i_tab_converted_data = it_datatab[] "ACTUAL DATA
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
***********************************************************************
* END-OF-SELECTION.
END-OF-SELECTION.
" For sample, Excel Data transfered to internal table is displayed with write
LOOP AT it_datatab INTO wa_datatab.
WRITE:/ wa_datatab-col1,
wa_datatab-col2,
wa_datatab-col3.
ENDLOOP.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
TYPE-POOLS: truxs.
* At selection screen
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
field_name = 'P_FILE'
IMPORTING
file_name = p_file.
START-OF-SELECTION.
" Convert Excel Data to SAP internal Table Data
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
* I_FIELD_SEPERATOR =
i_line_header = p_head
i_tab_raw_data = it_raw " WORK TABLE
i_filename = p_file
TABLES
i_tab_converted_data = it_datatab[] "ACTUAL DATA
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
***********************************************************************
* END-OF-SELECTION.
END-OF-SELECTION.
" For sample, Excel Data transfered to internal table is displayed with write
LOOP AT it_datatab INTO wa_datatab.
WRITE:/ wa_datatab-col1,
wa_datatab-col2,
wa_datatab-col3.
ENDLOOP.
If you want to go further with Excel in SAP, have a look on the SAPLINK ABAP2XLSX.
It is available in GITHUB.
I have already used it with a productive project and I was suprised with how many
opportunities it gives.
For example, you can manage very easily the formating of Cell with the ABAP2XLSX
classes.
Handling Data to and from Excel, is a way more completed and easily using the
predefined methods.
ABAP - Upload data from multiple worksheets of an excel file into SAP
This program demonstrates hoe to read data from multiple worksheets of an excel
file.
I developed this code using SAP class:
CL_GUI_CUSTOM_CONTAINER
and intefaces:
I_OI_CONTAINER_CONTROL,
I_OI_DOCUMENT_PROXY AND
I_OI_SPREADSHEET.
The output:
The Code:
*&---------------------------------------------------------------------*
*& Report ZTEST_SOURAV_EXCEL
*&
*&---------------------------------------------------------------------*
*& Sourav Bhaduri 02-Dec-2008
*&---------------------------------------------------------------------*
DATA:
oref_container TYPE REF TO cl_gui_custom_container,
iref_control TYPE REF TO i_oi_container_control,
iref_document TYPE REF TO i_oi_document_proxy,
iref_spreadsheet TYPE REF TO i_oi_spreadsheet,
iref_error TYPE REF TO i_oi_error.
DATA:
v_document_url TYPE c LENGTH 256,
i_sheets TYPE soi_sheets_table,
wa_sheets TYPE soi_sheets,
i_data TYPE soi_generic_table,
wa_data TYPE soi_generic_item,
i_ranges TYPE soi_range_list.
PARAMETERS:
p_file TYPE localfile OBLIGATORY,
p_rows TYPE i DEFAULT 100 OBLIGATORY, "Rows (Maximum 65536)
p_cols TYPE i DEFAULT 10 OBLIGATORY. "Columns (Maximum 256)
INITIALIZATION.
START-OF-SELECTION.
REFRESH i_data.
*&---------------------------------------------------------------------*
*& Form SUB_FILE_F4
*&---------------------------------------------------------------------*
* F4 help for file path
*----------------------------------------------------------------------*
FORM sub_file_f4 .
DATA:
l_desktop TYPE string,
l_i_files TYPE filetable,
l_wa_files TYPE file_table,
l_rcode TYPE int4.
* Finding desktop
CALL METHOD cl_gui_frontend_services=>get_desktop_directory
CHANGING
desktop_directory = l_desktop
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE e001(00) WITH
'Desktop not found'.
ENDIF.
* Update View
CALL METHOD cl_gui_cfw=>update_view
EXCEPTIONS
cntl_system_error = 1
cntl_error = 2
OTHERS = 3.
Email This