Monday 30 September 2019

Download Spool to Application and Presentation server in Text, HTML and Excel formats

In this blogpost you will learn to “Download spool to presentation server and application server in 3 formats – Excel, Text and HTML “.

In one of my projects I got a requirement to download the spool in Excel, text and HTML format in presentation/Application server.To convert spool into PDF, SAP has already provided a function module – CONVERT_ABAPSPOOLJOB_2_PDF and CONVERT_OTFSPOOLJOB_2_PDF.But unfortunately to convert in the above 3 formats there is no such Function module present.

Therefore, I have created 3 Function module’s for the 3 formats which converts the data and also download’s into desired location –

1. Z_CONVERT_SPOOL_TO TEXT
2. Z_CONVERT_SPOOL_TO_EXCEL
3. Z_CONVERT_SPOOL_TO_HTML

We need to Pass the below values in our above 3 Function module’s and it will download the file in appropriate location:-

1. Spool number
2. Path of application server
3. Path of presentation server

We can find the spool number and job status in TSP01 and TBTCO table.

Please see the below sample code for better understanding.

Report(Main Program) :

*&---------------------------------------------------------------------*
*& Report Z_SPOOL_TEST
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT z_spool_test.

* Selection screen
SELECTION-SCREEN BEGIN OF BLOCK b1.
PARAMETERS : p_spool TYPE rspoid,            "Spool number
             p_aser  TYPE string,            "Application server path
             p_pser  TYPE string,            "Presentation server path
             p_text  RADIOBUTTON GROUP rb1,  "Text format Radio button
             p_excel RADIOBUTTON GROUP rb1,  "Excel Format Radio button
             p_html  RADIOBUTTON GROUP rb1.  "HTML format Radio button
SELECTION-SCREEN END OF BLOCK b1.

IF p_text = 'X'.
  "Text format
  CALL FUNCTION 'Z_CONVERT_SPOOL_TO_TEXT'
    EXPORTING
      spool_number    = p_spool
      app_serv_path   = p_aser
      local_serv_path = p_pser.

ELSEIF p_excel = 'X'.                        "Excel format

  CALL FUNCTION 'Z_CONVERT_SPOOL_TO_EXCEL'
    EXPORTING
      spool_number    = p_spool
      app_serv_path   = p_aser
      local_serv_path = p_pser.

ELSEIF p_html = 'X'.                         "HTML format

  CALL FUNCTION 'Z_CONVERT_SPOOL_TO_HTML'
    EXPORTING
      spool_number    = p_spool
      app_serv_path   = p_aser
      local_serv_path = p_pser.

ENDIF.

Selection screen :

SAP ABAP Tutorials and Materials, SAP ABAP Learning, SAP ABAP Certifications, SAP ABAP Online Exam

1. Text format Function Module – z_convert_spool_to_text


◈ We can download the spool into the predefined path i.e. Application server/Presentation server in Text format.

◈ The extension of file should be .txt that will be passed into the FM.

Please see the below code –

FUNCTION z_convert_spool_to_text.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(SPOOL_NUMBER) TYPE  RSPOID
*"     REFERENCE(APP_SERV_PATH) TYPE  TEXT1024 OPTIONAL
*"     REFERENCE(LOCAL_SERV_PATH) TYPE  STRING OPTIONAL
*"----------------------------------------------------------------------

  TYPES : tt_bapixmspop TYPE STANDARD TABLE OF bapixmspop.
  DATA : lt_spool_attr_line  TYPE bapixmspoolid,
         lt_spool_list_plain TYPE tt_bapixmspop,
         lt_dummy            TYPE STANDARD TABLE OF rspoattr,
         lv_rqattr           TYPE tsp01,
         lv_first_page       TYPE i,
         lv_last_page        TYPE i,
         lv_pages            TYPE c.

  CALL FUNCTION 'RSPO_GET_INFORMATION_SPOOLJOB'
    EXPORTING
      rqident          = spool_number
    IMPORTING
      spool_attributes = lt_spool_attr_line
    EXCEPTIONS
      no_such_job      = 1
      read_error       = 2
      no_permission    = 3
      OTHERS           = 4.
  IF sy-subrc <> 0.                 ##FM_SUBRC_OK
  ENDIF.

  CALL FUNCTION 'RSPO_GET_ATTRIBUTES_SPOOLJOB'
    EXPORTING
      rqident     = spool_number
    IMPORTING
      rq          = lv_rqattr
    TABLES
      attributes  = lt_dummy
    EXCEPTIONS
      no_such_job = 1
      OTHERS      = 2.
  IF sy-subrc <> 0.           ##FM_SUBRC_OK
  ENDIF.

  lv_first_page = 1.

  CALL FUNCTION 'RSPO_RETURN_ABAP_SPOOLJOB'
    EXPORTING
      rqident              = spool_number
      first_line           = lv_first_page
      last_line            = lv_last_page
      pages                = lv_pages
    TABLES
      buffer               = lt_spool_list_plain
    EXCEPTIONS
      no_such_job          = 1
      not_abap_list        = 2
      job_contains_no_data = 3
      selection_empty      = 4
      no_permission        = 5
      can_not_access       = 6
      read_error           = 7
      OTHERS               = 8.
  IF sy-subrc <> 0.                 ##FM_SUBRC_OK
  ENDIF.

  IF NOT lt_spool_list_plain IS INITIAL.
    IF app_serv_path is NOT INITIAL.
