Você está na página 1de 7

The CL_SALV_TABLE class

The CL_SALV_TABLE class has been around for the better part of a few years as a wrapper to the class
CL_GUI_ALV_GRID and the REUSE function modules to generate ALVs.

The CL_SALV_TABLE class is very convenient and easy to use. Just call the factory method create an
instance of an ALV and a few method calls to set it up.

Creating Microsoft Excel files

The CL_SALV_TABLE class also provides support for the generation of Microsoft Excel files. Our main
interests in this document, would be the generation of Microsoft Excel files through the use of
SpreadsheetML (MS Office 2003).

For more information on spreadsheetML refer to https://en.wikipedia.org/wiki/SpreadsheetML

Creating Excel downloads


When an instance of CL_SALV_TABLE is created, you can use the to_xml method and specify the format
to generate an xml file. A spreadsheetML file can also be generated (which is also a derivative of xml
specified under a proprietary DTD/XSD for Microsoft) using the same method and can be named as an
"xls" file. See below example. The xstring content can be saved to a file.
cl_salv_table=>factory(
IMPORTING r_salv_table = lo_table
CHANGING
t_table = <ft_output>
).

data(lv_xstring) =
lo_table->to_xml xml_type = if_salv_bs_xml=>c_type_alv_xml ).

The extreme 'pro' of using this method as opposed to using a simple "call transformation id" statement
is that you'll be saving your time from a ton of data formatting errors that arise by trying to show XML
data in Excel. Believe me I had enough.

The drawback of this method however, is that you cannot style your ALVs the way you want. It will be
formatted to a predefined style.

For example the column header color will be #DDDDDD (a shade of gray) and the column header font
will be Arial.

The following is the analysis I performed while debugging the to_xml method, and how I overcame this
problem.
Analysis of CL_SALV_TABLE class's "to_xml" method

The functionality of the to_xml method can be broken down into the following.

1. Initialization of data types needed for the method

2. Preparing the table data to be transformed into an XML by preparing the data table with relevant
filters and sorts.

3. Transform to XML

It's clear that our method of interest is the transform method of the CL_SALV_BS_TT_UTIL class which is
inherited from the IF_SALV_BS_TT_UTIL interface.

Analysis of CL_SALV_BS_TT_UTIL class's "transform" method

The key findings in the analysis of this method was the order in which the transform method executes.
1. Get the appropriate transform based on the export type selected

2. Call the transform method of the export type

We're almost getting there. It's clear that the transform_tt method is what facilitates the
SpreadsheetML generation.

Let's take a look at the class that calls the transform_tt method to better understand how the XML
content is generated. From the screenshot in step 1 it's clear that the class of interest is
"CL_SALV_BS_TT_OFFICE2003".
What happens in the transform method of this class is

1. Convert the data into a generic XML format used by SAP to generate ALVs. This is present in the
"transform_tt" method of this class (warning, be prepared for a lot of inheritance when you go to the
code).

2. Once the ALV XML is generated, the "transform_from_alvxml" method generates the SpreadsheetML
xml. Going into the method will show you the XML transformation that's used.

SpreadsheetML transformation

If you are familiar with XML/XSLT transformations, you'll know that they can be opened and viewed
through the STRANS tcode. I found from the above code that the transformation used is
Opening the transform in strans shows all the styling and row generation logic incorporated into XSLT
logic.

Following style logic in the transform will allow us to change the header

Changing the Color code of the style element here should change the excel output's column header.

How to use the above information in our code


It's obvious that both transformation and the CL_SALV_BS_TT_OFFICE2003 class are standard SAP
classes and don't allow being modified.
Therefore we use the following approach to get what we want.

1. Create a copy of the transformation, and modify the Styles as needed.

2. Create a copy of the CL_SALV_BS_TT_OFFICE2003 and create a new method that calls the
transformation to return the SpreadsheetML file from the ALV XML.
3. In your code, using the instance of your CL_SALV_TABLE class, first get the ALV XML and then use the
class and new class's method to generate the XML.

lv_xstring = lo_table->to_xml( xml_type = if_salv_bs_xml=>c_type_alv_xml ).

lv_xstring = zcl_salv_bs_tt_office2003=>ztransform_from_alvxml( lv_xstring )


.

4. Save the XSTRING content as XML to a file.

Enjoy