*&---------------------------------------------------------------------*
*& Report ZPBK_TEST3
*&
*&---------------------------------------------------------------------*
*& This is an sample code to have 2 internal tables data in one excel
*& file's 2 sheets
*&---------------------------------------------------------------------*
REPORT zpbk_test3.
INCLUDE ole2incl.
DATA: application TYPE ole2_object,
workbooks TYPE ole2_object,
workbook TYPE ole2_object,
sheet TYPE ole2_object,
sheet2 TYPE ole2_object,
cells TYPE ole2_object.
CONSTANTS: row_max TYPE i VALUE 256.
DATA index TYPE i.
TABLES: vbap,mara.
TYPES: BEGIN OF itab1,
lifnr TYPE lfa1-lifnr,
land1 TYPE lfa1-land1,
END OF itab1,
BEGIN OF itab2,
matnr TYPE mara-matnr,
ersda TYPE mara-ersda,
ernam TYPE mara-ernam,
END OF itab2.
DATA: v_row TYPE sy-tabix.
DATA: itab1 TYPE STANDARD TABLE OF itab1 WITH HEADER LINE,
itab2 TYPE STANDARD TABLE OF itab2 WITH HEADER LINE.
************************************************************************
* START-OF-SELECTION
************************************************************************
START-OF-SELECTION.
SELECT lifnr land1
FROM lfa1
INTO CORRESPONDING FIELDS OF TABLE itab1
UP TO 5 ROWS.
SELECT matnr ersda ernam
FROM mara
INTO CORRESPONDING FIELDS OF
TABLE itab2 UP TO 5 ROWS.
END-OF-SELECTION.
CREATE OBJECT application 'excel.application'.
SET PROPERTY OF application 'visible' = 1.
* Create Empty Workbook
CALL METHOD OF
application
'Workbooks' = workbooks.
* Create First Excel Sheet
CALL METHOD OF
workbooks
'Add' = workbook.
GET PROPERTY OF application 'ACTIVESHEET' = sheet.
SET PROPERTY OF sheet 'Name' = 'LFA1'.
CLEAR v_row.
LOOP AT itab1.
v_row = sy-tabix.
PERFORM fill_cell USING v_row 1 itab1-lifnr.
PERFORM fill_cell USING v_row 2 itab1-land1.
ENDLOOP.
* Create second Excel sheet
GET PROPERTY OF application 'sheets' = sheet2.
CALL METHOD OF
sheet2
'Add' = workbook.
GET PROPERTY OF application 'ACTIVESHEET' = sheet.
SET PROPERTY OF sheet 'Name' = 'MARA'.
CLEAR v_row.
LOOP AT itab2.
v_row = sy-tabix.
PERFORM fill_cell USING v_row 1 itab2-matnr.
PERFORM fill_cell USING v_row 2 itab2-ersda.
PERFORM fill_cell USING v_row 3 itab2-ernam.
ENDLOOP.
* Save excel speadsheet to particular filename
CALL METHOD OF
sheet
'SaveAs'
EXPORTING
#1 = 'c:\temp\exceltest.xls' "filename
#2 = 1. "fileFormat
* Closes excel window, data is lost if not saved
SET PROPERTY OF application 'visible' = 0.
*&---------------------------------------------------------------------*
*& Form fill_cell
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM fill_cell USING row col val.
CALL METHOD OF
sheet
'Cells' = cells
EXPORTING
#1 = row
#2 = col.
SET PROPERTY OF cells 'Value' = val.
ENDFORM. " fill_cell
*& Report ZPBK_TEST3
*&
*&---------------------------------------------------------------------*
*& This is an sample code to have 2 internal tables data in one excel
*& file's 2 sheets
*&---------------------------------------------------------------------*
REPORT zpbk_test3.
INCLUDE ole2incl.
DATA: application TYPE ole2_object,
workbooks TYPE ole2_object,
workbook TYPE ole2_object,
sheet TYPE ole2_object,
sheet2 TYPE ole2_object,
cells TYPE ole2_object.
CONSTANTS: row_max TYPE i VALUE 256.
DATA index TYPE i.
TABLES: vbap,mara.
TYPES: BEGIN OF itab1,
lifnr TYPE lfa1-lifnr,
land1 TYPE lfa1-land1,
END OF itab1,
BEGIN OF itab2,
matnr TYPE mara-matnr,
ersda TYPE mara-ersda,
ernam TYPE mara-ernam,
END OF itab2.
DATA: v_row TYPE sy-tabix.
DATA: itab1 TYPE STANDARD TABLE OF itab1 WITH HEADER LINE,
itab2 TYPE STANDARD TABLE OF itab2 WITH HEADER LINE.
************************************************************************
* START-OF-SELECTION
************************************************************************
START-OF-SELECTION.
SELECT lifnr land1
FROM lfa1
INTO CORRESPONDING FIELDS OF TABLE itab1
UP TO 5 ROWS.
SELECT matnr ersda ernam
FROM mara
INTO CORRESPONDING FIELDS OF
TABLE itab2 UP TO 5 ROWS.
END-OF-SELECTION.
CREATE OBJECT application 'excel.application'.
SET PROPERTY OF application 'visible' = 1.
* Create Empty Workbook
CALL METHOD OF
application
'Workbooks' = workbooks.
* Create First Excel Sheet
CALL METHOD OF
workbooks
'Add' = workbook.
GET PROPERTY OF application 'ACTIVESHEET' = sheet.
SET PROPERTY OF sheet 'Name' = 'LFA1'.
CLEAR v_row.
LOOP AT itab1.
v_row = sy-tabix.
PERFORM fill_cell USING v_row 1 itab1-lifnr.
PERFORM fill_cell USING v_row 2 itab1-land1.
ENDLOOP.
* Create second Excel sheet
GET PROPERTY OF application 'sheets' = sheet2.
CALL METHOD OF
sheet2
'Add' = workbook.
GET PROPERTY OF application 'ACTIVESHEET' = sheet.
SET PROPERTY OF sheet 'Name' = 'MARA'.
CLEAR v_row.
LOOP AT itab2.
v_row = sy-tabix.
PERFORM fill_cell USING v_row 1 itab2-matnr.
PERFORM fill_cell USING v_row 2 itab2-ersda.
PERFORM fill_cell USING v_row 3 itab2-ernam.
ENDLOOP.
* Save excel speadsheet to particular filename
CALL METHOD OF
sheet
'SaveAs'
EXPORTING
#1 = 'c:\temp\exceltest.xls' "filename
#2 = 1. "fileFormat
* Closes excel window, data is lost if not saved
SET PROPERTY OF application 'visible' = 0.
*&---------------------------------------------------------------------*
*& Form fill_cell
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM fill_cell USING row col val.
CALL METHOD OF
sheet
'Cells' = cells
EXPORTING
#1 = row
#2 = col.
SET PROPERTY OF cells 'Value' = val.
ENDFORM. " fill_cell
If Header also required ?
ReplyDelete