SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Sample Code to Download ALV Output to Excel



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> OLE2, Excel, WinWord
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Sun Mar 02, 2008 9:18 pm    Post subject: Sample Code to Download ALV Output to Excel Reply with quote

Author : Beena Praveen Chandy
Original: https: //www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/11099070-0b01-0010-fd9d-af9dc9302fa5

Code:
******************************************************************
*& Report ZB_ALVOOPS
******************************************************************
REPORT zb_alvoops .
DATA gi_sflight TYPE STANDARD TABLE OF sflight.
DATA wa_sflight LIKE sflight.
DATA ok_code TYPE sy-ucomm.
DATA go_grid TYPE REF TO cl_gui_alv_grid.
DATA go_custom_container TYPE REF TO cl_gui_custom_container.
DATA : lt_fieldcat TYPE lvc_t_fcat,
ls_fieldcat LIKE LINE OF lt_fieldcat.
DATA ls_layout TYPE lvc_s_layo .

START-OF-SELECTION.
  SET SCREEN '100'.
*--------------------------------------------------------------------
* MODULE STATUS_0100 OUTPUT
*--------------------------------------------------------------------
MODULE status_0100 OUTPUT .
  SET PF-STATUS 'ZB_MYMENU'.
  SET TITLEBAR 'ZB_MYMENU'.
  IF go_custom_container IS INITIAL.
    CREATE OBJECT go_custom_container
    EXPORTING container_name = 'ALV_CONTAINER'.
    CREATE OBJECT go_grid
    EXPORTING
    i_parent = go_custom_container.
    PERFORM load_data_into_grid.
    PERFORM build_field_catalog.
    PERFORM set_layout.
    PERFORM display_data.
  ENDIF.
ENDMODULE. "STATUS_0100 OUTPUT

*&-------------------------------------------------------------------
*& Form load_data_into_grid
*&-------------------------------------------------------------------
FORM load_data_into_grid.
  SELECT * FROM zsflight INTO CORRESPONDING FIELDS OF TABLE gi_sflight.
ENDFORM. "load_data_into_grid
*--------------------------------------------------------------------
* MODULE USER_COMMAND_0100 INPUT
*--------------------------------------------------------------------
MODULE user_command_0100 INPUT.
  CASE ok_code.
    WHEN 'EXIT'.
      LEAVE TO SCREEN 0.
    WHEN 'XLS'.
      PERFORM export_to_excel.
  ENDCASE.
ENDMODULE. "USER_COMMAND_0100 INPUT
*&-------------------------------------------------------------------
*& Form display_data
*&-------------------------------------------------------------------
FORM display_data.
  CALL METHOD go_grid->set_table_for_first_display
    EXPORTING
      i_save                        = 'A'
      i_default                     = 'X'
      is_layout                     = ls_layout
    CHANGING
      it_outtab                     = gi_sflight
      it_fieldcatalog               = lt_fieldcat
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      OTHERS                        = 4.
  IF sy-subrc <> 0.
  ENDIF.
ENDFORM. "display_data
*&-------------------------------------------------------------------
*& Form build_field_catalog
*&-------------------------------------------------------------------
FORM build_field_catalog .
  REFRESH lt_fieldcat.
  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'CARRID'.
  ls_fieldcat-coltext = 'Airline Code '.
  ls_fieldcat-key = 'X'.
  APPEND ls_fieldcat TO lt_fieldcat.
  ls_fieldcat-fieldname = 'CONNID'.
  ls_fieldcat-coltext = 'Flight Connection Number'.
  ls_fieldcat-key = 'X'.
  APPEND ls_fieldcat TO lt_fieldcat.
  ls_fieldcat-fieldname = 'FLDATE'.
  ls_fieldcat-coltext = 'Flight Date'.
  ls_fieldcat-key = 'X'.
  APPEND ls_fieldcat TO lt_fieldcat.
  ls_fieldcat-fieldname = 'PRICE'.
  ls_fieldcat-coltext = 'Airfare'.
  APPEND ls_fieldcat TO lt_fieldcat.
ENDFORM. "build_field_catalog
*&-------------------------------------------------------------------
*& Form set_layout
*&-------------------------------------------------------------------
FORM set_layout.
  ls_layout-grid_title = text-001.
  ls_layout-sel_mode = 'B'.
  ls_layout-edit = ''.
  ls_layout-info_fname = 'color'.
  ls_layout-no_toolbar = 'X'.
  ls_layout-info_fname = 'COLOR'.
ENDFORM. "set_layout
DATA BEGIN OF xmplt_v OCCURS 1.
        INCLUDE STRUCTURE gxxlt_v.
