Capture ABAP report using WRITE statement to internal table with SAP

The below code demonstrates how to create a simple report using the write statement and then capture it to an internal table as it is displayed on screen. Does not work for ALV reports.

*&------------------------------------------------------------*
*& Report  ZREPORT_CAPTURE                                    *
*&                                                            *
*&------------------------------------------------------------*
*&                                                            *
*& Demonstrate function module 'LIST_TO_ASCI'                 *
*& ..........................................                 *
*&                                                            *
*& Captures a report displayed using the write statement to an*
*& internal table, this is then downloaded to a PC file       *
*&------------------------------------------------------------* 
REPORT  ZREPORT_CAPTURE  no standard page heading.
TABLES:     ekko.
TYPE-POOLS: slis.                                 "ALV Declarations
*Data Declaration
*----------------
TYPES: BEGIN OF t_ekko,
  ebeln TYPE ekpo-ebeln,
  ebelp TYPE ekpo-ebelp,
  statu TYPE ekpo-statu,
  aedat TYPE ekpo-aedat,
  matnr TYPE ekpo-matnr,
  menge TYPE ekpo-menge,
  meins TYPE ekpo-meins,
  netpr TYPE ekpo-netpr,
  peinh TYPE ekpo-peinh,
 END OF t_ekko.
DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
      wa_ekko TYPE t_ekko.
*ALV data declarations
DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      gd_tab_group TYPE slis_t_sp_group_alv,
      gd_layout    TYPE slis_layout_alv,
      gd_repid     LIKE sy-repid.
DATA: BEGIN OF it_report OCCURS 0,
      line(300),
      END OF it_report.
************************************************************************
*START-OF-SELECTION.
START-OF-SELECTION.
* retrieve data
  PERFORM data_retrieval.
************************************************************************
*END-OF-SELECTION.
END-OF-SELECTION.
* display date on screen
  write: sy-uline(27).
  LOOP AT it_ekko INTO wa_ekko.
    WRITE:/ sy-vline,
            (10)wa_ekko-ebeln, sy-vline,
            (10)wa_ekko-ebelp, sy-vline.
  ENDLOOP.
  write:/ sy-uline(27).
* capture report to internal table, does not work for ALV reports
  CALL FUNCTION 'LIST_TO_ASCI'
    EXPORTING
      LIST_INDEX               = 0
    TABLES
      listasci                 = it_report.
* download report to file
  CALL FUNCTION 'WS_DOWNLOAD'
    EXPORTING
      filename = 'C:\temp\report.txt'
      filetype = 'ASC'
    TABLES
      data_tab = it_report.
*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
FORM data_retrieval.
  SELECT ebeln ebelp statu aedat matnr menge meins netpr peinh
   UP TO 10 ROWS
    FROM ekpo
    INTO TABLE it_ekko.
ENDFORM.                    " DATA_RETRIEVAL

Related Articles

SAP ALV Reports - ABAP or Advanced List Viewer reports
ALV grid reports using the object methods functionality
ALV TREE Example ABAP code and information for creating ALV tree based reports
SAP Dynamic documents in ABAP objects
SAP dynamic document example screens
ABAP report Progress indicator - When added also Stops report timeout
SAP progress indicator bar to count number of records processed within ABAP report and stop timeout
SAP percentage complete progress indicator bar for your ABAP reports
Prevent ABAP report timeout by Reseting the SAP runtime value using TH_REDISPATCH
ABAP Reporting - Example code and information on various areas of ABAP reporting
ABAP Automatic refresh report to retrieve real time live data from SAP database
Automatic Refresh Report - periodically refreshes results
Write report straight to printer
Convert Spool request to PDF and send as e-mail
Convert Spool request to PDF and send as e-mail
Execute ABAP Report using the SUBMIT statement
ABAP report to display all the selection screen fields and any values input by the user