Wednesday, September 11, 2013

Download Excel with Headings and Formatting

I have a requirement to download internal table contents to excel with field headings with many formatting. Here is the sample code. Hope it can help you too :)

CREATE OBJECT H_EXCEL 'EXCEL.APPLICATION'.
  SET PROPERTY OF H_EXCEL  'Visible' 1.
  PERFORM ERR_HDL.
  CALL METHOD OF H_EXCEL 'Workbooks' H_MAPL.
  PERFORM ERR_HDL.
  CALL METHOD OF H_MAPL 'Add' H_MAP.
  PERFORM ERR_HDL.
* tell user what is going on
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
     EXPORTING
*           PERCENTAGE = 0
           TEXT       TEXT-009
       EXCEPTIONS
            OTHERS     1.
* tell user what is going on
  SET PROPERTY OF H_MAP 'NAME' 'COPY'.
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
     EXPORTING
*           PERCENTAGE = 0
           TEXT       TEXT-010
       EXCEPTIONS
            OTHERS     1.

LOOP AT t_dwld.
  CALL METHOD OF H_EXCEL 'Cells' H_ZL EXPORTING #1 #2 J.
  PERFORM ERR_HDL.
  SET PROPERTY OF H_ZL 'Value' VAL .
  PERFORM ERR_HDL.
  GET PROPERTY OF H_ZL 'Font' H_F.
  PERFORM ERR_HDL.
  SET PROPERTY OF H_F 'Bold' BOLD .
  PERFORM ERR_HDL.
  CALL METHOD OF h_zl 'Interior' h_i.
  PERFORM ERR_HDL.
  SET PROPERTY OF h_i 'Color' 866677777
  PERFORM ERR_HDL.
  FREE OBJECT H_EXCEL.
  PERFORM ERR_HDL.
  if sy-subrc 0.
    message 'Download completed. Save excel to your local folder.' type 'I'.
  endif.ENDLOOP.

with 
FORM ERR_HDL.
  IF SY-SUBRC <> 0.
    WRITE'Fehler bei OLE-Automation:'(010), SY-SUBRC.
    STOP.
  ENDIF.
ENDFORM.

1 comment:

  1. Nice sharing, keep on writing Ce'

    Pls, visit to my blog http:// menetes.blogspot.com

    ReplyDelete