DATA END OF xmplt_v.
DATA BEGIN OF xmplt_o OCCURS 1.
        INCLUDE STRUCTURE gxxlt_o.
DATA END OF xmplt_o.
DATA BEGIN OF xmplt_p OCCURS 1.
        INCLUDE STRUCTURE gxxlt_p.
DATA END OF xmplt_p.
DATA BEGIN OF xmplt_v1 OCCURS 1.
        INCLUDE STRUCTURE gxxlt_v.
DATA END OF xmplt_v1.
DATA BEGIN OF xmplt_o1 OCCURS 1.
        INCLUDE STRUCTURE gxxlt_o.
DATA END OF xmplt_o1.
DATA BEGIN OF xmplt_p1 OCCURS 1.
        INCLUDE STRUCTURE gxxlt_p.
DATA END OF xmplt_p1.
*&-------------------------------------------------------------------
*& Form export_to_excel
*&-------------------------------------------------------------------
FORM export_to_excel .
  DATA: lt_data TYPE REF TO data,
  out_line TYPE REF TO data,
  wa_fieldcat LIKE LINE OF lt_fieldcat,
  l_objtab LIKE STANDARD TABLE OF ddfieldin,
  wa_objtab LIKE LINE OF l_objtab,
  ws_count(3) TYPE c VALUE '1',
  wa_gxxltv TYPE gxxlt_v.
  FIELD-SYMBOLS: <l_fieldcat> TYPE lvc_t_fcat,
  <l_field> LIKE LINE OF <l_fieldcat>,
  <lt_table> TYPE table,
  <l_line> TYPE ANY,
  <fs_xlsdata> LIKE LINE OF gi_sflight,
  <fs_data1> TYPE ANY,
  <fs_data2> TYPE ANY.
* Get the frontend fieldcatalog
  CALL METHOD go_grid->get_frontend_fieldcatalog
    IMPORTING
      et_fieldcatalog = lt_fieldcat.
  DELETE lt_fieldcat WHERE no_out EQ 'X'.
*create dynamic table with the frontend fieldcatalog
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog           = lt_fieldcat
    IMPORTING
      ep_table                  = lt_data
    EXCEPTIONS
      generate_subpool_dir_full = 1
      OTHERS                    = 2.
  IF sy-subrc EQ 0.
    ASSIGN lt_data->* TO <lt_table>.
  ENDIF.
*create work area for the dynamic table
  CREATE DATA out_line LIKE LINE OF <lt_table>.
  ASSIGN out_line->* TO <l_line>.
  LOOP AT lt_fieldcat ASSIGNING <l_field> WHERE no_out NE 'X'.
    wa_objtab-tabname = 'V56I_DLNT_VIEW'.
    wa_objtab-fieldname = <l_field>-fieldname.
    APPEND wa_objtab TO l_objtab.
    CALL FUNCTION 'DD_FIELDINFO_SET_GET'
      EXPORTING
        langu          = sy-langu
      TABLES
        objinfo        = l_objtab
      EXCEPTIONS
        internal_error = 1
        OTHERS         = 2.
    IF sy-subrc EQ 0.
      wa_gxxltv-col_no = ws_count.
      wa_gxxltv-col_name = <l_field>-seltext.
      APPEND wa_gxxltv TO xmplt_v1.
      ADD 1 TO ws_count.
    ENDIF.
  ENDLOOP.
* Fill the dynamic table
  LOOP AT gi_sflight ASSIGNING <fs_xlsdata>.
    LOOP AT lt_fieldcat INTO wa_fieldcat.
      ASSIGN COMPONENT wa_fieldcat-fieldname OF STRUCTURE
      <fs_xlsdata> TO <fs_data1>.
      ASSIGN COMPONENT wa_fieldcat-fieldname OF STRUCTURE
      <l_line> TO <fs_data2>.
      MOVE <fs_data1> TO <fs_data2>.
    ENDLOOP.
    APPEND <l_line> TO <lt_table>.
  ENDLOOP.
  xmplt_o1-line_no = 1.
  xmplt_o1-info_name = text-013.
  APPEND xmplt_o1.
  xmplt_p1-line_no = 1.
  xmplt_p1-text = text-013.
  APPEND xmplt_p1.
* call to display the details in excel
  CALL FUNCTION 'XXL_SIMPLE_API'
    TABLES
      col_text          = xmplt_v1
      data              = <lt_table>
      online_text       = xmplt_o1
      print_text        = xmplt_p1
    EXCEPTIONS
      dim_mismatch_data = 1
      file_open_error   = 2
      file_write_error  = 3
      inv_winsys        = 4
      inv_xxl           = 5
      OTHERS            = 6.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM. " export_to_excel
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> OLE2, Excel, WinWord All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.