Downloading into Excel made easy using simple transformations

In this recipe, we will see how we can use simple transformations in order to download the contents of an internal table into Excel format. The advantage of this method is that the Excel file will contain the desired font size and colors without any programming effort required.

We will first create a sample Excel file in XML format. Then, we will upload it via the transformation editor XSLT_TOOL and generate a transformation accordingly. (The Excel XML and the transformation language may be mixed, thus the Excel XML is converted into a transformation.) The generated transformation will serve as a template representing our Excel file format. We will make slight changes in it and call it in our ABAP program in order to generate the Excel file.

The primary emphasis of this recipe is to generate an XML string containing the content of the Excel data in XML format. The download part of the XML will not be shown.

How to do it...

We will now see in detail the required steps:

  1. We first create an Excel file with the appropriate required column headings and some sample data. We will consider an example of employee number, name, and grade.
    How to do it...
  2. Next, we will save the Excel file in the XML Spreadsheet 2003 format.
    How to do it...
  3. Next, we will create a new simple transformation using transaction XSLT_TOOL.
  4. Then, from the Source Code tab, click on the Import (How to do it...) button. On the File dialog box that appears, select the ExcelXML file created earlier. This will create a transformation program within the editor.
  5. Next, we need to make some minor changes within the transformation. First, we will remove ss:ExpandedRowCount="6" from the Table element. The data rows pertaining to the sample data uploaded must be removed as well. These are contained within Row and Cell elements. (Our uploaded Excel XML file generated a transformation for our sample data. We need to delete the hard-coded lines pertaining to the sample data within the Row and Cell elements. We then make the necessary changes so that the transformation may be used for dynamic data passed in an internal table from our program.)
    How to do it...
  6. We will now add a root element table at the beginning of the transformation, before the tt:template command.
    How to do it...
  7. tt:loop is then added in place of the removed employee data. The loop addresses each line of the internal table "table". We print the contents of the fields PERNR, NAME, and GRADE as texts within the element Cell for every Row.
    How to do it...
  8. We will save and activate the transformation.
  9. We will then make a simple program to populate an internal table based on the root element table. We define an internal table, itab, and work area, wa, based on the table type zst6_tt_per and line type zst6_st_per, respectively. We then populate the table with suitable values.
    How to do it...
  10. Then, the CALL TRANSFORMATION statement is called and the internal table itab is passed. The resulting xml_string string contains the Excel data in XML format.
    How to do it...

How it works...

We uploaded the transformation using the XSLT_TOOL transaction's Import button. This transformation is based on the file that we uploaded. If we call the same transformation, the result will be the same file that was uploaded. Since we want the Excel output to be based on the content of the internal table of the ABAP program, we made the necessary changes to the transformation in order to incorporate a loop on a root element table. During serialization, the internal table itab was passed in place of the root element table defined within the transformation. The loop was run and the content of the table was included in the XML string. The xml_string may then be written as saved on the desktop or emailed to another user. When opened in Excel, it will show the user the data in Excel format along with the column heading colors and the fonts of the original sample file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.251.56