目次

Excelファイル出力

 

このトピックでは、EXCELファイル出力の方法を取り上げて説明します。

仕組

ABAPプログラムは、ABAP言語のOLE オートメーション技術を利用して、Excelファイルの読み取り、書き込みを行うことができます。 以下の図でその仕組みを示します。

  1. OLEサーバとなるデスクトップアプリケーション(ExcelやWordなど)は、フロントエンドマシンに稼働します。
  2. ABAPプログラムはAS ABAPに稼働します。
  3. OLEサーバを直接アクセスするのは同じフロントエンドマシンに稼働するSAP GUIです。
  4. ABAPプログラムのOLE関連命令はAS ABAPによりRFCを通して、フロントエンドマシンに稼働するSAP GUIと連携されます。

ABAPで制御されるアプリケーションはすべて、ABAPシステムに登録しなければなりません。

ABAP命令

以下のABAP命令でOLEサーバアプリケーションを制御します。

サンプルソース

任意テーブルのデータをExcelファイルへエクスポートするツールをサンプルとして取り上げてそのソースを以下に示します。 動的プログラミングのサンプルソースとしても参考できるものです。

REPORT  Y_XXXXX01.

INCLUDE ole2incl.

TYPES: BEGIN OF s_tabstr,
  fieldname TYPE fieldname,
  as4local TYPE as4local,
  POSITION TYPE tabfdpos,
  keyflag TYPE keyflag,
  rollname TYPE rollname,
  datatype TYPE datatype_d,
  leng TYPE ddleng,
  scrtext_s TYPE scrtext_s,
END OF s_tabstr.

 PARAMETERS: p_table TYPE dd02l-tabname obligatory.
 PARAMETERS: p_file TYPE localfile DEFAULT 'c:\temp\test1.xls'.

TABLES: v_username.


DATA: BEGIN OF i_data OCCURS 0.
  INCLUDE STRUCTURE v_username.
DATA: END OF i_data.


START-OF-SELECTION.

  DATA:
        g_tablename TYPE tabname,
        g_filename TYPE localfile,
        g_itab_header TYPE REF TO DATA,
        g_itab_data TYPE REF TO DATA.

g_tablename = p_table.
g_filename = p_file.


* get table header
PERFORM f_get_tablestruc
              USING g_tablename
              CHANGING  g_itab_header.

* get table data
PERFORM f_extract_tabledata
              USING g_tablename
              CHANGING  g_itab_data.

* create and export data to excel file
PERFORM f_export_to_excel
              USING g_itab_header
                        g_itab_data
                        g_filename.


FORM f_get_tablestruc   USING VALUE(u_tabnam) TYPE tabname
                                       CHANGING c_itab TYPE REF TO DATA.
  DATA:
       v_delm TYPE C,
       str_ty TYPE REF TO cl_abap_structdescr,
        t_cm TYPE cl_abap_structdescr=>component_table,
        s_cm LIKE LINE OF t_cm,
        ref_cm TYPE REF TO DATA,
        f_seq TYPE n length 4,
        tabstr TYPE STANDARD TABLE OF s_tabstr.

  FIELD-SYMBOLS:
  <str> TYPE ANY,
  <cmp> TYPE ANY,
  <tabstr> LIKE LINE OF tabstr,
  <lab> TYPE STANDARD TABLE.

  CLEAR: f_seq, t_cm[].

  MOVE cl_abap_char_utilities=>horizontal_tab TO v_delm.

  SELECT fieldname as4local POSITION keyflag rollname datatype leng scrtext_s
  FROM dd03vt
  INTO TABLE tabstr
  WHERE tabname EQ u_tabnam AND ddlanguage EQ sy-langu.
  IF sy-subrc NE 0.
    MESSAGE e004(zasd1) WITH TEXT-e01 u_tabnam space space.
    STOP.
  ENDIF.
  SORT tabstr BY POSITION ASCENDING.

  LOOP AT tabstr ASSIGNING <cmp>.
    CLEAR s_cm.
    f_seq = f_seq + 1.
    CONCATENATE 'col' f_seq INTO s_cm-name.
    s_cm-type = cl_abap_elemdescr=>get_c( 30 ).
    APPEND s_cm TO t_cm.
  ENDLOOP.

  str_ty = cl_abap_structdescr=>create( t_cm ).

  CREATE DATA ref_cm TYPE handle str_ty.
  ASSIGN ref_cm->* TO <str>.

  CREATE DATA c_itab  LIKE STANDARD TABLE OF <str>.
  ASSIGN c_itab->* TO <lab>.

  CLEAR f_seq.
  DATA w_cl TYPE string.

  LOOP AT tabstr ASSIGNING <tabstr>.
    f_seq = f_seq + 1.
    CLEAR w_cl.
    CONCATENATE 'COL' f_seq INTO w_cl.
    ASSIGN COMPONENT w_cl OF STRUCTURE <str> TO <cmp>.
    <cmp> = <tabstr>-scrtext_s.
  ENDLOOP.

  APPEND <str> TO <lab>.
  CLEAR f_seq.
  LOOP AT tabstr ASSIGNING <tabstr>.
    f_seq = f_seq + 1.
    CLEAR w_cl.
    CONCATENATE 'COL' f_seq INTO w_cl.
    ASSIGN COMPONENT w_cl OF STRUCTURE <str> TO <cmp>.
    <cmp> = <tabstr>-fieldname.
  ENDLOOP.
  APPEND <str> TO <lab>.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.
