You are on page 1of 11

5/16/23, 5:11 PM ABAP Code Samples : Read Excel Files from Presentation Server – Discovering ABAP

DISCOVERING ABAP
Knowledge Base for SAP ABAP Developers

SAP ABAP

ABAP Code Samples : Read Excel Files from


Presentation Server

Date: July 20, 2022 Author: Jagdish 1

In this post you will learn how to read Excel File from Presentation server.

Excel Conditional Movement of Data

x
Read Excel File from Presentation Server

https://discoveringabap.com/2022/07/20/abap-code-samples-read-excel-files-from-presentation-server/ 1/11
5/16/23, 5:11 PM ABAP Code Samples : Read Excel Files from Presentation Server – Discovering ABAP

Option 1 : Use FM ALSM_EXCEL_TO_INTERNAL_TABLE

Below code would upload xlsx file from presentation server i.e. your laptop / desktop. This also
includes example of how to handle date conversion and decimal places for amount.

Sample File

Click to see the sample Excel File : Test.xlsx

DATA : lt_data TYPE STANDARD TABLE OF alsmex_tabline,


lt_flights TYPE STANDARD TABLE OF sflight,
ls_flight TYPE sflight,
lv_file TYPE string VALUE 'C:\Demo\Test.xlsx'.

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'


EXPORTING
filename = CONV localfile( lv_file )
i_begin_col = 1
i_begin_row = 2
i_end_col = 9999
i_end_row = 9999
TABLES
intern = lt_data
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
"Error
ELSE.
LOOP AT lt_data INTO DATA(ls_data).
ASSIGN COMPONENT ls_data-col OF STRUCTURE ls_flight TO FIELD-SYMBOL(<lfs_fld>).
CASE ls_data-col.
when 4. "Convert Date
if strlen( ls_data-value ) = 10.
ls_data-value = |{ ls_data-value+6(4) }{ ls_data-value+3(2) }{ ls_data-value(2) }|.
else.
ls_data-value = sy-datum.
endif.
when 5 or 10. "Handle Decimal place
REPLACE all OCCURRENCES OF '.' IN ls_data-value WITH ''.
REPLACE all OCCURRENCES OF ',' IN ls_data-value WITH '.'.
ENDCASE.
<lfs_fld> = ls_data-value.
AT END OF row.
APPEND ls_flight TO lt_flights.
ENDAT.
ENDLOOP.
cl_demo_output=>display( lt_flights ).
ENDIF.

However, in this option we can not read the data from a specific tab and if the file has multiple tabs it
is not possible to manage the data upload using this method.

Advertisements

https://discoveringabap.com/2022/07/20/abap-code-samples-read-excel-files-from-presentation-server/ 2/11
5/16/23, 5:11 PM ABAP Code Samples : Read Excel Files from Presentation Server – Discovering ABAP

Option 2 : Using class cl_fdt_xl_spreadsheet

TYPES: BEGIN OF xls_line,


data(256) TYPE x,
END OF xls_line.
DATA : lt_bin_data TYPE STANDARD TABLE OF xls_line,
lv_file TYPE string VALUE 'C:\Demo\Test.xlsx'.

cl_gui_frontend_services=>gui_upload(
EXPORTING
filename = lv_file
filetype = 'BIN'
IMPORTING
filelength = DATA(lv_file_length)
header = DATA(lv_xstring_header)
CHANGING
data_tab = lt_bin_data
EXCEPTIONS
OTHERS = 99 ).
"CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = lv_file_length
IMPORTING
buffer = lv_xstring_header
TABLES
binary_tab = lt_bin_data
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc = 0.
TRY.
DATA(lo_excel) = NEW cl_fdt_xl_spreadsheet(
document_name = iv_file
xdocument = lv_xstring_header ) .
lo_excel->if_fdt_doc_spreadsheet~get_worksheet_names(
IMPORTING
worksheet_names = DATA(lt_worksheets) ).
DATA(lv_woksheetname) = lt_worksheets[ 1 ]. "Read first sheet
DATA(lo_data_ref) = lo_excel->if_fdt_doc_spreadsheet~get_itab_from_worksheet(
lv_woksheetname ).
ASSIGN lo_data_ref->* TO FIELD-SYMBOL(<lfs_data_tab>).
cl_demo_output=>display( <lfs_data_tab> ).
CATCH cx_fdt_excel_core.
"Error handling
ENDTRY.
ENDIF.

This is a effective method which allows us to read Excel file. However, this comes at a risk. SAP x
suggests to use this class only within BRF+ framework and not outside. See the note below for the
warning.

https://discoveringabap.com/2022/07/20/abap-code-samples-read-excel-files-from-presentation-server/ 3/11
5/16/23, 5:11 PM ABAP Code Samples : Read Excel Files from Presentation Server – Discovering ABAP
Advertisements

