Ir al contenido principal

A to Z of OLE Excel in ABAP 7.4

 SAP users, both business and end users always need to download the output of a report to spreadsheet and do their analytics. The standard excel output from a report is very simple process but it is old fashioned and the spreadsheet looks quite boring. There is no default formatting and the users have to do all the hard work of changing the fonts, coloring the texts, marking the borders etc.

Updated 16th Aug 2019 – If you are working in non-ABAP 7.4 (below), there is complete reference program for you too. Go to the end of this article. Thank you Legxis (LeonievonK) for the share.

I acknowledge, whatever I mentioned above can be achieved in many ways programmatically. We can do it in the old traditional ABAP way but providing multiple tabs in the spreadsheet and formatting is quite tricky with non OLE method.

OLE = Object Linking and Embedding

The high level agenda of this article is to be the G.O.A.T. (please google it if you do not know the full form) of OLE Excel Guide. We want this one article to be the light house of all ABAP developers who needs to work with OLE Excel in their projects. Too much of chest thumping before the start. Right? 馃槈

We would use the new ABAP 7.4+ syntaxes to created Excel with OLE. We would format the spreadsheet. Headers in bold, font color would be different at different area, background would be blue or any color of your choice. We would mark the borders better and also create multiple Tabs in the excel spreadsheet.

Also Read: Create your first OData Service

Let’s start our dive into the OLE approach of creating Excel.

Step 1 – Include OLE2INCL in the program

The OLE automation programs “OLE2INCL” include needs to be specified. It is kind of a library of OLE syntax.

Step 2 – Populate the internal tables with data

For our demo, we are fetching data from some standard SAP tables so that everyone can use our code snippet.

We have 3 internal tables viz GT_EKKO, GT_EKPO and GT_EKBE ready.

Step 3 – Put the internal table in a string table separated by delimiter

Our goal is to put each internal table data in different tabs of the excel. So we would save the internal table data in a long character variable where the data would be separated by delimiter ‘|’. The long variable would be used later to create a file at run-time and save in the tabs of the spreadsheet.

For our explanation, we have created a Table Type with 1500 long characters and used it for storing data separated by ‘|’. I have defined the delimiter variable as ‘deli’ which contains ‘|’ value ( cl_abap_char_utilities=>horizontal_tab ).

Did you think deli was for food? 馃檪

Step 4 – Put internal table data to respective data/file types

Loop through the internal tables and concatenate the data to the respective string variables (gt_1, gt_2, gt_3). Please note, I have passed the headers as well, which will act like column names in excel sheets.

Check the new syntax for concatenation. If you are new to ABAP 7.4 syntax, please take the Free End to End Video Course on ABAP 7.4 Syntax and New Features.

Step 5 – Time for OLE Application

Create an OLE object as shown below.

Step 6 – Create Workbook and Worksheets

If you are struggling (I hope not) with the concepts of workbook and worksheets then this snapshot should help.

We are going to use all these functionalities in our OLE generation report. Excited?? 馃槢

Let us create Workbook and Worksheets.

All the objects of excel application must be declare with type ‘ole2_object’.

Also CheckAn ABAPer’s First SAPUI5 App in SAP WebIDE

Step 7 – Activate the current worksheet and name it

Step 8 – Pass the data from string internal table to Excel file

There are two ways to pass the data in excel:
i) one-by-one
ii) copy-paste method

Here, we am going to copy whole data from internal table and paste it in the excel. This approach saves time and increases the performance of code. See, we revealed a way to optimize the code. 馃槢

The above snippet is self explanatory. It copies the data of internal table into clipboard.

Ctrl C is always followed by Ctrl V. 馃檪

Now paste the copied data from clipboard to spreadsheet.

For pasting the copied data in excel sheet, we need to select the cells and need to make the range, in which the data will be pasted.

In our program, we have EBELN as our first field in every table. After copying that data into excel sheet, we see EBELN in below format because of space constraints (less width of cell).

Change the width of particular column with property ‘Columnwidth’.

Now we need to select the range and paste it in excel worksheet.