*    Download in Application server
      FIELD-SYMBOLS <hex_container> TYPE any.
      OPEN DATASET app_serv_path IN TEXT MODE FOR OUTPUT ENCODING DEFAULT WITH WINDOWS LINEFEED.
      IF sy-subrc = 0.
        LOOP AT lt_spool_list_plain ASSIGNING <hex_container> .
          TRANSFER <hex_container> TO app_serv_path.
          CLEAR <hex_container>.
        ENDLOOP.
        CLOSE DATASET app_serv_path.
        MESSAGE 'File succesfully transferred' TYPE 'S' .
      ELSE.
        MESSAGE 'File cannot be opened' TYPE 'E' .
      ENDIF.
    ELSEIF local_serv_path is NOT INITIAL.
*      Download in presentation server
      CALL FUNCTION 'GUI_DOWNLOAD'
        EXPORTING
          filename                = local_serv_path
          append                  = 'X'
        TABLES
          data_tab                = lt_spool_list_plain
        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 'File succesfully transferred' TYPE 'S' .
      ENDIF.
      else.
        MESSAGE 'Please provide path for Application/Presentation server' TYPE 'E' .
    ENDIF.
  ENDIF.

ENDFUNCTION.

2. Excel format Function Module – z_convert_spool_to_excel


◈ We can download the spool into the predefined path i.e. Application server/Presentation server in Excel format.

◈ The extension of file should be .xls that will be passed into the FM.

FUNCTION z_convert_spool_to_excel.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(SPOOL_NUMBER) TYPE  RSPOID
*"     REFERENCE(APP_SERV_PATH) TYPE  TEXT1024 OPTIONAL
*"     REFERENCE(LOCAL_SERV_PATH) TYPE  STRING OPTIONAL
*"----------------------------------------------------------------------

  TYPES : tt_bapixmspop TYPE STANDARD TABLE OF bapixmspop.
  DATA : lt_spool_attr_line  TYPE bapixmspoolid,
         lt_spool_list_plain TYPE tt_bapixmspop,
         lv_spool_list       TYPE  list_string_table,
         lv_first_page       TYPE i,
         lv_last_page        TYPE i.

  CALL FUNCTION 'RSPO_GET_INFORMATION_SPOOLJOB'
    EXPORTING
      rqident          = spool_number
    IMPORTING
      spool_attributes = lt_spool_attr_line
    EXCEPTIONS
      no_such_job      = 1
      read_error       = 2
      no_permission    = 3
      OTHERS           = 4.
  IF sy-subrc <> 0.                 ##FM_SUBRC_OK
  ENDIF.

  CALL FUNCTION 'RSPO_RETURN_SPOOLJOB_DAT'
    EXPORTING
      rqident              = spool_number
      first_line           = lv_first_page
      last_line            = lv_last_page
      pages                = 'X'
    IMPORTING
      buffer_dat           = lv_spool_list
    EXCEPTIONS
      no_such_job          = 1
      not_abap_list        = 2
      job_contains_no_data = 3
      selection_empty      = 4
      no_permission        = 5
      can_not_access       = 6
      read_error           = 6
      OTHERS               = 7.
  IF sy-subrc <> 0.                 ##FM_SUBRC_OK
  ENDIF.

  IF NOT lv_spool_list IS INITIAL.
    IF app_serv_path IS NOT INITIAL.
      FIELD-SYMBOLS <hex_container> TYPE any.
      OPEN DATASET app_serv_path FOR OUTPUT IN TEXT MODE ENCODING DEFAULT WITH SMART LINEFEED.
      IF sy-subrc = 0.
        LOOP AT lv_spool_list ASSIGNING <hex_container> .
          TRANSFER <hex_container> TO app_serv_path.
          CLEAR <hex_container>.
        ENDLOOP.
        CLOSE DATASET app_serv_path.
        MESSAGE 'File succesfully transferred' TYPE 'S' .
      ELSE.
        MESSAGE 'File cannot be opened' TYPE 'E' .
      ENDIF.
    ELSEIF local_serv_path IS NOT INITIAL.
