본문 바로가기

SAP

[ABAP] EXCEL DOWNLOAD Snippet

반응형

 

내가 짠 코드는 아니고 어디서 퍼왔는데 출처가 불분명하다.

아마 비슷하게 검색하면 나올 듯.

 

사용하는 방법은 간단하다.

아래 함수를 사용한다.

*  perform get_data.
*
*  if gt_list[] is not initial.
*
*    perform excel_instantiate.
*    perform download_to_excel.
*
*  endif.

복붙하면 한글로 된 부분이 깨질텐데 그 부분을 고쳐주고,

딱봐도 필드명을 되어있는 곳을 고쳐주면 된다.

"for excel functionality
DATA : lr_excel_structure      TYPE REF TO data,
       lo_source_table_descr   TYPE REF TO cl_abap_tabledescr,
       lo_table_row_descriptor TYPE REF TO cl_abap_structdescr,
       lv_content              TYPE xstring,
       lt_binary_tab           TYPE TABLE OF sdokcntasc,
       lv_length               TYPE i.

DATA :       gt_excel                TYPE TABLE OF 테이블타입.


FORM download_excel.
  CLEAR gt_excel.
  DATA : lt_filter TYPE TABLE OF 인터널테이블타입.
  LOOP AT gt_main INTO DATA(ls_main).
*    IF ls_main-check = 'X'.
      APPEND ls_main TO lt_filter.

*    ENDIF.
  ENDLOOP.

  IF lt_filter[] IS NOT INITIAL.
    gt_excel = CORRESPONDING #( lt_filter ).
    PERFORM excel_instantiate.
    PERFORM download_to_excel.
  ELSE.
    MESSAGE TEXT-i01 TYPE 'I'.
  ENDIF.

ENDFORM.







FORM excel_instantiate.

  "create data reference
  GET REFERENCE OF gt_excel INTO lr_excel_structure.
  DATA(lo_itab_services) = cl_salv_itab_services=>create_for_table_ref( lr_excel_structure ).
  lo_source_table_descr ?= cl_abap_tabledescr=>describe_by_data_ref( lr_excel_structure  ).
  lo_table_row_descriptor ?= lo_source_table_descr->get_table_line_type( ).

  "excel instantiate
  DATA(lo_tool_xls) = cl_salv_export_tool_ats_xls=>create_for_excel(
                            EXPORTING r_data =  lr_excel_structure  ) .

  "Add columns to sheet
  DATA(lo_config) = lo_tool_xls->configuration( ).

  lo_config->add_column(
       EXPORTING
         header_text          =  '구역'
         field_name           =  'AREAID'
         display_type         =   if_salv_bs_model_column=>uie_text_view ).


  lo_config->add_column(
        EXPORTING
          header_text          =  '공장명'
          field_name           =  'SHOPID'
          display_type         =   if_salv_bs_model_column=>uie_text_view ).

  lo_config->add_column(
        EXPORTING
          header_text          =  '저장위치'
          field_name           =  'MTRL_SLOC_ID'
          display_type         =   if_salv_bs_model_column=>uie_text_view ).



  "get excel in xstring
  TRY.
      lo_tool_xls->read_result(  IMPORTING content  = lv_content  ).
    CATCH cx_root.
  ENDTRY.


  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer        = lv_content
    IMPORTING
      output_length = lv_length
    TABLES
      binary_tab    = lt_binary_tab.

ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_TO_EXCEL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  < --  p2        text
*----------------------------------------------------------------------*
FORM download_to_excel.
  DATA : lv_path     TYPE string,
         lv_fullpath TYPE string.
  CONCATENATE '프로그램명' sy-datum sy-uzeit INTO DATA(lv_filename1) SEPARATED BY '_'.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title      = 'Enter File Name'
      default_extension = 'XLSX'
      default_file_name = lv_filename1
    CHANGING
      filename          = lv_filename1
      path              = lv_path
      fullpath          = lv_fullpath.


  IF lv_fullpath IS NOT INITIAL.

    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        bin_filesize            = lv_length
        filename                = lv_fullpath
        filetype                = 'BIN'
      TABLES
        data_tab                = lt_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.
* Implement suitable error handling here
    ELSE.

      CALL METHOD cl_gui_frontend_services=>execute
        EXPORTING
          document               = lv_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.

사실 아래 글을 응용하면 스탠다드성으로 쉽게 할 수 있는데, 체크된 컬럼을 전처리 한다던가 하는 기능을 개발할때 쓴다.

[ABAP] ABAP alv toolbar button 제거하기 + 노가다 snippet (tistory.com)

 

[ABAP] ABAP alv toolbar button 제거하기 + 노가다 snippet

SAP ABAP ALV Toolbar 버튼 제거 / ABAP ALV Toolbar 버튼 추가 / ABAP ALV Toolbar 버튼 변경 / ABAP ALV Toolbar Function Code / ABAP ALV Grid Function Code (tistory.com) SAP ABAP ALV Toolbar 버튼 제거 / ABAP ALV Toolbar 버튼 추가 / ABAP ALV

itchallenger.tistory.com

 

반응형