Application Development 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: 

Function module to download to Excel worksheet.

Former Member
0 Kudos

Hi All,

Need a function module to download internal table data to exact worksheet number in the excel sheet, that too it sholud be appended from some row number.

The regular function modules are helping just to create an excel sheet and fill data, But not to append to exact work sheet, like work sheet no.5 or 6 of excel on presentation.

Thanks in Advance,

Regards,

Yogesh

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Check WS_EXCEL.

regards

prabhu

12 REPLIES 12

Former Member
0 Kudos

Check WS_EXCEL.

regards

prabhu

Former Member
0 Kudos

Hi

Refer link below It will definately solve your problem

https://www.sdn.sap.com/irj/sdn/forums

Mark if Helpful

Regards

Tushar Mundlik

Former Member
0 Kudos

below is the code for deciding the fiel to download in which sheet and which palce ..etc ..

this example for 2 work sheets

Multiple sheet Excel Document   

Example code for create a multiple sheet Excel document:
 

*&---------------------------------------------------------------------*
*& Report  ZMULTIEXCEL                                                  *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT  MULTIEXCEL                    .

INCLUDE ole2incl.
DATA: application TYPE ole2_object,
       workbook TYPE ole2_object,
       sheet TYPE ole2_object,
       cells TYPE ole2_object.
CONSTANTS: row_max TYPE i VALUE 256.
DATA index TYPE i.

DATA: BEGIN OF itab1 OCCURS 0, first_name(10), END OF itab1.
DATA: BEGIN OF itab2 OCCURS 0, last_name(10), END OF itab2.
DATA: BEGIN OF itab3 OCCURS 0, formula(50), END OF itab3.


************************************************************************
*START-OF-SELECTION
START-OF-SELECTION.

  APPEND: 'Peter' TO itab1, 'Ivanov' TO itab2,
                              '=Sheet1!A1 & " " & Sheet2!A1' TO itab3,
            'John' TO itab1, 'Smith' TO itab2,
                              '=Sheet1!A2 & " " & Sheet2!A2' TO itab3.

  CREATE OBJECT application 'excel.application'.
  SET PROPERTY OF application 'visible' = 1.
  CALL METHOD OF application 'Workbooks' = workbook.
  CALL METHOD OF workbook 'Add'.

* Create first Excel Sheet
  CALL METHOD OF application 'Worksheets' = sheet
                               EXPORTING #1 = 1.
  CALL METHOD OF sheet 'Activate'.
  SET PROPERTY OF sheet 'Name' = 'Sheet1'.
  LOOP AT itab1.
    index = row_max * ( sy-tabix - 1 ) + 1. " 1 - column name
    CALL METHOD OF sheet 'Cells' = cells EXPORTING #1 = index.
    SET PROPERTY OF cells 'Value' = itab1-first_name.
  ENDLOOP.

* Create second Excel sheet
  CALL METHOD OF application 'Worksheets' = sheet
                               EXPORTING #1 = 2.
  SET PROPERTY OF sheet 'Name' = 'Sheet2'.
  CALL METHOD OF sheet 'Activate'.
  LOOP AT itab2.
    index = row_max * ( sy-tabix - 1 ) + 1. " 1 - column name
    CALL METHOD OF sheet 'Cells' = cells EXPORTING #1 = index.
    SET PROPERTY OF cells 'Value' = itab2-last_name.
  ENDLOOP.

* Create third Excel sheet
  CALL METHOD OF application 'Worksheets' = sheet
                               EXPORTING #1 = 3.
  SET PROPERTY OF sheet 'Name' = 'Sheet3'.
  CALL METHOD OF sheet 'Activate'.
  LOOP AT itab3.
    index = row_max * ( sy-tabix - 1 ) + 1. " 1 - column name
    CALL METHOD OF sheet 'Cells' = cells EXPORTING #1 = index.
    SET PROPERTY OF cells 'Formula' = itab3-formula.
    SET PROPERTY OF cells 'Value' = itab3-formula.
  ENDLOOP.

* Save excel speadsheet to particular filename
  CALL METHOD OF sheet 'SaveAs'
                  EXPORTING #1 = 'c:tempexceldoc1.xls'     "filename
                            #2 = 1.                          "fileFormat
                            
*  Closes excel window, data is lost if not saved
*  SET PROPERTY OF application 'visible' = 0.

 

Girish

0 Kudos

Hi ,

I am not getting 4th and 5th sheet newly created,

instead they are getting over writed in 3rd sheet itself.

how to resolve this.

0 Kudos

I faced the same problem as well (could not get the page 3 and page 4). I managed to find the solution here.

<a href="https://www.sdn.sap.comhttp://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/204d1bb8-489d-2910-d0b5-cdddb3227820?overridelayout=true">https://www.sdn.sap.comhttp://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/204d1bb8-489d-2910-d0b5-cdddb3227820?overridelayout=true</a>

