Wednesday, December 29, 2010
Summary
Normally, we use the Function module WS_EXCEL to download the data into an excel sheet, but sometimes we may require to download the different data in to an excel with more than one sheet, in that case we can make use of the OLE concept to achieve this one. Use the Class cl_gui_frontend_services,In this class use the method clipboard_export.

Original SDN Link


Creating Excel with More than one page

Sample program
 *&---------------------------------------------------------------------*
*& Report ZETA_EXCEL_DOWNLOAD_CLIPBOARD *
*& *
*&---------------------------------------------------------------------*
REPORT zeta_excel_download_clipboard .
INCLUDE ole2incl.
.
DATA: w_cell1 TYPE ole2_object,
w_cell2 TYPE ole2_object.
*--- Ole data Declarations
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
gs_interior TYPE ole2_object, " Pattern
worksheet   TYPE ole2_object,
h_cell      TYPE ole2_object,
h_cell1     TYPE ole2_object,
range       TYPE ole2_object,
h_sheet2    TYPE ole2_object,
h_sheet3    TYPE ole2_object,
gs_font     TYPE ole2_object,
flg_stop(1) TYPE c.
*********************************************************************
** Internal table Declaration
*********************************************************************
DATA:   BEGIN OF t_excel OCCURS 0,
vkorg(20)  TYPE c, "Sales Org
vbtyp(20)  TYPE c, "Document Category
auart(20)  TYPE c, "Document Type
ernam(20)  TYPE c, "Created By
vbeln(20)  TYPE c, "Document Number
posnr(20)  TYPE c, "Item Number
erdat(20)  TYPE c, "Created Date
vdatu(20)  TYPE c, "Header Requested Delivery Date
reqdat(20) TYPE c, "Request date
condat(20) TYPE c, "Confirm date
lifsk(20)  TYPE c, "Header Block
txt30(30)  TYPE c, "Order User Status Description
lifsp(20)  TYPE c, "Line Block
dispo(20)  TYPE c, "MRP Controller
dsnam(20)  TYPE c, "MRP Controller Description
vmsta(20)  TYPE c, "Material Sales Status
kunnr(20)  TYPE c, "Sold To
cname(35)  TYPE c, "Sold To Name
regio(20)  TYPE c, "State
cufd(10)   TYPE c, "CUD
bstnk(20)  TYPE c, "PO#
bsark(20)  TYPE c, "Ordering Method
matnr(20)  TYPE c, "Material
maktx(35)  TYPE c, "Material Description
t200(20)   TYPE c,                                            "T200
vtext(20)  TYPE c, "T200 Description
matkl(20)  TYPE c, "Material Group
zzbomind(7) TYPE c, "BOM Indicator
ostat(20)  TYPE c, "Order Status
cmgst(20)  TYPE c, "CRD
inco1(20)  TYPE c, "Incoterms
oqty(20)   TYPE c, "Order Quantity
pqty(20)   TYPE c, "Open Quantity
unit(20)   TYPE c, "UOM
onet(20)   TYPE c, "Order Value
pnet(20)   TYPE c, "Open Value
curr(20)   TYPE c, "Currency key
so_bezei   LIKE tvkbt-bezei,"Sales Office
sg_bezei   LIKE tvgrt-bezei,"Sales Group
bname(20)  TYPE c, "Ordering Party
contact(20) TYPE c, "Contact Name
telf1(20)  TYPE c, "Contact telf1
reqqty(20) TYPE c, "Item Request qty
reqval(20) TYPE c, "Item Request value
conqty(20) TYPE c, "Item Confirm qty
conval(20) TYPE c, "Item Confirm value
zzrev(02)  TYPE c, "Revenue recognition acceptance
bezei(20)  TYPE c, "Revenue recognition text
vgbel(20)  TYPE c, "Reference Order for RETURNS
0008text(255) TYPE c, "Internal Order Comment Text
END OF t_excel.
DATA:
t_excel_bckord LIKE t_excel OCCURS 0 WITH HEADER LINE,
t_excel_bcklog LIKE t_excel OCCURS 0 WITH HEADER LINE,
t_excel_blkord LIKE t_excel OCCURS 0 WITH HEADER LINE.
TYPES: data1(1500) TYPE c,
ty TYPE TABLE OF data1.
DATA:  it TYPE ty WITH HEADER LINE,
it_2 TYPE ty WITH HEADER LINE,
it_3 TYPE ty WITH HEADER LINE,
rec TYPE sy-tfill,
deli(1) TYPE c,
l_amt(18) TYPE c.
DATA: BEGIN OF hex,
tab TYPE x,
END OF hex.
FIELD-SYMBOLS:  .
CONSTANTS cns_09(2) TYPE n VALUE 09.
ASSIGN deli TO  TYPE 'X'.
hex-tab = cns_09.
 = hex-tab.
