Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Print EXCEL file from ABAP program ?

Former Member
0 Likes
3,414

Hello,

In my program, I need to print an excel file using the OLE:

CREATE OBJECT excel 'EXCEL.APPLICATION' .

but then i do not the command for printing the file , for example filename = C:\test.xls

Thanks

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,890

Check this link,

Regards,

Joan

8 REPLIES 8
Read only

Former Member
0 Likes
1,890

check the folloiwng example:- in two part PART-1

REPORT ZOLE_ABAP.

INCLUDE ole2incl.

TYPES: BEGIN OF ty_spfli,

kunnr TYPE kna1-kunnr,

land1 TYPE kna1-land1,

NAME1 TYPE KNA1-NAME1,

ORT01 TYPE KNA1-ORT01,

REGIO TYPE KNA1-REGIO,

ADRNR TYPE KNA1-ADRNR,

END OF ty_spfli.

TYPES: BEGIN OF ty_titles,

title(20) TYPE c,

field(20) TYPE c,

END OF ty_titles.

DATA: t_spfli TYPE STANDARD TABLE OF ty_spfli,

t_titles TYPE STANDARD TABLE OF ty_titles.

FIELD-SYMBOLS: <fs_spfli> LIKE LINE OF t_spfli,

<fs_titles> LIKE LINE OF t_titles,

<fs> TYPE ANY.

DATA: w_tabix TYPE sy-tabix,

w_titles TYPE sy-tabix,

w_line TYPE sy-tabix,

w_field TYPE string,

filename TYPE string,

path TYPE string,

fullpath TYPE string.

DATA: data_titles TYPE REF TO data.

DATA: e_sheet TYPE ole2_object,

e_activesheet TYPE ole2_object,

e_newsheet TYPE ole2_object,

e_appl TYPE ole2_object,

e_work TYPE ole2_object,

e_cell TYPE ole2_object,

e_color TYPE ole2_object,

e_bold TYPE ole2_object.

SELECTION-SCREEN BEGIN OF BLOCK b1.

PARAMETERS: p_file TYPE rlgrap-filename.

SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.

PERFORM get_titles.

PERFORM get_data.

PERFORM create_excel.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL METHOD cl_gui_frontend_services=>file_save_dialog

EXPORTING

window_title = 'Select archivo'

default_extension = 'xls'

file_filter = '*.xls'

CHANGING

filename = filename

path = path

fullpath = fullpath.

IF sy-subrc EQ 0.

p_file = fullpath.

ENDIF.

Read only

Former Member
0 Likes
1,890

REMAING PART PART-II

FORM get_titles.

CREATE DATA data_titles TYPE ty_titles.

ASSIGN data_titles->* TO <fs_titles>.

<fs_titles>-title = 'Customer Number 1'.

<fs_titles>-field = 'KUNNR'.

APPEND <fs_titles> TO t_titles.

<fs_titles>-title = 'Country Key'.

<fs_titles>-field = 'LAND1'.

APPEND <fs_titles> TO t_titles.

<fs_titles>-title = 'Name 1'.

<fs_titles>-field = 'NAME1'.

APPEND <fs_titles> TO t_titles.

<fs_titles>-title = 'City'.

<fs_titles>-field = 'ORT01'.

APPEND <fs_titles> TO t_titles.

<fs_titles>-title = 'Region'.

<fs_titles>-field = 'REGIO'.

APPEND <fs_titles> TO t_titles.

<fs_titles>-title = 'Address'.

<fs_titles>-field = 'ADRNR'.

APPEND <fs_titles> TO t_titles.

ENDFORM. "get_titles

FORM get_data.

SELECT KUNNR LAND1 NAME1 ORT01 REGIO ADRNR

INTO TABLE t_spfli

FROM KNA1

WHERE LAND1 EQ 'IN'

and KUNNR EQ '0000700008'.

ENDFORM. " get_data

Read only

0 Likes
1,890

hi,

thanks for your help

But in the code I do not see and method that allows printing directly the excel file ?

please show me !

Thanks

Read only

Former Member
0 Likes
1,890

REMAING PART PART-III

FORM create_excel.

w_line = 1.

CREATE OBJECT e_appl 'EXCEL.APPLICATION'.

SET PROPERTY OF e_appl 'VISIBLE' = 1.

CALL METHOD OF e_appl 'WORKBOOKS' = e_work.

CALL METHOD OF e_work 'Add' = e_work.

GET PROPERTY OF e_appl 'ActiveSheet' = e_activesheet.

SET PROPERTY OF e_activesheet 'Name' = 'Customer Details'.

LOOP AT t_spfli ASSIGNING <fs_spfli>.

w_tabix = sy-tabix.

w_line = w_line + 1.

LOOP AT t_titles ASSIGNING <fs_titles>.

w_titles = sy-tabix.

CALL METHOD OF e_appl 'Cells' = e_cell

EXPORTING

#1 = 1

#2 = w_titles.

SET PROPERTY OF e_cell 'Value' = <fs_titles>-title.

GET PROPERTY OF e_cell 'Interior' = e_color.

SET PROPERTY OF e_color 'ColorIndex' = 35.