Former Member
0 Kudos

Download a report to excel with format (border, color cell, etc)

Try this program...it may help you to change the font ..etc.

Code:

REPORT ZSIRI NO STANDARD PAGE HEADING.

  • this report demonstrates how to send some ABAP data to an

  • EXCEL sheet using OLE automation.

INCLUDE OLE2INCL.

  • handles for OLE objects

DATA: H_EXCEL TYPE OLE2_OBJECT, " Excel object

H_MAPL TYPE OLE2_OBJECT, " list of workbooks

H_MAP TYPE OLE2_OBJECT, " workbook

H_ZL TYPE OLE2_OBJECT, " cell

H_F TYPE OLE2_OBJECT. " font

TABLES: SPFLI.

DATA H TYPE I.

  • table of flights

DATA: IT_SPFLI LIKE SPFLI OCCURS 10 WITH HEADER LINE.

&----


*& Event START-OF-SELECTION

&----


START-OF-SELECTION.

  • read flights

SELECT * FROM SPFLI INTO TABLE IT_SPFLI UP TO 10 ROWS.

  • display header

ULINE (61).

WRITE: / SY-VLINE NO-GAP,

(3) 'Flg'(001) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP,

(4) 'Nr'(002) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP,

(20) 'Von'(003) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP,

(20) 'Nach'(004) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP,

(8) 'Zeit'(005) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP.

ULINE /(61).

  • display flights

LOOP AT IT_SPFLI.

WRITE: / SY-VLINE NO-GAP,

IT_SPFLI-CARRID COLOR COL_KEY NO-GAP, SY-VLINE NO-GAP,

IT_SPFLI-CONNID COLOR COL_NORMAL NO-GAP, SY-VLINE NO-GAP,

IT_SPFLI-CITYFROM COLOR COL_NORMAL NO-GAP, SY-VLINE NO-GAP,

IT_SPFLI-CITYTO COLOR COL_NORMAL NO-GAP, SY-VLINE NO-GAP,

IT_SPFLI-DEPTIME COLOR COL_NORMAL NO-GAP, SY-VLINE NO-GAP.

ENDLOOP.

ULINE /(61).

  • tell user what is going on

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

  • PERCENTAGE = 0

TEXT = TEXT-007

EXCEPTIONS

OTHERS = 1.

  • start Excel

CREATE OBJECT H_EXCEL 'EXCEL.APPLICATION'.

  • PERFORM ERR_HDL.

SET PROPERTY OF H_EXCEL 'Visible' = 1.

  • CALL METHOD OF H_EXCEL 'FILESAVEAS' EXPORTING #1 = 'c:\kis_excel.xls'

.

  • PERFORM ERR_HDL.

  • tell user what is going on

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

  • PERCENTAGE = 0

TEXT = TEXT-008

EXCEPTIONS

OTHERS = 1.

  • get list of workbooks, initially empty

CALL METHOD OF H_EXCEL 'Workbooks' = H_MAPL.

PERFORM ERR_HDL.

  • add a new workbook

CALL METHOD OF H_MAPL 'Add' = H_MAP.

PERFORM ERR_HDL.

  • tell user what is going on

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

  • PERCENTAGE = 0

TEXT = TEXT-009

EXCEPTIONS

OTHERS = 1.

  • output column headings to active Excel sheet

PERFORM FILL_CELL USING 1 1 1 'Flug'(001).

PERFORM FILL_CELL USING 1 2 0 'Nr'(002).

PERFORM FILL_CELL USING 1 3 1 'Von'(003).

PERFORM FILL_CELL USING 1 4 1 'Nach'(004).

PERFORM FILL_CELL USING 1 5 1 'Zeit'(005).

LOOP AT IT_SPFLI.

  • copy flights to active EXCEL sheet

H = SY-TABIX + 1.

PERFORM FILL_CELL USING H 1 0 IT_SPFLI-CARRID.

PERFORM FILL_CELL USING H 2 0 IT_SPFLI-CONNID.

PERFORM FILL_CELL USING H 3 0 IT_SPFLI-CITYFROM.

PERFORM FILL_CELL USING H 4 0 IT_SPFLI-CITYTO.

PERFORM FILL_CELL USING H 5 0 IT_SPFLI-DEPTIME.

ENDLOOP.

  • changes by Kishore - start

  • CALL METHOD OF H_EXCEL 'Workbooks' = H_MAPL.

CALL METHOD OF H_EXCEL 'Worksheets' = H_MAPL." EXPORTING #1 = 2.

PERFORM ERR_HDL.

  • add a new workbook

CALL METHOD OF H_MAPL 'Add' = H_MAP EXPORTING #1 = 2.

PERFORM ERR_HDL.

  • tell user what is going on

SET PROPERTY OF H_MAP 'NAME' = 'COPY'.

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

  • PERCENTAGE = 0

