14.01.2020 Views

ABAP_to_the_Future

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

11

ABAP2XLSX

CREATE OBJECT lo_excel_writer TYPE zcl_excel_writer_2007.

ld_xml_file = lo_excel_writer->write_file( lo_excel ).

Listing 11.3 Converting an Excel Object to XML

The interface ZIF_EXCEL_WRITER defines the methods and attributes for converting

an Excel object into an XML file. Theref ore, if a new version of Excel comes out

and it turns out to be radically different, then a new class is created that handles

the new version, and the calling program do es not need to change at all, except

that the CREATE OBJECT statement has to refer to ZCL_EXCEL_WRITER_2057 or whatever

the new Excel version will be. As an example, if the spreadsheet is to be

macro-enabled, then the object has to be created as TYPE ZCL_EXCEL_WRITER_XLSM.

If you drill down into the WRITE_FILE method, then you’ll see a series of steps that

build up the exact folder structure you saw earlier (Figure 11.3) when you looked

at your zipped spreadsheet file—that is, first a ZIP object is created, and then the

folders and XML files are append ed to that ZIP object. The ZCL_EXCEL_WRITER

class uses standard SAP mechanisms to build up an XML structure, with the result

coming out as an XSTRING object, which is essentia lly an enormous amount of

information in a really long string.

As mentioned previously, the settings for the rows that repeat the top when

printing were stored in the workbook file within the xl folder. If you look inside

the WRITE_FILE method, you’ll see a submethod called CREATE_XL_WORKBOOK,

which does what you would expect—that is, it creates the XML file called “workbook”

that lives in the xl folder.

Downloading the Spreadsheet to the Frontend

Once you have an XML file containing the spreadsheet information, you can

export this file from the ABAP system toa directory on your local machine, where

the file will look just like a spreadsheet that has been create d manually in Excel

and then saved.

The code in Listing 11.4, which shows how to download the spreadsheet, consists

entirely of calls to standard SAP methods. First, the XML string is converted into

so-called raw data; then, the user is asked where he wants to save the file; finally,

the file is downloaded in the normal manner.

DATA: lt_rawdata TYPE solix_tab,

ld_bytecount TYPE i,

ld_filename TYPE string,

464

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!