12
SDN Contribution © 2006 SAP AG 1 Sample Code to Download ALV Output to Excel. Applies to: ABAP. Summary This sample code explains how to download an ALV report output to Excel. It is based on object oriented programming. Author : Beena Praveen Chandy Company: Wipro Technologies Created on: 26 May 2006 Author Bio Beena Praveen Chandy is working as an ABAP Consultant in Wipro Technologies. Code Sample

7247824 Alv Output to Excel

Embed Size (px)

Citation preview

Page 1: 7247824 Alv Output to Excel

SDN Contribution

© 2006 SAP AG 1

Sample Code to Download ALV Output to Excel.

Applies to:

ABAP.

Summary

This sample code explains how to download an ALV report output to Excel. It is based on object oriented programming.

Author : Beena Praveen Chandy

Company: Wipro Technologies

Created on: 26 May 2006

Author Bio

Beena Praveen Chandy is working as an ABAP Consultant in Wipro Technologies.

Code Sample

Page 2: 7247824 Alv Output to Excel

© 2006 SAP AG 2

****************************************************************** *& 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

Page 3: 7247824 Alv Output to Excel

© 2006 SAP AG 3

*&------------------------------------------------------------------- *& 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 *&-------------------------------------------------------------------

Page 4: 7247824 Alv Output to Excel

© 2006 SAP AG 4

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.

Page 5: 7247824 Alv Output to Excel

© 2006 SAP AG 5

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'.

Page 6: 7247824 Alv Output to Excel

© 2006 SAP AG 6

*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

Page 7: 7247824 Alv Output to Excel

© 2006 SAP AG 7

<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

Page 8: 7247824 Alv Output to Excel

Settings in the Menu Painter (SE41)

© 2006 SAP AG 8

Page 9: 7247824 Alv Output to Excel

© 2006 SAP AG 9

Page 10: 7247824 Alv Output to Excel

Output Screens .

© 2006 SAP AG 10

Page 11: 7247824 Alv Output to Excel

© 2006 SAP AG 11

Page 12: 7247824 Alv Output to Excel

Disclaimer and Liability Notice

This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.

SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk.

SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.

© 2006 SAP AG 12