download sap abap internal table into an Excel file


Most of the time, abaper need to provide the data of final table, i.e internal table into a excel file,
below code is with just 3 fields, with step by step code, starting from selecting data from database table, getting file location from user through pop up and save the file, on successfully file save, system will automatically open it.  

Program code.

REPORT ZINTERNAL_TABLE_TO_EXCEL.

tables : kna1.

"internal table
types : begin of tp_kna1,
         kunnr type kunnr,
         anred type kna1-anred,
         name1 type kna1-name1,
        end of tp_kna1.

"for headings
types : begin of tp_heading,
          heading(30),
        end of tp_heading.

data : gt_kna1 type STANDARD TABLE OF tp_kna1,
       gs_kna1 type tp_kna1,
       gt_heading type STANDARD TABLE OF tp_heading,
       gs_heading type tp_heading.

*Global variables
DATA: gv_filename TYPE string,                               "file name
      gv_path     TYPE string,                               "file path
      gv_fullpath     TYPE string,                           "file full path
      gv_result   TYPE i,
      LV_BIN_FILESIZE TYPE I, " Binary File Size
      gv_xml_str TYPE string,                                "XML string
      gv_url(500).


SELECTION-SCREEN begin of BLOCK b1 WITH FRAME TITLE text-001.
 select-OPTIONS : s_kunnr for kna1-kunnr.
SELECTION-SCREEN end of BLOCK b1.


START-OF-SELECTION.
     perform get_data.
     perform get_save_location.
     perform build_headings.
     perform download.
end-of-SELECTION.
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_DATA.

    select kunnr anred name1 from kna1 into CORRESPONDING FIELDS OF TABLE gt_kna1
    WHERE kunnr in S_KUNNR.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  GET_SAVE_LOCATION
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_SAVE_LOCATION.

"file name auto generate
  CONCATENATE 'Customer details' '_' sy-datum sy-uzeit INTO gv_filename.

* Display save dialog window
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title      = 'Select the location'
default_extension = 'xls'
default_file_name = GV_FILENAME
*initial_directory = ‘C:’
CHANGING
filename          = gv_filename
path              = gv_path
fullpath          = GV_FULLPATH
user_action       = gv_result.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  BUILD_HEADINGS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM BUILD_HEADINGS.

"excel first row headings or coulmn labels
 clear : GT_HEADING[], gs_heading.

 GS_HEADING-HEADING = 'Customer ID'.
 append gs_heading to gt_heading.
 clear : gs_heading.

 GS_HEADING-HEADING = 'Title'.
 append gs_heading to gt_heading.
 clear : gs_heading.

 GS_HEADING-HEADING = 'Name'.
 append gs_heading to gt_heading.
 clear : gs_heading.

ENDFORM.

FORM DOWNLOAD.

    if GV_FULLPATH is NOT INITIAL.

      CALL FUNCTION 'GUI_DOWNLOAD'
        EXPORTING
*          BIN_FILESIZE            = LV_BIN_FILESIZE
          FILENAME                = GV_FULLPATH
          FILETYPE                = 'DAT'
* IMPORTING
*         FILELENGTH              =
        TABLES
          DATA_TAB                = GT_KNA1
         FIELDNAMES               = GT_HEADING
        EXCEPTIONS
          FILE_WRITE_ERROR        = 1
          NO_BATCH                = 2
          GUI_REFUSE_FILETRANSFER = 3
          INVALID_TYPE            = 4
          NO_AUTHORITY            = 5
          UNKNOWN_ERROR           = 6
          HEADER_NOT_ALLOWED      = 7
          SEPARATOR_NOT_ALLOWED   = 8
          FILESIZE_NOT_ALLOWED    = 9
          HEADER_TOO_LONG         = 10
          DP_ERROR_CREATE         = 11
          DP_ERROR_SEND           = 12
          DP_ERROR_WRITE          = 13
          UNKNOWN_DP_ERROR        = 14
          ACCESS_DENIED           = 15
          DP_OUT_OF_MEMORY        = 16
          DISK_FULL               = 17
          DP_TIMEOUT              = 18
          FILE_NOT_FOUND          = 19
          DATAPROVIDER_EXCEPTION  = 20
          CONTROL_FLUSH_ERROR     = 21
          OTHERS                  = 22.
            .
    IF SY-SUBRC <> 0.
*     Implement suitable error handling here

      else.  "execute file

      CALL METHOD CL_GUI_FRONTEND_SERVICES=>EXECUTE
        EXPORTING
          DOCUMENT               = GV_FULLPATH
*          APPLICATION            =
*          PARAMETER              =
*          DEFAULT_DIRECTORY      =
*          MAXIMIZED              =
*          MINIMIZED              =
*          SYNCHRONOUS            =
*          OPERATION              = 'OPEN'
        EXCEPTIONS
          CNTL_ERROR             = 1
          ERROR_NO_GUI           = 2
          BAD_PARAMETER          = 3
          FILE_NOT_FOUND         = 4
          PATH_NOT_FOUND         = 5
          FILE_EXTENSION_UNKNOWN = 6
          ERROR_EXECUTE_FAILED   = 7
          SYNCHRONOUS_FAILED     = 8
          NOT_SUPPORTED_BY_GUI   = 9
          OTHERS                 = 10
              .
      IF SY-SUBRC <> 0.
*       Implement suitable error handling here
      ENDIF.


      ENDIF.

    endif.

ENDFORM.

Selection screen
File save location dialog

 
Output of downloaded file










Post a Comment

0 Comments

Total Pageviews