Saturday, December 18, 2010
First sample program
REPORT  ztest_notepad.
DATA: BEGIN OF it_t001 OCCURS 0,
bukrs TYPE t001-bukrs,
butxt TYPE t001-butxt,
ort01 TYPE t001-ort01,
END OF it_t001.
DATA: BEGIN OF it_head OCCURS 0,
bukrs TYPE string,
butxt TYPE string,
ort01 TYPE string,
END OF it_head.

START-OF-SELECTION.
SELECT * FROM t001 INTO CORRESPONDING FIELDS OF TABLE it_t001 UP TO 10 ROWS.

it_head-bukrs = 'Company Code'.
it_head-butxt = 'Name of Company Code or Company'.
it_head-ort01 = 'City'.
APPEND it_head.
CLEAR  it_head.
"Download headers first
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = 'C:/t001.xls'
filetype = 'ASC'
append   = space
TABLES
data_tab = it_head.
"Download data to the same file with APPEND = 'X'
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = 'C:/t001.xls'
filetype = 'ASC'
append   = 'X'
TABLES
data_tab = it_t001.

Second sample program
*&---------------------------------------------------------------------*
*& Report  ZVENKAT_DOWNLOAD_XLS_WITH_HEAD
*&
*&---------------------------------------------------------------------*
REPORT  zvenkat_download_xls_with_head.
*--------------------------------------------------------*
"  Data retrieval related declarations
*--------------------------------------------------------*
TYPES:
BEGIN OF t_emp_dat,
pernr TYPE pa0001-pernr,
persg TYPE pa0001-persg,
persk TYPE pa0001-persk,
plans TYPE pa0001-plans,
stell TYPE pa0001-stell,
END OF t_emp_dat,
t_attachment TYPE  solisti1.
DATA:
w_emp_data TYPE t_emp_dat,
w_attachment     TYPE  t_attachment.
DATA:
i_emp_data TYPE STANDARD TABLE OF t_emp_dat,
i_attachment     TYPE STANDARD TABLE OF t_attachment.

PARAMETERS: p_file TYPE string DEFAULT 'c:\tmp\test.xls'.
*--------------------------------------------------------*
"Start-of-selection.
*--------------------------------------------------------*
START-OF-SELECTION.
PERFORM get_data.
PERFORM build_xls_data_and_download.
*&--------------------------------------------------------*
"Form  get_data from PA0001
*&--------------------------------------------------------*
FORM get_data.

SELECT pernr
persg
persk
plans
stell
FROM pa0001
INTO CORRESPONDING FIELDS OF TABLE i_emp_data
UP TO 4 ROWS.

ENDFORM.                    " get_data
*&---------------------------------------------------------*
"Form  build_xls_data_and_download
*&---------------------------------------------------------*
FORM build_xls_data_and_download.
"If you have Unicode check active in program attributes then
"you will need to declare constants as follows.
CLASS cl_abap_char_utilities DEFINITION LOAD.
CONSTANTS:
con_tab  TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
con_cret TYPE c VALUE cl_abap_char_utilities=>cr_lf.
DATA :l_lines TYPE char10.

DESCRIBE TABLE i_emp_data LINES l_lines.
CONCATENATE 'Total no of records:' l_lines
INTO  w_attachment
SEPARATED BY  con_tab.

CONCATENATE con_cret
w_attachment
INTO w_attachment.

APPEND w_attachment TO i_attachment.
CLEAR  w_attachment.

CONCATENATE 'PERNR' 'PERSG' 'PERSK' 'PLANS' 'STELL'
INTO  w_attachment
SEPARATED BY  con_tab.

CONCATENATE con_cret
w_attachment
INTO w_attachment.

APPEND w_attachment TO i_attachment.
CLEAR  w_attachment.

LOOP AT i_emp_data INTO w_emp_data.

CONCATENATE w_emp_data-pernr
w_emp_data-persg
w_emp_data-persk
w_emp_data-plans
w_emp_data-stell
INTO w_attachment
SEPARATED BY con_tab.

CONCATENATE con_cret w_attachment
INTO w_attachment.

APPEND w_attachment TO i_attachment.
CLEAR  w_attachment.
ENDLOOP.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename              = p_file
write_field_separator = con_tab
TABLES
data_tab              = i_attachment.
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.                    "build_xls_data_and_download 

0 comments:

Post a Comment

Your useful comments, suggestions are appreciated.Your comments are moderated.

Followers

Contact Form

Name

Email *

Message *

Web Dynpro ABAP Book

An SAP Consultant

Follow US


Want to Contribute ?

If you are interested in writing about the new stuff you learn everyday while working, please write to the.sap.consultants@gmail.com.

Click on Contribution for more details.