GET PROPERTY OF e_cell 'Font' = e_bold.

SET PROPERTY OF e_bold 'Bold' = 1.

CALL METHOD OF e_appl 'Cells' = e_cell

EXPORTING

#1 = w_line

#2 = w_titles.

CONCATENATE '<fs_spfli>-' <fs_titles>-field

INTO w_field.

ASSIGN (w_field) TO <fs>.

SET PROPERTY OF e_cell 'Value' = <fs>.

GET PROPERTY OF e_cell 'Interior' = e_color.

SET PROPERTY OF e_cell 'ColumnWidth' = 20.

SET PROPERTY OF e_color 'ColorIndex' = 0.

GET PROPERTY OF e_cell 'Font' = e_bold.

SET PROPERTY OF e_bold 'Bold' = 0.

ENDLOOP.

ENDLOOP.

CALL METHOD OF e_work 'SAVEAS'

EXPORTING

#1 = p_file.

CALL METHOD OF e_work 'close'.

CALL METHOD OF e_appl 'QUIT'.

FREE OBJECT e_appl.

ENDFORM. " create_excel

Read only

Former Member
0 Likes
1,890

Hi TinhLangTham,

Let say example filename = p_path. "C:\test.xls

DATA: it_string_copy(255) TYPE c OCCURS 0.

*Create Excel Application
    CREATE OBJECT excel 'EXCEL.APPLICATION'.

    IF sy-subrc NE 0.
      WRITE: / 'No EXCEL creation possible'.
      STOP.
    ENDIF.

*Create workbook in excel
    CALL METHOD OF excel 'WORKBOOKS' = workbook .
*Set property of excel visible
    SET PROPERTY OF excel 'VISIBLE' = 1.
    SET PROPERTY OF excel 'SheetsInNewWorkbook' = 1.
    CALL METHOD OF workbook 'ADD'.

    CALL METHOD OF excel 'WORKSHEETS' = sheet
      EXPORTING
      #1 = 1.
*Set name of Sheet and active it
    SET PROPERTY OF sheet 'NAME' = 'PO_PR_Changes '.
    CALL METHOD OF sheet 'ACTIVATE'.

*  GET PROPERTY OF EXCEL 'ActiveWorkbook' = SHEET.

*Past the data into Clip board
    CALL METHOD cl_gui_frontend_services=>clipboard_export
      IMPORTING
        data         = it_string_copy[]  "Final Table
      CHANGING
        rc           = rc
      EXCEPTIONS
        cntl_error   = 1
        error_no_gui = 2
        OTHERS       = 3.
*  Paste from clipboard
    CALL METHOD OF columns 'Select'.
    CALL METHOD OF sheet 'Paste'.

    CALL METHOD OF sheet 'SAVEAS'
      EXPORTING #1 = filename #2 = 1.  "file name is the path.

    SET PROPERTY OF excel 'VISIBLE' = 0.  " Here is the property excel will be visible

    FREE OBJECT:
              sheet.

Regards,

Suneel G

Read only

Former Member
0 Likes
1,890

Hello ,

You can try this code for printing EXCEL file from ABAP:

*

  • Make used of OLE to create a new Excel File.

*

*

REPORT ZCREATEEXCEL.

TYPE-POOLS OLE2.

DATA: EXCEL TYPE OLE2_OBJECT,

WORKBOOKS TYPE OLE2_OBJECT,

WORKBOOK TYPE OLE2_OBJECT.

DATA: FILENAME LIKE RLGRAP-FILENAME.

  • START THE EXCEL APPLICATION

CREATE OBJECT EXCEL 'EXCEL.APPLICATION'.

PERFORM ERR_HDL.

  • PUT EXCEL IN FRONT

SET PROPERTY OF EXCEL 'VISIBLE' = 1.

PERFORM ERR_HDL.

  • INFORM USER OF THE CURRENT STATUS

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

PERCENTAGE = 0

TEXT = TEXT-I08

EXCEPTIONS

OTHERS = 1.

  • CREATE AN EXCEL WORKBOOK OBJECT

CALL METHOD OF EXCEL 'WORKBOOKS' = WORKBOOKS.

PERFORM ERR_HDL.

CALL METHOD OF WORKBOOKS 'ADD' = WORKBOOK.

PERFORM ERR_HDL.

  • EXCEL FILENAME

CONCATENATE SY-REPID '_' SY-DATUM6(2) '_' SY-DATUM4(2) '_'

SY-DATUM(4) '_' SY-UZEIT '.XLS' INTO FILENAME.

CALL METHOD OF WORKBOOK 'SAVEAS' EXPORTING #1 = FILENAME.

FORM ERR_HDL.

IF SY-SUBRC <> 0.

WRITE: / 'OLE ERROR: RETURN CODE ='(I10), SY-SUBRC.

STOP.

ENDIF.

ENDFORM.

*-- End of Program

Hope this can help your requirement

Read only

Former Member
0 Likes
1,890

See i give full code in three section u just run this code

Edited by: krupa jani on May 12, 2009 11:24 AM

Read only

Former Member
0 Likes
1,891

Check this link,

Regards,

Joan