TEXT = TEXT-009

EXCEPTIONS

OTHERS = 1.

  • output column headings to active Excel sheet

PERFORM FILL_CELL USING 1 1 1 'Flug'(001).

PERFORM FILL_CELL USING 1 2 0 'Nr'(002).

PERFORM FILL_CELL USING 1 3 1 'Von'(003).

PERFORM FILL_CELL USING 1 4 1 'Nach'(004).

PERFORM FILL_CELL USING 1 5 1 'Zeit'(005).

LOOP AT IT_SPFLI.

  • copy flights to active EXCEL sheet

H = SY-TABIX + 1.

PERFORM FILL_CELL USING H 1 0 IT_SPFLI-CARRID.

PERFORM FILL_CELL USING H 2 0 IT_SPFLI-CONNID.

PERFORM FILL_CELL USING H 3 0 IT_SPFLI-CITYFROM.

PERFORM FILL_CELL USING H 4 0 IT_SPFLI-CITYTO.

PERFORM FILL_CELL USING H 5 0 IT_SPFLI-DEPTIME.

ENDLOOP.

  • changes by Kishore - end

  • disconnect from Excel

  • CALL METHOD OF H_EXCEL 'FILESAVEAS' EXPORTING #1 = 'C:\SKV.XLS'.

FREE OBJECT H_EXCEL.

PERFORM ERR_HDL.

----


  • FORM FILL_CELL *

----


  • sets cell at coordinates i,j to value val boldtype bold *

----


FORM FILL_CELL USING I J BOLD VAL.

CALL METHOD OF H_EXCEL 'Cells' = H_ZL EXPORTING #1 = I #2 = J.

PERFORM ERR_HDL.

SET PROPERTY OF H_ZL 'Value' = VAL .

PERFORM ERR_HDL.

GET PROPERTY OF H_ZL 'Font' = H_F.

PERFORM ERR_HDL.

SET PROPERTY OF H_F 'Bold' = BOLD .

PERFORM ERR_HDL.

ENDFORM.

&----


*& Form ERR_HDL

&----


  • outputs OLE error if any *

----


  • --> p1 text

  • <-- p2 text

----


FORM ERR_HDL.

IF SY-SUBRC <> 0.

WRITE: / 'Fehler bei OLE-Automation:'(010), SY-SUBRC.

STOP.

ENDIF.

ENDFORM. " ERR_HDL

Please note that this example maybe slow at filling the excel table

(perhaps four fields per second on a 900 MHz machine - almost 30 seconds

for a short example).

To get the data on properties and methods - there is a bit of smoke and mirrors

going on here; they are EXCEL properties and methods, not sap ones - so you need

to look at excel help to determine how a particular function is structured. then

build the block in sap, as shown in the example.

If you only want to transfer the data to Excel like when you transfer the data from

ALV to Excel simply use the Function Modules:

XXL_SIMPLE_API

If you want more modifications when you transfer it to Excel use:

XXL_FULL_API

Former Member
0 Kudos

Often we face situations where we need to download internal table contents onto an Excel sheet. We are familiar with the function module WS_DOWNLOAD. Though this function module downloads the contents onto the Excel sheet, there cannot be any column headings or we cannot differentiate the primary keys just by seeing the Excel sheet. For this purpose, we can use the function module XXL_FULL_API. The Excel sheet which is generated by this function module contains the column headings and the key columns are highlighted with a different color. Other options that are available with this function module are we can swap two columns or supress a field from displaying on the Excel sheet. The simple code for the usage of this function module is given below.

Code

Program code :

-


REPORT Excel.

TABLES:

sflight.

  • header data................................

DATA :

header1 LIKE gxxlt_p-text VALUE 'Raj',

header2 LIKE gxxlt_p-text VALUE 'Excel sheet'.

  • Internal table for holding the SFLIGHT data

DATA BEGIN OF t_sflight OCCURS 0.

INCLUDE STRUCTURE sflight.

DATA END OF t_sflight.

  • Internal table for holding the horizontal key.

DATA BEGIN OF t_hkey OCCURS 0.

INCLUDE STRUCTURE gxxlt_h.

DATA END OF t_hkey .

  • Internal table for holding the vertical key.

DATA BEGIN OF t_vkey OCCURS 0.

INCLUDE STRUCTURE gxxlt_v.

DATA END OF t_vkey .

  • Internal table for holding the online text....

DATA BEGIN OF t_online OCCURS 0.

INCLUDE STRUCTURE gxxlt_o.

DATA END OF t_online.

  • Internal table to hold print text.............

DATA BEGIN OF t_print OCCURS 0.

INCLUDE STRUCTURE gxxlt_p.

DATA END OF t_print.

  • Internal table to hold SEMA data..............

DATA BEGIN OF t_sema OCCURS 0.

