We apologize for the inconvenience, but we need to take the system offline while we improve the platform. Please plan your tasks accordingly.
You can also use the abap2xlsx tool described in SDN blog abap2xlsx - Generate your professional Excel spreadsheet from ABAP, by Ivan Femia, which generates an XLSX file (available from Excel 2007). It is much more simple to use.
Exporting data to Excel - XML to the rescue
There is often a requierment to export report data to a Microsoft Excel spreadsheet. If the report is a simple ALV grid then the ALV control can do it automatically, but otherwise ABAP coding is required.
The simplest solution is to download a tab separated file and then open this in Excel. This is fine for simple data, but has lots of problems:
- no formatting (headings, colours, column widths etc)
- dates are YYYYMMDD, so are not shown as a date in Excel
- Only one worksheet can be exported
- leading zeros are lost
- numeric fields of more than about 15 digits are truncated
There have traditionally been a few ways around this problem
- Use SAP function modules to create an XLS file (no customisation possible)
- Use OLE to start Excel and manipulate the data cell by cell (difficult, not much help, presentation server only)
However, Excel can now read and write xml files. An ABAP program can create an xml file and it can be opened directly into Excel. The advantages and possibilities are endless:
- An xml file is plain text so can be created on the presentation server or application server;
- alternatively, it can be sent as an attachment to an email, or can be provided as a parameter to a web service;
- colours, fonts, number formats and so on can be specified;
- formatting can be applied to rows, columns or single cells;
- column heights and widths can be specified, or auto-formatted;
- multiple worksheets can be created;
- formulas can be entered in cells;
How to proceed
- View the XML output by Excel
- Code ABAP to create xml data as a table or string. Possible techniques are
View and analyse xml file created by Excel
Create a new Excel workbook and enter the data you would like to see. Add formatting, headings, sheet names etc, and some data.
Save as an xml file.
Open the xml file in a text editor (eg Notepad).
It should look something like this:
Possibly the simplest way to create the XML is to create an internal table and fill it with the xml tags and data.
This code is from the ABAP forum, by Rudolf Lindenmann
Coding using the xml DOM concept
SAP provides an interface and classes for building up an xml document. This ensures that the xml document is well formed, ie does not contain any syntax errors. There is some overhead in the coding, so it is probably overkill for a really simple export, but it is a much better alternative for anything complex.
There are a number of blogs on creating an xml document using DOM.
Sample code for a report to download any database table to a file on the presentation server.
Column headings are provided from the data dictionary, formatted bold with yellow background, wordwrap.
Numeric fields are formatted with the number of decimal places specified in the data dictionary.
Text fields are formatted as Text rather than General
Coding using transformations
With ECC5 and later, xml can be created using Transformations. The transformation can be either coded with an ABAP-like syntax, or an XSLT style sheet.
For more information, look the F1 help on CALL TRANSFORMATION
Export the file
Last but not least, the file needs to be exported. Options include:
Download to presentation server using GUI_DOWNLOAD
Download to application server using OPEN DATASET etc
Send as an attachment to an email
Have the xml table/string as a parameter on a RFC or web service.