*      Download in presentation server
      CALL FUNCTION 'GUI_DOWNLOAD'
        EXPORTING
          filename                = local_serv_path
          append                  = 'X'
        TABLES
          data_tab                = lv_spool_list
        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 'File succesfully transferred' TYPE 'S' .
      ENDIF.
    ELSE.
      MESSAGE 'Please specify either Application/presentation server path' TYPE 'E' .
    ENDIF.
  ENDIF.

ENDFUNCTION.

3. HTML format Function Module – z_convert_spool_to_html


◈ We can download the spool into the predefined path i.e. Application server/Presentation server in HTML format.

◈ The extension of file should be .zip that will be passed into the FM.

◈ It will be download with .ZIP extension, inside zip folder we will have out HTML format spool file.

FUNCTION z_convert_spool_to_html.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(SPOOL_NUMBER) TYPE  RSPOID
*"     REFERENCE(APP_SERV_PATH) TYPE  TEXT1024 OPTIONAL
*"     REFERENCE(LOCAL_SERV_PATH) TYPE  STRING OPTIONAL
*"----------------------------------------------------------------------

  DATA : lt_spool_attr_line   TYPE bapixmspoolid,
         lv_spool_list_zipped TYPE  xstring,
         lv_abap_true         TYPE boolean,
         lt_tab               TYPE enh_version_management_hex_tb,
         lv_size              TYPE i,
         lv_first_page        TYPE i,
         lv_last_page         TYPE i,
         lv_pages             TYPE c.

  CALL FUNCTION 'RSPO_GET_INFORMATION_SPOOLJOB'
    EXPORTING
      rqident          = spool_number
    IMPORTING
      spool_attributes = lt_spool_attr_line
    EXCEPTIONS
      no_such_job      = 1
      read_error       = 2
      no_permission    = 3
      OTHERS           = 4.
  IF sy-subrc <> 0.                 ##FM_SUBRC_OK
  ENDIF.

  lv_first_page = 1.
  lv_last_page = 0.
  lv_abap_true = 'X'.

  CALL FUNCTION 'RSPO_RETURN_SPOOLJOB_HTML'
    EXPORTING
      rqident              = spool_number
      first_line           = lv_first_page
      last_line            = lv_last_page
      pages                = lv_abap_true
      compressed           = lv_abap_true
    IMPORTING
      e_zip                = lv_spool_list_zipped
    EXCEPTIONS
      no_such_job          = 1
      not_abap_list        = 2
      job_contains_no_data = 3
      selection_empty      = 4
      no_permission        = 5
      can_not_access       = 6
      read_error           = 7
      conversion_error     = 8
      cannot_compress      = 9
      OTHERS               = 10.
  CASE sy-subrc.
    WHEN 0.  " everything ok
  ENDCASE.

  IF xstrlen( lv_spool_list_zipped ) > 0.

    CALL FUNCTION 'ENH_XSTRING_TO_TAB'
      EXPORTING
        im_xstring = lv_spool_list_zipped
      IMPORTING
        ex_data    = lt_tab
        ex_leng    = lv_size.

    IF NOT lt_tab IS INITIAL.
      IF app_serv_path IS NOT INITIAL.
        FIELD-SYMBOLS <hex_container> TYPE x.
        OPEN DATASET app_serv_path FOR OUTPUT IN BINARY MODE.
        IF sy-subrc = 0.
          LOOP AT lt_tab ASSIGNING <hex_container> .
            TRANSFER <hex_container> TO app_serv_path.
          ENDLOOP.
          CLOSE DATASET app_serv_path.
          MESSAGE 'File succesfully transferred' TYPE 'S' .
        ELSE.
          MESSAGE 'File cannot be opened' TYPE 'E' .
        ENDIF.
      ELSEIF local_serv_path IS NOT INITIAL.
        CALL FUNCTION 'GUI_DOWNLOAD'
          EXPORTING
            bin_filesize            = lv_size
*           codepage                = codepage
            filename                = local_serv_path
            filetype                = 'BIN'
*           append                  = append
*           WRITE_BOM               = write_bom
*           WK1_N_FORMAT            = ' '
*           WK1_N_SIZE              = ' '
*           WK1_T_FORMAT            = ' '
*           WK1_T_SIZE              = ' '
*           COL_SELECT              = ' '
*           COL_SELECTMASK          = ' '
*           NO_AUTH_CHECK           = ' '
*      IMPORTING
*           FILELENGTH              =
          TABLES
            data_tab                = lt_tab
*           FIELDNAMES              =
          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
            OTHERS                  = 19.
        IF sy-subrc = 0.
          MESSAGE 'File succesfully transferred' TYPE 'S' .
        ENDIF.
      ELSE.
        MESSAGE 'Please provide path for Application/Presentation server' TYPE 'E' .
      ENDIF.
    ENDIF.
  ENDIF.

ENDFUNCTION.

No comments:

Post a Comment