INCLUDE STRUCTURE gxxlt_s.

DATA END OF t_sema.

  • Retreiving data from sflight.

SELECT * FROM sflight

INTO TABLE t_sflight.

  • Text which will be displayed online is declared here....

t_online-line_no = '1'.

t_online-info_name = 'Created by'.

t_online-info_value = 'Raj'.

APPEND t_online.

  • Text which will be printed out..........................

t_print-hf = 'H'.

t_print-lcr = 'L'.

t_print-line_no = '1'.

t_print-text = 'This is the header'.

APPEND t_print.

t_print-hf = 'F'.

t_print-lcr = 'C'.

t_print-line_no = '1'.

t_print-text = 'This is the footer'.

APPEND t_print.

  • Defining the vertical key columns.......

t_vkey-col_no = '1'.

t_vkey-col_name = 'MANDT'.

APPEND t_vkey.

t_vkey-col_no = '2'.

t_vkey-col_name = 'CARRID'.

APPEND t_vkey.

t_vkey-col_no = '3'.

t_vkey-col_name = 'CONNID'.

APPEND t_vkey.

t_vkey-col_no = '4'.

t_vkey-col_name = 'FLDATE'.

APPEND t_vkey.

  • Header text for the data columns................

t_hkey-row_no = '1'.

t_hkey-col_no = 1.

t_hkey-col_name = 'PRICE'.

APPEND t_hkey.

t_hkey-col_no = 2.

t_hkey-col_name = 'CURRENCY'.

APPEND t_hkey.

t_hkey-col_no = 3.

t_hkey-col_name = 'PLANETYPE'.

APPEND t_hkey.

t_hkey-col_no = 4.

t_hkey-col_name = 'SEATSMAX'.

APPEND t_hkey.

t_hkey-col_no = 5.

t_hkey-col_name = 'SEATSOCC'.

APPEND t_hkey.

t_hkey-col_no = 6.

t_hkey-col_name = 'PAYMENTSUM'.

APPEND t_hkey.

  • populating the SEMA data.......................... t_sema-col_no = 1. t_sema-col_typ = 'STR'. t_sema-col_ops = 'DFT'. APPEND t_sema.

t_sema-col_no = 2.

APPEND t_sema.

t_sema-col_no = 3.

APPEND t_sema.

t_sema-col_no = 4.

APPEND t_sema.

t_sema-col_no = 5.

APPEND t_sema.

t_sema-col_no = 6.

APPEND t_sema.

t_sema-col_no = 7.

APPEND t_sema.

t_sema-col_no = 8.

APPEND t_sema.

t_sema-col_no = 9.

APPEND t_sema.

t_sema-col_no = 10.

t_sema-col_typ = 'NUM'.

t_sema-col_ops = 'ADD'.

APPEND t_sema.

CALL FUNCTION 'XXL_FULL_API'

EXPORTING

  • DATA_ENDING_AT = 54

  • DATA_STARTING_AT = 5

filename = 'TESTFILE'

header_1 = header1

header_2 = header2

no_dialog = 'X'

no_start = ' '

n_att_cols = 6

n_hrz_keys = 1

n_vrt_keys = 4

sema_type = 'X'

  • SO_TITLE = ' '

TABLES

data = t_sflight

hkey = t_hkey

online_text = t_online

print_text = t_print

sema = t_sema

vkey = t_vkey

EXCEPTIONS

cancelled_by_user = 1

data_too_big = 2

dim_mismatch_data = 3

dim_mismatch_sema = 4

dim_mismatch_vkey = 5

error_in_hkey = 6

error_in_sema = 7

file_open_error = 8

file_write_error = 9

inv_data_range = 10

inv_winsys = 11

inv_xxl = 12

OTHERS = 13

.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

Former Member
0 Kudos

Hi,

use this FM

XXL_FULL_API

Regards,

Vijay

Former Member
0 Kudos

hi...

Use this FM

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

filename = l_fnam

filetype = 'DBF'

TABLES

data_tab = ig_file

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.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Former Member
0 Kudos

Hi Yogesh,

Here is another method:

1) First download the entire excel file.

2) Update the desired worksheet in ABAP, and save the table.

3) Then download the same into a new excel file, overwriting the old one.

I don't have the exact code right now, if you wish I can search for it and paste ....

A constraint will be the existing file size, hope it isnt very very large

Cheers,

Shailesh.

Always provide feedback; it keeps us upbeat and makes us answer even more

0 Kudos

Hi Shailesh,

I have the similar problem. So created the Table, to enter data from excel sheet. Then how do i do it to reteive entered data? After entering data, i want to create the material using them.

Can u give me the code to do it with explanations. I'm new to SAP.

Thanx in advance.

Regards,

Rashika.

kesavadas_thekkillath
Active Contributor
0 Kudos

search scn for OLE Excel download .