0% found this document useful (0 votes)
1K views

Controlling Excel Using OLE Automation in ABAP

The document describes how to export data from an SAP system to an Excel spreadsheet using ABAP. It includes steps to: 1. Create an ABAP program to retrieve sales order data from SAP and store it in an internal table. 2. Use OLE automation to create an Excel application object, add a new workbook, and write the sales order header and line item data to cells while formatting fonts and adding cell borders. 3. Save the workbook locally and close the active window.

Uploaded by

kosalaw
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Controlling Excel Using OLE Automation in ABAP

The document describes how to export data from an SAP system to an Excel spreadsheet using ABAP. It includes steps to: 1. Create an ABAP program to retrieve sales order data from SAP and store it in an internal table. 2. Use OLE automation to create an Excel application object, add a new workbook, and write the sales order header and line item data to cells while formatting fonts and adding cell borders. 3. Save the workbook locally and close the active window.

Uploaded by

kosalaw
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Step 01 – Record Macro

Step 02 – Switch to ABAP

Example:-
*&---------------------------------------------------------------------*
*& Report  ZKWEXCEL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zkwexcel.

TYPE-POOLS ole2 .

TYPES:  BEGIN OF t_vbap,
          vbeln TYPE vbap-vbeln,
          posnr TYPE vbap-posnr,
          kwmeng TYPE vbap-kwmeng,
        END OF t_vbap.

DATA : i_vbap  TYPE TABLE OF t_vbap,
       ls_vbap TYPE t_vbap.

DATA: h_excel_application TYPE ole2_object,        " Excel object
      h_activewindow      TYPE ole2_object,        " Active Window
      h_work_books        TYPE ole2_object,        " List of workbooks
      h_work_book         TYPE ole2_object,        " Workbook
      h_active_cell       TYPE ole2_object,        " Cell
      h_cell_font         TYPE ole2_object,        " Font
      range               TYPE ole2_object,
      h_borders           TYPE ole2_object.

DATA :  lv_row    TYPE i,
        lv_row_h  TYPE i,
        lv_col    TYPE i,
        i_val     TYPE string,
        l_prop    TYPE i.

START-OF-SELECTION.

  SELECT *
    INTO CORRESPONDING FIELDS OF TABLE i_vbap
    UP TO 10 ROWS
    FROM vbap.

END-OF-SELECTION.

  CREATE OBJECT h_excel_application 'EXCEL.APPLICATION'.

* Workbooks.Add
  GET PROPERTY OF h_excel_application 'Workbooks' = h_work_books .
  CALL METHOD OF h_work_books 'Add' = h_work_book .