*
*
FORM f_extract_tabledata   USING VALUE(u_tabnam) TYPE tabname
                                       CHANGING c_itab TYPE REF TO DATA.

  DATA:
        w_file TYPE string,
        tabstr TYPE STANDARD TABLE OF s_tabstr.

  FIELD-symbols:
  <itab>   TYPE STANDARD TABLE.

  CREATE DATA c_itab TYPE STANDARD TABLE OF (u_tabnam).

  ASSIGN c_itab->* TO <itab>.

  SELECT *
  FROM (u_tabnam)
  INTO TABLE <itab>.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM.                    "f_down_proc


FORM f_export_to_excel USING u_itab_header TYPE REF TO DATA
                                             u_itab_data TYPE REF TO DATA
                                             u_filename TYPE localfile.

DATA: w_excel TYPE ole2_object,
      w_workbooks TYPE ole2_object,
      w_workbook TYPE ole2_object,
      w_cell TYPE ole2_object.

DATA: w_row TYPE I,
          w_col  TYPE I,
          type   TYPE C,
          count TYPE I.

  FIELD-SYMBOLS:
  <str> TYPE ANY,
  <cmp> TYPE ANY,
  <fs_itab_header> TYPE STANDARD TABLE,
  <fs_itab_data> TYPE STANDARD TABLE,
  <fs_itab_headrow> TYPE ANY,
  <fs_itab_datarow> TYPE ANY.

* Create OLE automation object of type Excel.Application.
CREATE OBJECT w_excel 'Excel.Application'.

* Add a new Excel Workbook.
CALL METHOD OF w_excel 'Workbooks' = w_workbooks.
CALL METHOD OF w_workbooks 'Add' = w_workbook.

* Display MS-Excel when executing the program.
SET PROPERTY OF w_excel 'Visible' = 1.

* Export title to the Excel sheet.
 ASSIGN u_itab_header->* TO <fs_itab_header>.

 FIELD-SYMBOLS:
  <fs_headrow> TYPE ANY.

w_row = 1.


LOOP AT <fs_itab_header> ASSIGNING <fs_itab_headrow>.
  w_col = 1.
  DESCRIBE FIELD <fs_itab_headrow> TYPE type COMPONENTS count.
  WHILE w_col <= count.
    ASSIGN COMPONENT w_col OF STRUCTURE <fs_itab_headrow> TO <cmp>.
    CALL METHOD OF w_excel 'Cells' = w_cell
       EXPORTING #1 = w_row  #2 = w_col.
    SET PROPERTY OF w_cell 'Value' = <cmp>.

    w_col = w_col + 1.

  ENDWHILE.
  w_row = w_row + 1.

ENDLOOP.


* Export data to the Excel sheet.
 ASSIGN u_itab_data->* TO <fs_itab_data>.

LOOP AT <fs_itab_data> ASSIGNING <fs_itab_datarow>.
  w_col = 1.
  DESCRIBE FIELD <fs_itab_headrow> TYPE type COMPONENTS count.
  WHILE w_col <= count.
    ASSIGN COMPONENT w_col OF STRUCTURE <fs_itab_datarow> TO <cmp>.
    CALL METHOD OF w_excel 'Cells' = w_cell
       EXPORTING #1 = w_row  #2 = w_col.
    SET PROPERTY OF w_cell 'Value' = <cmp>.

    w_col = w_col + 1.

  ENDWHILE.
  w_row = w_row + 1.

ENDLOOP.

* Save file.
CALL METHOD OF w_workbook 'SaveAs'
EXPORTING #1 = u_filename
  #6 = 1.

* Quit Excel.
CALL METHOD OF w_excel 'Quit'.

FREE OBJECT: w_excel,
w_workbooks,
w_workbook,
w_cell.

ENDFORM.