Download Template into Excel

 Download Template into Excel:

**&---------------------------------------------------------------------*
*& Report ZVB_DOWNLOAD_EXCEL_TMP
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT
ZVB_DOWNLOAD_EXCEL_TMP.
TYPE-POOLS:truxs,icon.
TABLES sscrfields.
SELECTION-SCREEN FUNCTION KEY 1.

TYPES: BEGIN OF ty_file,
         ebeln(10) TYPE c,
         ebelp(5)  TYPE c,
         elikz(1)  TYPE c,
       END OF ty_file.

DATA : g_functxt TYPE smp_dyntxt.

DATA:gt_template  TYPE TABLE OF ty_file,
       gt_itab_cols TYPE abap_component_tab.

DATA : g_o_excel_structure      TYPE REF TO data,
       g_o_source_table_descr   TYPE REF TO cl_abap_tabledescr,
       g_o_table_row_descriptor TYPE REF TO cl_abap_structdescr,
       g_f_content              TYPE xstring,
       g_t_binary_tab           TYPE TABLE OF sdokcntasc,
       g_f_length               TYPE i,
       g_f_filename1            TYPE string,
       g_f_path                 TYPE string,
       g_f_fullpath             TYPE string.
*
PARAMETERS : p_file TYPE ibipparms-path.

INITIALIZATION.
  g_functxt-icon_id = icon_xxl.
  g_functxt-icon_text = 'Download Template'.
  sscrfields-functxt_01 = g_functxt.


AT SELECTION-SCREEN.
  CASE sscrfields-ucomm.
    WHEN 'FC01'.
      PERFORM prepare_template.
      PERFORM download_template.
  ENDCASE.

*&---------------------------------------------------------------------*
*& Form prepare_template
*&---------------------------------------------------------------------*
FORM prepare_template .
  GET REFERENCE OF gt_template INTO g_o_excel_structure.
  DATA(g_o_itab_services) = cl_salv_itab_services=>create_for_table_ref( g_o_excel_structure ).
  g_o_source_table_descr ?= cl_abap_tabledescr=>describe_by_data_ref( g_o_excel_structure  ).
  g_o_table_row_descriptor ?= g_o_source_table_descr->get_table_line_type( ).


  DATA(g_o_tool_xls) = cl_salv_export_tool_ats_xls=>create_for_excel(
                            EXPORTING r_data =  g_o_excel_structure  ) .

  DATA : lv_ebeln TYPE string,
         lv_ebelp TYPE string,
         lv_elikz TYPE string.

  lv_ebeln = 'PO number'.
  lv_ebelp = 'po item'.
  lv_elikz = 'DCI'.

  DATA(g_o_config) = g_o_tool_xls->configuration( ).

  g_o_config->add_column(
      EXPORTING
        header_text          =  lv_ebeln " 'PO Number'
        field_name           =  'EBELN'
        display_type         =   if_salv_bs_model_column=>uie_text_view ).

  g_o_config->add_column(
      EXPORTING
        header_text          =  lv_EBELP "'PO item'
        field_name           =  'EBELP'
        display_type         =   if_salv_bs_model_column=>uie_text_view ).


  g_o_config->add_column(
      EXPORTING
        header_text          =  lv_ELIKZ " 'Tracking Number'
        field_name           =  'ELIKZ'
        display_type         =   if_salv_bs_model_column=>uie_text_view ).

  TRY.
      g_o_tool_xls->read_result(  IMPORTING content  = g_f_content  ).
    CATCH cx_root.
  ENDTRY.


  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer        = g_f_content
    IMPORTING
      output_length = g_f_length
    TABLES
      binary_tab    = g_t_binary_tab.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_TEMPLATE
*&---------------------------------------------------------------------*
FORM download_template .
  DATA : lv_window_title TYPE string.
  lv_window_title = 'Select file path'.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title      = lv_window_title "'Select file path'
      default_extension = cl_gui_frontend_services=>filetype_excel
      default_file_name = g_f_filename1
    CHANGING
      filename          = g_f_filename1
      path              = g_f_path
      fullpath          = g_f_fullpath.


  IF g_f_fullpath IS NOT INITIAL.

    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        bin_filesize            = g_f_length
        filename                = g_f_fullpath
        filetype                = 'BIN'
      TABLES
        data_tab                = g_t_binary_tab
      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.
*      MESSAGE text-015 TYPE 'W' DISPLAY LIKE 'E'.
*      LEAVE TO SCREEN sy-dynnr.
    ELSE.

      CALL METHOD cl_gui_frontend_services=>execute
        EXPORTING
          document               = g_f_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.
*        MESSAGE text-014 TYPE 'W' DISPLAY LIKE 'E'.
*        LEAVE TO SCREEN sy-dynnr.

      ENDIF.

    ENDIF.

  ENDIF.

ENDFORM.

Output:


Comments

Popular posts from this blog

CDS Views: Table Functions with Example

ABAP-Managed Database Procedures (AMDP) with Example

Passing Dynamic Variables to SO10 TEXT