* Set header values
  lv_row = 1 .
  lv_col = 1 .
  CALL METHOD OF h_excel_application 'Cells' = h_active_cell
    EXPORTING #1 = lv_row #2 = lv_col .

  i_val = 'Sales Order No' .
  SET PROPERTY OF h_active_cell 'FormulaR1C1' = i_val .
  "CALL METHOD OF h_active_cell 'Font' = h_cell_font.
  GET PROPERTY OF h_active_cell 'Font' = h_cell_font.
  SET PROPERTY OF h_cell_font 'Bold' = 1. " Bold
  SET PROPERTY OF h_cell_font 'Underline' = 2.

  lv_row = 1 .
  lv_col = 2 .
  CALL METHOD OF h_excel_application 'Cells' = h_active_cell
    EXPORTING #1 = lv_row #2 = lv_col .

  i_val = 'Line Item' .
  SET PROPERTY OF h_active_cell 'FormulaR1C1' = i_val .
  CALL METHOD OF h_active_cell 'Font' = h_cell_font.
  SET PROPERTY OF h_cell_font 'Bold' = 0. " Bold
  SET PROPERTY OF h_cell_font 'Color' = -16776961.
  SET PROPERTY OF h_cell_font 'TintAndShade' = 0.

  lv_row = 1 .
  lv_col = 3 .
  CALL METHOD OF h_excel_application 'Cells' = h_active_cell
    EXPORTING #1 = lv_row #2 = lv_col .
  i_val = 'Order Quantity' .
  SET PROPERTY OF h_active_cell 'FormulaR1C1' = i_val .
  CALL METHOD OF h_active_cell 'Font' = h_cell_font.
  SET PROPERTY OF h_cell_font 'Bold' = 1. " Bold

  CLEAR: lv_row, lv_row_h.
  lv_row = 1.
  LOOP AT i_vbap INTO ls_vbap.

    lv_row = lv_row + 1 .
    lv_col = 1 .

    AT NEW vbeln.

      CALL METHOD OF h_excel_application 'Cells' = h_active_cell
        EXPORTING #1 = lv_row #2 = lv_col .

      CONCATENATE 'Sales Order No:' ls_vbap-vbeln INTO i_val.
      SET PROPERTY OF h_active_cell 'FormulaR1C1' = i_val .

      CALL METHOD OF h_active_cell 'Font' = h_cell_font.
      SET PROPERTY OF h_cell_font 'Bold' = 1. " Bold

      lv_row = lv_row + 1.

    ENDAT.

    lv_col = 1 .
    CALL METHOD OF h_excel_application 'Cells' = h_active_cell
      EXPORTING #1 = lv_row #2 = lv_col .

    i_val = ls_vbap-vbeln.
    SET PROPERTY OF h_active_cell 'FormulaR1C1' = i_val .

    lv_col = 2 .
    CALL METHOD OF h_excel_application 'Cells' = h_active_cell
      EXPORTING #1 = lv_row #2 = lv_col .

    i_val = ls_vbap-posnr .
    SET PROPERTY OF h_active_cell 'FormulaR1C1' = i_val .

    lv_col = 3 .
    CALL METHOD OF h_excel_application 'Cells' = h_active_cell
      EXPORTING #1 = lv_row #2 = lv_col .

    i_val = ls_vbap-kwmeng.
    SET PROPERTY OF h_active_cell 'FormulaR1C1' = i_val .

    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 5.
    IF sy-subrc = 0.
      SET PROPERTY OF h_borders 'Linestyle'  = -4142.
    ENDIF.

    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 6.
    SET PROPERTY OF h_borders 'Linestyle'  = -4142.

    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 7.
    SET PROPERTY OF h_borders 'Linestyle'  = -4142.

    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 8.
    SET PROPERTY OF h_borders 'Linestyle'     = 1.
    SET PROPERTY OF h_borders 'ColorIndex'    = 0.
    SET PROPERTY OF h_borders 'TintAndShade'  = 0.
    SET PROPERTY OF h_borders 'Weight'        = 2.

    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 9.
    SET PROPERTY OF h_borders 'Linestyle'     = -4119.
    SET PROPERTY OF h_borders 'ColorIndex'    = 0.
    SET PROPERTY OF h_borders 'TintAndShade'  = 0.
    SET PROPERTY OF h_borders 'Weight'        = 4.

    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 10.
    SET PROPERTY OF h_borders 'Linestyle'  = -4142.

    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 11.
    SET PROPERTY OF h_borders 'Linestyle'  = -4142.

    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 12.
    SET PROPERTY OF h_borders 'Linestyle'  = -4142.

  ENDLOOP.

* Save
  CALL METHOD OF h_work_book 'SaveAs'
    EXPORTING
    #1 = 'C:\3.xls'.

* Close the current window
  GET PROPERTY OF h_excel_application 'ActiveWindow'  = h_activewindow .
  CALL METHOD OF h_activewindow 'Close'.
Tips:

In Excel Shet
    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 5.
    IF sy-subrc = 0.
      SET PROPERTY OF h_borders 'Linestyle'  = -4142.
    ENDIF.
    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 6.
    SET PROPERTY OF h_borders 'Linestyle'  = -4142.
    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 7.
    SET PROPERTY OF h_borders 'Linestyle'  = -4142.
    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 8.
    SET PROPERTY OF h_borders 'Linestyle'     = 1.
    SET PROPERTY OF h_borders 'ColorIndex'    = 0.
    SET PROPERTY OF h_borders 'TintAndShade'  = 0.
    SET PROPERTY OF h_borders 'Weight'        = 2.
    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 9.
    SET PROPERTY OF h_borders 'Linestyle'     = -4119.
    SET PROPERTY OF h_borders 'ColorIndex'    = 0.
    SET PROPERTY OF h_borders 'TintAndShade'  = 0.
    SET PROPERTY OF h_borders 'Weight'        = 4.
    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 10.
    SET PROPERTY OF h_borders 'Linestyle'  = -4142.
    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 11.
    SET PROPERTY OF h_borders 'Linestyle'  = -4142.
    CALL METHOD OF h_active_cell 'Borders' = h_borders
      EXPORTING #1 = 12.
    SET PROPERTY OF h_borders 'Linestyle'  = -4142.

Links

http://www.scribd.com/doc/7202623/MSWord-Excel-With-ABAP#

http://sample-code-abap.blogspot.com/2009/07/controlling-excel-using-ole-
automation.html

http://www.sapfans.com/forums/viewtopic.php?
f=13&t=226174&start=0&st=0&sk=t&sd=a

http://sapfans.com/forums/viewtopic.php?t=77834

http://sapfans.com/forums/viewtopic.php?t=3195

http://www.sapfans.com/forums/viewtopic.php?t=29911&

http://sapfans.com/forums/viewtopic.php?t=60287

http://www.sapfans.com/forums/viewtopic.php?
f=13&t=33400&start=0&st=0&sk=t&sd=a

You might also like