EXCEL_DOWNLOAD

FUNCTION Y_BC_ZXK_001.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(PI_EXCELPATH) TYPE  STRING
*"     VALUE(PI_SHEETNAME) TYPE  CHAR20 DEFAULT 'DATA'
*"     VALUE(PI_HEADER) TYPE  STRING OPTIONAL
*"  TABLES
*"      PT_TAB
*"----------------------------------------------------------------------

  DATA:L_MSG TYPE STRING VALUE '下载数据到EXCEL,请等待....'.
  DEFINE ASS_CELLS.
    CALL METHOD OF g_excel 'CELLS' = g_cell   "定义在excel的位置 1为行 2为列
    EXPORTING
      #1 = &1
      #2 = &2.
    PERFORM EXCEL_ERROR.

    SET PROPERTY OF G_cell &3 = &4.   "填充值  3为VALUE 4为填充内容
    PERFORM EXCEL_ERROR.
  END-OF-DEFINITION.


  PERFORM CREATE_APPLICATION.
  PERFORM PROGRESS_INDICATOR  USING L_MSG.
  PERFORM CREATE_WORKBOOK_AND_WORKSHEET USING PI_SHEETNAME .
  PERFORM CELLS_ASSIGN TABLES PT_TAB USING PI_HEADER.
  PERFORM EXCEL_SAVE USING PI_EXCELPATH..
ENDFUNCTION.

FORM PROGRESS_INDICATOR  USING    P_MSG.
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
    EXPORTING
      PERCENTAGE = 0
      TEXT       = P_MSG.
ENDFORM.


FORM CREATE_APPLICATION.
  CREATE OBJECT G_EXCEL 'EXCEL.APPLICATION'.
  PERFORM EXCEL_ERROR.
  SET PROPERTY OF G_EXCEL 'SHEETSINNEWWORKBOOK' = 1."设置 Microsoft Excel 软件打开时,自动插入到新工作簿中的工作表数目(即初始sheet数目,默认名字依次为 Sheet1、Sheet2.....)
  PERFORM EXCEL_ERROR.
  SET PROPERTY OF G_EXCEL 'VISIBLE' = 0."1.前台运行。为0时表示为后台运行
  PERFORM EXCEL_ERROR.
ENDFORM.

FORM CREATE_WORKBOOK_AND_WORKSHEET USING PI_SHEETNAME.
*创建workbook
  CALL METHOD OF G_EXCEL 'WORKBOOKS' = G_WORKBOOK .
  PERFORM EXCEL_ERROR.
  CALL METHOD OF G_WORKBOOK 'ADD'.
  PERFORM EXCEL_ERROR.
*添加worksheet.
  CALL METHOD OF G_EXCEL 'WORKSHEETS' = G_SHEET.
  PERFORM EXCEL_ERROR.
  CALL METHOD OF G_SHEET 'ADD'.
  PERFORM EXCEL_ERROR.
*指定要被操作的sheet.
  CALL METHOD OF G_EXCEL 'WORKSHEETS' = G_SHEET
  EXPORTING
    #1 = 1.  "第几个被操作的sheet或直接写sheet名字
  PERFORM EXCEL_ERROR.
  CALL METHOD OF G_SHEET 'ACTIVATE'.
  PERFORM EXCEL_ERROR.
*sheet重命名
  SET PROPERTY OF G_SHEET 'NAME' = PI_SHEETNAME.  "导出的EXCEL表单名
  PERFORM EXCEL_ERROR.
ENDFORM.

FORM CELLS_ASSIGN TABLES PT_TAB USING PI_HEADER.
*HEADER
  DATA:BEGIN OF LS_HEADER,
       HEADER(20),
       END OF LS_HEADER,
       LT_HEADER LIKE STANDARD TABLE OF LS_HEADER,
       L_HEADER_LINE TYPE I VALUE 0.
  SPLIT PI_HEADER AT '#' INTO TABLE LT_HEADER.
  LOOP AT LT_HEADER INTO LS_HEADER.
    L_HEADER_LINE = L_HEADER_LINE + 1.
    ASS_CELLS 1 L_HEADER_LINE 'VALUE' LS_HEADER-HEADER.
    CLEAR:LS_HEADER   .
  ENDLOOP.
  CLEAR:L_HEADER_LINE.

*ITEMS.
  DATA: L_LINE    TYPE I VALUE 0,
        L_ROW     TYPE I VALUE 1,
        DESCR_REF TYPE REF TO CL_ABAP_STRUCTDESCR.
  FIELD-SYMBOLS: <FS_FIELD> TYPE ABAP_COMPDESCR,
                 <F1>,
                 <F2>.
  DESCR_REF ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( PT_TAB ).
  LOOP AT PT_TAB ASSIGNING <F1>.
    L_ROW = L_ROW + 1.
    LOOP AT DESCR_REF->COMPONENTS ASSIGNING <FS_FIELD>.
      L_LINE = L_LINE + 1.
      ASSIGN COMPONENT <FS_FIELD>-NAME OF STRUCTURE <F1> TO <F2>.
      ASS_CELLS L_ROW L_LINE 'VALUE' <F2>.

    ENDLOOP.
    CLEAR:L_LINE.
  ENDLOOP.
  CLEAR:L_ROW.

ENDFORM.

FORM EXCEL_SAVE USING PI_EXCELPATH.
  GET PROPERTY OF G_EXCEL 'ACTIVESHEET' = G_SHEET."激活工作簿
  PERFORM EXCEL_ERROR.
  GET PROPERTY OF G_EXCEL 'ACTIVEWORKBOOK' = G_WORKBOOK."激活工作区
  PERFORM EXCEL_ERROR.
  CALL METHOD OF G_WORKBOOK 'SAVEAS'
  EXPORTING
    #1 = PI_EXCELPATH
    #2 = 1.
  PERFORM EXCEL_ERROR.
  CALL METHOD OF G_WORKBOOK 'CLOSE'. "关闭工作区
  PERFORM EXCEL_ERROR.
  CALL METHOD OF G_EXCEL 'QUIT'."退出excel
  PERFORM EXCEL_ERROR.
  FREE OBJECT G_SHEET.
  FREE OBJECT G_WORKBOOK.
  FREE OBJECT G_EXCEL.
  FREE OBJECT G_CELL.

ENDFORM.

FORM EXCEL_ERROR .
  IF SY-SUBRC NE 0.
    MESSAGE 'EXCEL导出发生异常' TYPE 'E'.
    STOP.
  ENDIF.
ENDFORM.

推荐阅读更多精彩内容

  • 花似伊,柳似伊,花柳青春人别离 只留双泪垂 长江东,长江西,两岸鸳鸯两岸飞 相逢知几时
    继锋非阅读 13评论 0 0
  • 2017-05-22 糖果单词 - Tracy 糖果单词 本期小编:胖崔 创始人:胖崔、老郭、一婷、老K、孟爷 作...
    糖果单词阅读 47评论 0 0