Note 2468709 – Usage of standard class CL_FDT_XL_SPREADSHEET

The solution to this issue is to use the ABAP2XLSX open source project. It can be found on github at
below links.

https://github.com/abap2xlsx/abap2xlsx

Visit ABAP Code Sample : ABAP2XLSX project for tutorial on installing the project.

Visit ABAP Code Samples page for more code samples.

If you like the content, please subscribe…

Type your email… Subscribe

Join 2,984 other followers


Advertisements
x

https://discoveringabap.com/2022/07/20/abap-code-samples-read-excel-files-from-presentation-server/ 4/11
5/16/23, 5:11 PM ABAP Code Samples : Read Excel Files from Presentation Server – Discovering ABAP

Published by Jagdish
Jagdish has extensive experience in SAP ABAP, ABAP on HANA, OOABAP, OData
Development in ABAP, and RESTful Application Programming Model in ABAP. He is a
dedicated SAP Developer who is passionate about his work. Additionally, he owns
and is the sole author of the SAP ABAP knowledge-sharing portal,
https://discoveringabap.com/. If you require any assistance with learning ABAP-
related topics, SAP certifications, or ABAP-related issues, feel free to contact
Jagdish. View all posts by Jagdish

← PREVIOUS NEXT →
SAP Conversational AI [1] : Build a Simple Chatbot ABAP Code Samples : Read Excel Files from
Application Server

ONE THOUGHT ON “ABAP CODE SAMPLES : READ EXCEL FILES FROM


PRESENTATION SERVER”
Add Comment ↓

Pingback: ABAP Code Samples : Write Excel File to Presentation Server –


Discovering ABAP

LEAVE A REPLY

Enter your comment here... x

https://discoveringabap.com/2022/07/20/abap-code-samples-read-excel-files-from-presentation-server/ 5/11
5/16/23, 5:11 PM ABAP Code Samples : Read Excel Files from Presentation Server – Discovering ABAP

Advertisements

18 Years of Filmora®
Makes It Easy to Create
Stunning Videos on
Windows & Mac.
Free Video Editor for Win/Mac, 800+ Video
Effects, Easy-to-Use, Step By Step Tutorial

Download

Ad
report this ad

Seach on this site...

https://discoveringabap.com/2022/07/20/abap-code-samples-read-excel-files-from-presentation-server/ 6/11
5/16/23, 5:11 PM ABAP Code Samples : Read Excel Files from Presentation Server – Discovering ABAP

report this ad

Type your email… Subscribe

Join 2,984 other followers

Quick Links

ABAP Code Samples

ABAP Debugger

ABAP Development in Eclipse

ABAP Editor

ABAP Expressions (7.4+)

ABAP on HANA

ABAP on HANA : AMDP

ABAP on HANA : CDS Views

Devtoberfest 2022

Interview Preparation

OData x

OO ABAP

https://discoveringabap.com/2022/07/20/abap-code-samples-read-excel-files-from-presentation-server/ 7/11
5/16/23, 5:11 PM ABAP Code Samples : Read Excel Files from Presentation Server – Discovering ABAP

RAP Model

SAP ALV with IDA

SAP Conversational AI

SAP Tips & Tricks

SAPUI5/Fiori

Welcome to Discovering ABAP

What's New?

TRENDING POSTS

ABAP 7.4 and beyond [2] : Read Internal Tables with New Syntax

ABAP 7.4 And Beyond [5] : 'New' in Class

ABAP 7.4 and beyond [9] : FOR LOOP for Internal Tables

Exploring ABAP on HANA [7] : Expressions & Operations in CDS Views

ABAP Code Samples : Send emails with .xlsx Attachment

Write to us @jp.sap.abap21@gmail.com for any queries...

x
report this ad

https://discoveringabap.com/2022/07/20/abap-code-samples-read-excel-files-from-presentation-server/ 8/11
5/16/23, 5:11 PM ABAP Code Samples : Read Excel Files from Presentation Server – Discovering ABAP

https://discoveringabap.com/2022/07/20/abap-code-samples-read-excel-files-from-presentation-server/ 9/11
5/16/23, 5:11 PM ABAP Code Samples : Read Excel Files from Presentation Server – Discovering ABAP

https://discoveringabap.com/2022/07/20/abap-code-samples-read-excel-files-from-presentation-server/ 10/11
5/16/23, 5:11 PM ABAP Code Samples : Read Excel Files from Presentation Server – Discovering ABAP

Advertisements

report this ad


© 2023 DISCOVERING ABAP

https://discoveringabap.com/2022/07/20/abap-code-samples-read-excel-files-from-presentation-server/ 11/11

You might also like