Download File Excel with Header

Gunakan function module WS_DOWNLOAD untuk mengunduh file excel dengan header line.

Line code-nya bisa dilihat dibawah ini.

REPORT ydownloadwithheader NO STANDARD PAGE HEADING.

TYPES : BEGIN OF ty_data,
        store(15),
        book(30),
        author(30),
        amount(7) TYPE p,
        price TYPE dmbtr,
        total TYPE dmbtr,
        curr TYPE waers,
        END OF ty_data.

DATA : t_data TYPE ty_data OCCURS 0 WITH HEADER LINE.

DATA: BEGIN OF wa_header,
        name TYPE c LENGTH 30,
      END OF wa_header.

DATA: t_header LIKE TABLE OF wa_header.

START-OF-SELECTION.
  PERFORM f_process_data.
  PERFORM f_download.

*&---------------------------------------------------------------------*
*&      Form  F_PROCESS_DATA
*&---------------------------------------------------------------------*
FORM f_process_data .

*fill data t_data
  t_data-store = 'Gramedia'.
  t_data-book = 'Negeri 5 Menara'.
  t_data-author = 'A. Fuadi'.
  t_data-amount = 1000.
  t_data-price = 50000.
  t_data-total = t_data-amount * t_data-price.
  t_data-curr = 'IDR'.
  APPEND t_data.
  t_data-store = 'Eureka'.
  t_data-amount = 1000.
  t_data-price = 50000.
  t_data-total = t_data-amount * t_data-price.
  APPEND t_data.

*set header
  APPEND 'STORE'  TO t_header.
  APPEND 'BOOK'   TO t_header.
  APPEND 'AUTHOR' TO t_header.
  APPEND 'AMOUNT' TO t_header.
  APPEND 'PRICE'  TO t_header.
  APPEND 'TOTAL'  TO t_header.
  APPEND 'CURRENCY'  TO t_header.

ENDFORM.                    " F_PROCESS_DATA
*&---------------------------------------------------------------------*
*&      Form  F_DOWNLOAD
*&---------------------------------------------------------------------*
FORM f_download.
  DATA: lv_filename TYPE string,
        lv_path TYPE string,
        lv_fullpath TYPE string,
        lv_result TYPE i,
        lv_default_fname TYPE string,
        lv_fname TYPE localfile.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title      = 'File Directory'                  "#EC NOTEXT
      default_extension = 'XLS'
      initial_directory = 'D:\'
    CHANGING
      filename          = lv_filename
      path              = lv_path
      fullpath          = lv_fullpath
      user_action       = lv_result.

  lv_fname = lv_fullpath.

*download file in excel
  CALL FUNCTION 'WS_DOWNLOAD'                               "#EC *
    EXPORTING
      bin_filesize        = ''
      filename            = lv_fname
      filetype            = 'DAT'
    TABLES
      data_tab            = t_data
      fieldnames          = t_header
    EXCEPTIONS
      file_open_error     = 1
      file_write_error    = 2
      invalid_filesize    = 3
      invalid_table_width = 4
      invalid_type        = 5
      no_batch            = 6
      unknown_error       = 7
      OTHERS              = 8.

ENDFORM.                    " F_DOWNLOAD
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s