DATA gv_sheet_name(20) TYPE c .
*----------------------------------------------------------------------*
* M A C R O Declaration
*----------------------------------------------------------------------*
DEFINE ole_check_error.
if &1 ne 0.
message e001(zz) with &1.
exit.
endif.
END-OF-DEFINITION.
t_excel_bckord-vkorg = 'ABC'.
t_excel_bckord-vbtyp = 'DEF'.
t_excel_bckord-auart = 'GHI'.
t_excel_bckord-ernam = 'JKL'.
t_excel_bckord-vbeln = 'MNO'.
t_excel_bckord-0008text = 'XYZ'.
APPEND t_excel_bckord.
t_excel_bckord-vkorg = 'ABC1'.
t_excel_bckord-vbtyp = 'DEF1'.
t_excel_bckord-auart = 'GHI1'.
t_excel_bckord-ernam = 'JKL1'.
t_excel_bckord-vbeln = 'MNO1'.
t_excel_bckord-0008text = 'XYZ1'.
APPEND t_excel_bckord.
t_excel_bckord-vkorg = 'ABC2'.
t_excel_bckord-vbtyp = 'DEF2'.
t_excel_bckord-auart = 'GHI2'.
t_excel_bckord-ernam = 'JKL2'.
t_excel_bckord-vbeln = 'MNO2'.
t_excel_bckord-0008text = 'XYZ2'.
APPEND t_excel_bckord.
t_excel_bcklog-vkorg = 'ABC'.
t_excel_bcklog-vbtyp = 'DEF'.
t_excel_bcklog-auart = 'GHI'.
t_excel_bcklog-ernam = 'JKL'.
t_excel_bcklog-vbeln = 'MNO'.
t_excel_bcklog-0008text = 'XYZ'.
APPEND t_excel_bcklog.
t_excel_bcklog-vkorg = 'ABC1'.
t_excel_bcklog-vbtyp = 'DEF1'.
t_excel_bcklog-auart = 'GHI1'.
t_excel_bcklog-ernam = 'JKL1'.
t_excel_bcklog-vbeln = 'MNO1'.
t_excel_bcklog-0008text = 'XYZ1'.
APPEND t_excel_bcklog.
t_excel_bcklog-vkorg = 'ABC2'.
t_excel_bcklog-vbtyp = 'DEF2'.
t_excel_bcklog-auart = 'GHI2'.
t_excel_bcklog-ernam = 'JKL2'.
t_excel_bcklog-vbeln = 'MNO2'.
t_excel_bcklog-0008text = 'XYZ2'.
APPEND t_excel_bcklog.
t_excel_bcklog-vkorg = 'ABC3'.
t_excel_bcklog-vbtyp = 'DEF3'..
t_excel_bcklog-auart = 'GHI3'.
t_excel_bcklog-ernam = 'JKL3'.
t_excel_bcklog-vbeln = 'MNO3'.
t_excel_bcklog-0008text = 'XYZ3'.
APPEND t_excel_bcklog.
t_excel_blkord-vkorg = 'ABC'.
t_excel_blkord-vbtyp = 'DEF'.
t_excel_blkord-auart = 'GHI'.
t_excel_blkord-ernam = 'JKL'.
t_excel_blkord-vbeln = 'MNO'.
t_excel_blkord-0008text = 'XYZ'.
APPEND t_excel_blkord.
t_excel_blkord-vkorg = 'ABC1'.
t_excel_blkord-vbtyp = 'DEF1'.
t_excel_blkord-auart = 'GHI1'.
t_excel_blkord-ernam = 'JKL1'.
t_excel_blkord-vbeln = 'MNO1'.
t_excel_blkord-0008text = 'XYZ1'.
APPEND t_excel_blkord.
t_excel_blkord-vkorg = 'ABC2'.
t_excel_blkord-vbtyp = 'DEF2'.
t_excel_blkord-auart = 'GHI2'.
t_excel_blkord-ernam = 'JKL2'.
t_excel_blkord-vbeln = 'MNO2'.
t_excel_blkord-0008text = 'XYZ2'.
APPEND t_excel_blkord.
t_excel_blkord-vkorg = 'ABC3'.
t_excel_blkord-vbtyp = 'DEF3'..
t_excel_blkord-auart = 'GHI3'.
t_excel_blkord-ernam = 'JKL3'.
t_excel_blkord-vbeln = 'MNO3'.
t_excel_blkord-0008text = 'XYZ3'.
APPEND t_excel_blkord.
t_excel_blkord-vkorg = 'ABC4'.
t_excel_blkord-vbtyp = 'DEF4'..
t_excel_blkord-auart = 'GHI4'.
t_excel_blkord-ernam = 'JKL4'.
t_excel_blkord-vbeln = 'MNO4'.
t_excel_blkord-0008text = 'XYZ4'.
APPEND t_excel_blkord.
LOOP AT t_excel_bckord.
CONCATENATE
t_excel_bckord-vkorg
t_excel_bckord-vbtyp
t_excel_bckord-auart
t_excel_bckord-ernam
t_excel_bckord-vbeln
t_excel_bckord-posnr
t_excel_bckord-erdat
t_excel_bckord-vdatu
t_excel_bckord-reqdat
t_excel_bckord-condat
t_excel_bckord-lifsk
t_excel_bckord-txt30
t_excel_bckord-lifsp
t_excel_bckord-dispo
t_excel_bckord-dsnam
t_excel_bckord-vmsta
t_excel_bckord-kunnr
t_excel_bckord-cname
t_excel_bckord-regio
t_excel_bckord-cufd
t_excel_bckord-bstnk
t_excel_bckord-bsark
t_excel_bckord-matnr
t_excel_bckord-maktx
t_excel_bckord-t200
t_excel_bckord-vtext
t_excel_bckord-matkl
t_excel_bckord-zzbomind
t_excel_bckord-ostat
t_excel_bckord-cmgst
t_excel_bckord-inco1
t_excel_bckord-oqty
t_excel_bckord-pqty
t_excel_bckord-unit
t_excel_bckord-onet
t_excel_bckord-pnet
t_excel_bckord-curr
t_excel_bckord-so_bezei
t_excel_bckord-sg_bezei
t_excel_bckord-bname
t_excel_bckord-contact
t_excel_bckord-telf1
t_excel_bckord-reqqty
t_excel_bckord-reqval
t_excel_bckord-conqty
t_excel_bckord-conval
t_excel_bckord-zzrev
t_excel_bckord-bezei
t_excel_bckord-vgbel
t_excel_bckord-0008text
INTO it
SEPARATED BY deli.
APPEND it.
CLEAR it.
ENDLOOP.
LOOP AT t_excel_bcklog.
CONCATENATE
t_excel_bcklog-vkorg
t_excel_bcklog-vbtyp
t_excel_bcklog-auart
t_excel_bcklog-ernam
t_excel_bcklog-vbeln
t_excel_bcklog-posnr
t_excel_bcklog-erdat
t_excel_bcklog-vdatu
t_excel_bcklog-reqdat
t_excel_bcklog-condat
t_excel_bcklog-lifsk
t_excel_bcklog-txt30
t_excel_bcklog-lifsp
t_excel_bcklog-dispo
t_excel_bcklog-dsnam
t_excel_bcklog-vmsta
t_excel_bcklog-kunnr
t_excel_bcklog-cname
t_excel_bcklog-regio
t_excel_bcklog-cufd
t_excel_bcklog-bstnk
t_excel_bcklog-bsark
t_excel_bcklog-matnr
t_excel_bcklog-maktx
t_excel_bcklog-t200
t_excel_bcklog-vtext
t_excel_bcklog-matkl
t_excel_bcklog-zzbomind
t_excel_bcklog-ostat
t_excel_bcklog-cmgst
t_excel_bcklog-inco1
t_excel_bcklog-oqty
t_excel_bcklog-pqty
t_excel_bcklog-unit
t_excel_bcklog-onet
t_excel_bcklog-pnet
t_excel_bcklog-curr
t_excel_bcklog-so_bezei
t_excel_bcklog-sg_bezei
t_excel_bcklog-bname
t_excel_bcklog-contact
t_excel_bcklog-telf1
t_excel_bcklog-reqqty
t_excel_bcklog-reqval
t_excel_bcklog-conqty
t_excel_bcklog-conval
t_excel_bcklog-zzrev
t_excel_bcklog-bezei
t_excel_bcklog-vgbel
t_excel_bcklog-0008text
INTO it_2
SEPARATED BY deli.
APPEND it_2.
CLEAR it_2.
ENDLOOP.
LOOP AT t_excel_blkord.
CONCATENATE
t_excel_blkord-vkorg
t_excel_blkord-vbtyp
t_excel_blkord-auart
t_excel_blkord-ernam
t_excel_blkord-vbeln
t_excel_blkord-posnr
t_excel_blkord-erdat
t_excel_blkord-vdatu
t_excel_blkord-reqdat
t_excel_blkord-condat
t_excel_blkord-lifsk
t_excel_blkord-txt30
t_excel_blkord-lifsp
t_excel_blkord-dispo
t_excel_blkord-dsnam
t_excel_blkord-vmsta
t_excel_blkord-kunnr
t_excel_blkord-cname
t_excel_blkord-regio
t_excel_blkord-cufd
t_excel_blkord-bstnk
t_excel_blkord-bsark
t_excel_blkord-matnr
t_excel_blkord-maktx
t_excel_blkord-t200
t_excel_blkord-vtext
t_excel_blkord-matkl
t_excel_blkord-zzbomind
t_excel_blkord-ostat
t_excel_blkord-cmgst
t_excel_blkord-inco1
t_excel_blkord-oqty
t_excel_blkord-pqty
t_excel_blkord-unit
t_excel_blkord-onet
t_excel_blkord-pnet
t_excel_blkord-curr
t_excel_blkord-so_bezei
t_excel_blkord-sg_bezei
t_excel_blkord-bname
t_excel_blkord-contact
t_excel_blkord-telf1
t_excel_blkord-reqqty
t_excel_blkord-reqval
t_excel_blkord-conqty
t_excel_blkord-conval
t_excel_blkord-zzrev
t_excel_blkord-bezei
t_excel_blkord-vgbel
t_excel_blkord-0008text
INTO it_3
SEPARATED BY deli.
APPEND it_3.
CLEAR it_3.
ENDLOOP.
* start Excel
IF h_excel-header = space OR h_excel-handle = -1.
CREATE OBJECT h_excel 'EXCEL.APPLICATION'.
ENDIF.
*--- get list of workbooks, initially empty
CALL METHOD OF h_excel 'Workbooks' = h_mapl.
SET PROPERTY OF h_excel 'Visible' = 1.
CALL METHOD OF h_mapl 'Add' = h_map.
gv_sheet_name = 'Back Orders'.
GET PROPERTY OF h_excel 'ACTIVESHEET' = worksheet.
SET PROPERTY OF worksheet 'Name' = gv_sheet_name .
*--Formatting the area of additional data 1 and doing the BOLD
CALL METHOD OF h_excel 'Cells' = w_cell1
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF h_excel 'Cells' = w_cell2
EXPORTING
#1 = 1
#2 = 50.
CALL METHOD OF h_excel 'Range' = h_cell
EXPORTING
#1 = w_cell1
#2 = w_cell2.
GET PROPERTY OF h_cell 'Font' = gs_font .
SET PROPERTY OF gs_font 'Bold' = 1 .
DATA l_rc TYPE i.
CALL METHOD cl_gui_frontend_services=>clipboard_export
IMPORTING
data                 = it[]
CHANGING
rc                   = l_rc
EXCEPTIONS
cntl_error           = 1
error_no_gui         = 2
not_supported_by_gui = 3
OTHERS               = 4.
CALL METHOD OF h_excel 'Cells' = w_cell1
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF h_excel 'Cells' = w_cell2
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF h_excel 'Range' = range
EXPORTING
#1 = w_cell1
#2 = w_cell2.
CALL METHOD OF range 'Select'.
CALL METHOD OF worksheet 'Paste'.
gv_sheet_name = 'Backlog'.
GET PROPERTY OF h_excel 'Sheets' = h_sheet2 .
CALL METHOD OF h_sheet2 'Add' = h_map.
SET PROPERTY OF h_map 'Name' = gv_sheet_name .
GET PROPERTY OF h_excel 'ACTIVESHEET' = worksheet.
*--Formatting the area of additional data 1 and doing the BOLD
CALL METHOD OF h_excel 'Cells' = w_cell1
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF h_excel 'Cells' = w_cell2
EXPORTING
#1 = 1
#2 = 50.
CALL METHOD OF h_excel 'Range' = h_cell
EXPORTING
#1 = w_cell1
#2 = w_cell2.
GET PROPERTY OF h_cell 'Font' = gs_font .
SET PROPERTY OF gs_font 'Bold' = 1 .
CALL METHOD cl_gui_frontend_services=>clipboard_export
IMPORTING
data                 = it_2[]
CHANGING
rc                   = l_rc
EXCEPTIONS
cntl_error           = 1
error_no_gui         = 2
not_supported_by_gui = 3
OTHERS               = 4.
CALL METHOD OF h_excel 'Cells' = w_cell1
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF h_excel 'Cells' = w_cell2
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF h_excel 'Range' = range
EXPORTING
#1 = w_cell1
#2 = w_cell2.
CALL METHOD OF range 'Select'.
CALL METHOD OF worksheet 'Paste'.
gv_sheet_name = 'Blocked Orders'.
GET PROPERTY OF h_excel 'Sheets' = h_sheet3 .
CALL METHOD OF h_sheet3 'Add' = h_map.
SET PROPERTY OF h_map 'Name' = gv_sheet_name .
GET PROPERTY OF h_excel 'ACTIVESHEET' = worksheet.
*--Formatting the area of additional data 1 and doing the BOLD
CALL METHOD OF h_excel 'Cells' = w_cell1
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF h_excel 'Cells' = w_cell2
EXPORTING
#1 = 1
#2 = 50.
CALL METHOD OF h_excel 'Range' = h_cell
EXPORTING
#1 = w_cell1
#2 = w_cell2.
GET PROPERTY OF h_cell 'Font' = gs_font .
SET PROPERTY OF gs_font 'Bold' = 1 .
CALL METHOD cl_gui_frontend_services=>clipboard_export
IMPORTING
data                 = it_3[]
CHANGING
rc                   = l_rc
EXCEPTIONS
cntl_error           = 1
error_no_gui         = 2
not_supported_by_gui = 3
OTHERS               = 4.
CALL METHOD OF h_excel 'Cells' = w_cell1
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF h_excel 'Cells' = w_cell2
EXPORTING
#1 = 1
#2 = 1.
CALL METHOD OF h_excel 'Range' = range
EXPORTING
#1 = w_cell1
#2 = w_cell2.
CALL METHOD OF range 'Select'.
CALL METHOD OF worksheet 'Paste'.
*--- disconnect from Excel
FREE OBJECT h_zl.
FREE OBJECT h_mapl.
FREE OBJECT h_map.
FREE OBJECT h_excel.

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.