Step 9 – Formatting of the Excel Spreadsheet in SAP ABAP

The above steps ensure, we have the data in our excel. Now we have the interesting job to do i.e. Formatting.

9.1 Create Borders

Whatever data we are going to paste in excel should contains borders. For achieving this, Excel application has a property as ‘borders’.

Above, 7 is indicating border for left side. Same way we have,
8 for right side, 9 for top side, etc.

Also Check : How to export a formatted Excel file with colors, borders, filters in Web Dynpro.

9.2 Change the Font and Color of Header cells

For our case, we have only 4 headers in every worksheet. Therefore we have hard coded for 4 headers. You should make it dynamic.

Above code is pretty simple. But you may want to understand the property names. 馃槉 For different colors, we have different numbers in OLE as shown below:

Choose your favourite color and you are done.

Check the output of the three Tabs.

Isn’t this cool? 馃槈 You can play with the excel application and it’s properties to explore more. Next time create your own excel output, format it with the eyes of a designer and amaze your client with the beautiful presentation. 馃槉 After all, SAP is not that boring as others complain.

The complete end to end code snippet is below at the end of the article. You may copy and past it in your ABAP editor. It should work without any hiccups.

This is the first time I am writing any blog in any space. Hopefully with each new articles, I would learn the tricks of writing better tutorials and learning series. SAPYard inspired me to be a technical author and I thoroughly enjoyed the process, right from conceptualizing the topic, working on the actual program to present and actually implementing my writing skills. If you want to join the Awesome SAPYard Author’s team, feel free to register or you may submit your articles directly to mail@sapyard.com or mailsapyard@gmail.com with all details (text, images and code) and SAPYard team would publish it in your name.


Source:

https://sapyard.com/a-to-z-of-ole-excel-in-abap-7-4/


Esta info la a帽ado, pues es la parte donde podemos combinar con ABAP objetos OLE

Ejemplo

El ejemplo siguiente es para la funci贸n NumberFormat .

Visual Basic para Aplicaciones

Range("A1:A5").Value = 12345
Range("A1:A5").NumberFormat = "0.00"
Range("A1:A5").NumberFormat = "General"


Source

https://docs.microsoft.com/es-es/office/vba/api/excel.cellformat.numberformat


Comentarios

Entradas m谩s populares de este blog

How to add custom field in Additional B Tab for SAP Sales Order In this article we want to explain step by step how to add custom field in SAP Sales Order transaction VA01/VA02/VA03. This time Mr ABAPGurus will give the tutorial of enhancement to add custom field in SAP Sales order. In this sample we will add new custom field for comments and customer satisfaction, this data will save into separate table from SAP Standard table ( VBAK / VBAP ). SAP provided us with ADDITIONAL TAB in the sales order transaction ( VA01/VA02/VA03 ) which allow customer to add custom fields.  T he different between ADDITIONAL A and ADDITIONAL B is the ADDITIONAL A for field which already predefine in Sales Order Header ( VBAK ) fields and ADDITIONAL B is for field that freely define. This sample we will use ADDITIONAL B because we will store data from customer satisfaction into Z database table. 1. Create one Z table using SE11 SAP Transaction code. 2.Using SE38 Transaction code and ope

VOFM

Creating VOFM custom routine, functionality same as SAP standard routine 103 with Additional Functionality 1.    Introduction 1.1   Document Purpose 1.2    This is the document helps you to create custom routine for sales order for below specific requirement: Functional description: In case of sale order with the SD document category ‘K’ or ‘L’ or ‘H’ related to an invoice, we need to fill the fixed value date (VBKD_VALDT) with the billing reference date (VBRK_FKDAT). For example: Invoice 90001111, the billing date is 22.07.2013 If we create a sale order related to this invoice, we want the fixed value date equal to the billing reference date. As per the requirement: Sap standard routine ‘103’ triggers whenever sales order is being created with reference. Sap standard routine is configured in VOFM transaction as: Sap standard routine ‘103’ has the functionality of copying the billing document header data and billing document line item data into the