9

Download internal table data in XLSX from ABAP

 10 months ago
source link: https://blogs.sap.com/2023/08/22/download-internal-table-data-in-xlsx-from-abap/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
August 22, 2023 2 minute read

Download internal table data in XLSX from ABAP

Hi folks,

I know this is a very old topic, but I didn’t find easy solution for this. My solution is simple to output the internal table data to XLSX file. Just choose the file name by F4 help, make sure you give the extension ‘xlsx’.

The solution is based on the built in functions of cl_salv_table class. I hope this will help you. Please let me know your comments and suggestions. Please ignore the naming conventions and untidiness of the code. 🙂

Cheers! Happy coding! 🙂

Code:

*&---------------------------------------------------------------------*
*& Report ZARNAB_XLSX
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zarnab_xlsx.

TYPES:
  BEGIN OF ty_s_cline,
    line TYPE c LENGTH 1024, " Line of type Character
  END OF ty_s_cline .

DATA: gr_table     TYPE REF TO cl_salv_table,          " Basis Class for Simple Tables
      gt_outtab    TYPE STANDARD TABLE OF sflight,     " Flight
      lr_functions TYPE REF TO cl_salv_functions_list, " Generic and User-Defined Functions in List-Type Tables
      lr_layout    TYPE REF TO cl_salv_layout,         " Settings for Layout
      lv_xml_type  TYPE salv_bs_constant,
      lv_xml       TYPE xstring,
      gt_srctab    TYPE STANDARD TABLE OF ty_s_cline,
      gt_len       TYPE i,                             " Len of type Integers
      lv_file_xlsx TYPE string,
      lv_file      TYPE localfile,                     " Local file for upload/download
      ls_key       TYPE salv_s_layout_key,             " Layout Key
      lr_content   TYPE REF TO cl_salv_form_element.   " General Element in Design Object

PARAMETERS: p_file TYPE localfile. " Local file for upload/download

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  CALL FUNCTION 'F4_FILENAME'
    IMPORTING
      file_name = lv_file.

  p_file = lv_file.
  lv_file_xlsx = lv_file.

START-OF-SELECTION.

  SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_outtab UP TO 100 ROWS.

  TRY.

      cl_salv_table=>factory(
        IMPORTING
          r_salv_table = gr_table
        CHANGING
          t_table      = gt_outtab ).

    CATCH cx_salv_msg. "#EC NO_HANDLER

  ENDTRY.

  lr_functions = gr_table->get_functions( ).

  lr_functions->set_all( abap_true ).

  lr_layout = gr_table->get_layout( ).

  ls_key-report = sy-repid.

  lr_layout->set_key( ls_key ).

  lr_layout->set_save_restriction( if_salv_c_layout=>restrict_user_independant ).

  gr_table->set_top_of_list( lr_content ).

  gr_table->set_end_of_list( lr_content ).

*  gr_table->display( ).

  lv_xml_type =  if_salv_bs_xml=>c_type_xlsx. "if_salv_bs_xml=>c_type_mhtml.

  lv_xml      = gr_table->to_xml( xml_type = lv_xml_type ).

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer        = lv_xml
*     APPEND_TO_TABLE       = ' '
    IMPORTING
      output_length = gt_len
    TABLES
      binary_tab    = gt_srctab.

  CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      bin_filesize            = gt_len
      filename                = lv_file_xlsx
      filetype                = 'BIN'
    CHANGING
      data_tab                = gt_srctab
    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
      not_supported_by_gui    = 22
      error_no_gui            = 23
      OTHERS                  = 24.

  IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ELSE. " ELSE -> IF sy-subrc <> 0

    MESSAGE 'Excel file downloaded!' TYPE 'S'.

  ENDIF. " IF sy-subrc <> 0

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK