download sap abap internal table to csv with separator and heading


        In many scenarios, abap consultant need to download the internal table data into csv format, also some many times, the separator requirement is different some time field separator like semicolon ';' or comma ',' or pipe '|'  etc.
        This abap code will give you and idea about, how you can download the internal table csv file, along with separator from selection screen and also with column headings.

abap code

REPORT ZINTERNAL_TABLE_TO_CSV.

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
DATAgv_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).

dataiout type table of string .
dataxout type string.
data var type string.
field-symbols<fs>.


SELECTION-SCREEN begin of BLOCK b1 WITH FRAME TITLE text-001.
 select-OPTIONS s_kunnr for kna1-kunnr.
 PARAMETERS     p_sep type char1 OBLIGATORY DEFAULT ';'.
SELECTION-SCREEN end of BLOCK b1.


START-OF-SELECTION.
     perform get_data.
      perform build_headings.
     perform generated_csv_format.
     perform get_save_location.
     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  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  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 'csv'
default_file_name GV_FILENAME
*initial_directory = ‘C:’
CHANGING
filename          gv_filename
path              gv_path
fullpath          GV_FULLPATH
user_action       gv_result.

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              =
*          WRITE_FIELD_SEPARATOR   = 'X'
        TABLES
          DATA_TAB                IOUT
*         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.
*&---------------------------------------------------------------------*
*&      Form  GENERATED_CSV_FORMAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GENERATED_CSV_FORMAT.

 clear iout[]xout.

"first add headings to first row
   loop at GT_HEADING into GS_HEADING.

    if xout is INITIAL.
      xout gs_heading-HEADING.
    else.
      concatenate xout gs_heading-HEADING into xout separated by P_SEP.
    endif.

   clear gs_heading.
   endloop.

"append after loop all heading
    append xout to iout.


loop at gt_kna1 into gs_kna1.

  clear xout.
  do.
    assign component sy-index of structure gs_kna1 to <fs>.
    if sy-subrc <> 0.
      exit.
    endif.
    if sy-index 1.
      xout <fs>.
    else.
      var <fs>.
      CONDENSE var.    "use string , it will work for all data type
      concatenate xout var into xout separated by P_SEP.    "separator will be as per given
    endif.
  enddo.

  append xout to iout.

clear gs_kna1.
endloop.

ENDFORM.
    
selection screen 

         Output








Post a Comment

1 Comments

  1. It works better than I expected. With some tweaks I made it versatile for working with different table structures. Thank you.

    ReplyDelete

Total Pageviews