메뉴 건너뛰기

SAP 한국 커뮤니티

엑셀로 데이터 내보내기 샘플 코드

sapjoy 2007.01.26 21:12 조회 수 : 5215 추천:33

*&---------------------------------------------------------------------*
*& Report  Z_EXCEL_FORM3                                               *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  z_excel_form3                                               .
TABLES: kna1, brfckna1.

PARAMETERS: kunnr LIKE kna1-kunnr DEFAULT   '1'.


*

* Internal table customer_data with structure of BRFCKNA1

*

DATA: BEGIN OF customer_data OCCURS 0.

        INCLUDE STRUCTURE brfckna1.

DATA: END OF customer_data.

*

* OLE-specific include file

*

INCLUDE ole2incl.       " OLE-specific include file


*

* Define OLE objects of type OLE_OBJEKT

*

DATA: excel TYPE ole2_object.         " EXCEL object

DATA: books TYPE ole2_object.         " collection of books

DATA: book TYPE ole2_object.          " book

DATA: cell TYPE ole2_object.          " cell



*

* Loop counter: Start value --->        1st output line in EXCEL table

*

DATA: i TYPE i VALUE '4'.

CREATE OBJECT excel 'EXCEL.APPLICATION'.

CALL METHOD OF excel 'WORKBOOKS' = books.

CALL METHOD OF books 'ADD' = book.

CALL FUNCTION 'RFC_CUSTOMER_GET'
  EXPORTING
    kunnr             = kunnr
  TABLES
    customer_t        = customer_data
  EXCEPTIONS
    nothing_specified = 1
    no_record_found   = 2
    OTHERS            = 3.


IF sy-subrc <> 0.

  WRITE: / text-001.        "Call error !

  EXIT.

ENDIF.



* Fill the second line on EXCEL worksheet with header line info

*

PERFORM fill_cell USING  2  1  'Number'.

PERFORM fill_cell USING  2  2  'Form'.

PERFORM fill_cell USING  2  3  'Name'.

PERFORM fill_cell USING  2  4  'POBox'.

PERFORM fill_cell USING  2  5  'Street'.

PERFORM fill_cell USING  2  6  'ZIP'.

PERFORM fill_cell USING  2  7  'City'.

PERFORM fill_cell USING  2  8  'Tel.No'.

PERFORM fill_cell USING  2  9  'Tel.Fax'.



LOOP AT customer_data.

  PERFORM fill_cell USING  i  1  customer_data-kunnr.

  PERFORM fill_cell USING  i  2  customer_data-anred.

  PERFORM fill_cell USING  i  3  customer_data-name1.

  PERFORM fill_cell USING  i  4  customer_data-pfach.

  PERFORM fill_cell USING  i  5  customer_data-stras.

  PERFORM fill_cell USING  i  6  customer_data-pstlz.

  PERFORM fill_cell USING  i  7  customer_data-ort01.

  PERFORM fill_cell USING  i  8  customer_data-telf1.

  PERFORM fill_cell USING  i  9  customer_data-telfx.

  i  =  i  +  1.

ENDLOOP.

SET  PROPERTY  OF  excel  'VISIBLE'  =  1.

*&--------------------------------------------------------------------*
*&      Form  fill_cell
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*      -->I          text
*      -->J          text
*      -->WERTE      text
*---------------------------------------------------------------------*
FORM fill_cell USING i j werte.

  CALL METHOD OF excel 'CELLS' = cell
    EXPORTING
      #1 = i
      #2 = j.

  SET PROPERTY OF cell 'VALUE' =   werte.

ENDFORM.                    "fill_cell


*