点击(此处)折叠或打开

  1. *&---------------------------------------------------------------------*
  2. *& Report ZDEMO15
  3. *&
  4. *&---------------------------------------------------------------------*
  5. *&
  6. *&
  7. *&---------------------------------------------------------------------*

  8. REPORT zdemo15.

  9. DATA:BEGIN OF gs_itab,
  10.       matnr TYPE makt-matnr,
  11.       maktx TYPE makt-maktx,
  12.       END OF gs_itab,
  13.       gt_itab LIKE TABLE OF gs_itab.

  14. DATA:gv_lines TYPE i.

  15. INCLUDE ole2incl.

  16. DATA: gv_excel TYPE ole2_object, " Excel object
  17.       gv_workbook TYPE ole2_object, " workbook
  18.       gv_sheet TYPE ole2_object, " sheet
  19.       gv_zl TYPE ole2_object, " cell
  20.       gv_f TYPE ole2_object. " font

  21. ***dowm muban
  22. DATA:gs_wwwdatatab TYPE wwwdatatab, "Contents of table WWWDATA
  23.      gv_filename TYPE rlgrap-filename,
  24.      gv_rc TYPE sy-subrc.

  25. PERFORM frm_get_file_path USING gv_filename.
  26. CHECK gv_filename IS NOT INITIAL.

  27. SELECT SINGLE
  28.    relid "Region in IMPORT/EXPORT Data Table
  29.    objid "SAP WWW Gateway Object Name
  30.  FROM wwwdata
  31.  INTO CORRESPONDING FIELDS OF gs_wwwdatatab
  32.  WHERE srtf2 = '0'
  33.  AND relid = 'MI'
  34.  AND objid = 'ZDEMO15'.
  35. IF sy-subrc <> 0 .
  36.   MESSAGE 'not exit' TYPE 'I'.
  37. ENDIF.

  38. CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
  39.   EXPORTING
  40.     key = gs_wwwdatatab
  41.     destination = gv_filename
  42.   IMPORTING
  43.     rc = gv_rc.
  44. IF gv_rc <> 0.
  45.   MESSAGE 'Down faile' TYPE 'E'.
  46. ENDIF.

  47. * start Excel
  48. CREATE OBJECT gv_excel 'EXCEL.APPLICATION'.
  49. PERFORM err_hdl.
  50. SET PROPERTY OF gv_excel 'Visible' = 0. "1 DISPLAY 0 NO-DISPLAY
  51. PERFORM err_hdl.

  52. * get list of workbooks, initially empty
  53. CALL METHOD OF
  54.     gv_excel
  55.     'WORKBOOKS' = gv_workbook.
  56. PERFORM err_hdl.

  57. * OPEN DOWNLOAD EXCEL
  58. CALL METHOD OF
  59.     gv_workbook
  60.     'OPEN'

  61.   EXPORTING
  62.     #1 = gv_filename.

  63. CALL METHOD OF
  64.     gv_excel
  65.     'SHEETS' = gv_sheet
  66.   EXPORTING
  67.     #1 = 1.

  68. CALL METHOD OF
  69.     gv_sheet
  70.     'SELECT'.

  71. CALL METHOD OF gv_sheet 'ACTIVATE'. "sheet ACTIVATE

  72. ** add a new workbook
  73. *CALL METHOD OF
  74. * gv_workbook
  75. * 'Add' = gv_map.
  76. *PERFORM err_hdl.

  77. * output column headings to active Excel sheet
  78. PERFORM fill_cell USING 1 1 1 '物料编码'.
  79. PERFORM fill_cell USING 1 2 1 '物料描述'.

  80. SELECT matnr maktx INTO TABLE gt_itab
  81.   FROM makt UP TO 10 ROWS.

  82. gv_lines = 1.
  83. LOOP AT gt_itab INTO gs_itab.
  84.   gv_lines = gv_lines + 1.
  85.   PERFORM fill_cell USING gv_lines 1 0 gs_itab-matnr.
  86.   PERFORM fill_cell USING gv_lines 2 0 gs_itab-maktx.
  87. ENDLOOP.

  88. SET PROPERTY OF gv_excel 'DisplayAlerts' = 0.

  89. CALL METHOD OF
  90.     gv_sheet
  91.     'SaveAs'

  92.   EXPORTING
  93.     #1 = gv_filename
  94.     #2 = 1
  95.     #3 = '123456'."增加密码设置 FZH

  96. CALL METHOD OF
  97.     gv_sheet
  98.     'close'

  99.   EXPORTING
  100.     #1 = 0.

  101. CALL METHOD OF
  102.     gv_sheet
  103.     'quit'.

  104. CALL METHOD OF
  105.     gv_sheet
  106.     'Exit' = gv_sheet.

  107. * disconnect from Excel
  108. DO.
  109.   CALL FUNCTION 'FLUSH'
  110.     EXCEPTIONS
  111.       OTHERS = 0.

  112.   FREE OBJECT gv_excel.
  113.   IF sy-subrc EQ 0.
  114.     EXIT.
  115.   ENDIF.
  116. ENDDO.
  117. PERFORM err_hdl.



  118. *&---------------------------------------------------------------------*
  119. *& Form fill_cell
  120. *&---------------------------------------------------------------------*
  121. * text
  122. *----------------------------------------------------------------------*
  123. * -->I text
  124. * -->J text
  125. * -->BOLD text
  126. * -->VAL text
  127. *----------------------------------------------------------------------*
  128. FORM fill_cell USING i j bold val.
  129.   CALL METHOD OF
  130.       gv_excel
  131.       'Cells' = gv_zl
  132.     EXPORTING
  133.       #1 = i
  134.       #2 = j.
  135.   PERFORM err_hdl.
  136.   SET PROPERTY OF gv_zl 'Value' = val .
  137.   PERFORM err_hdl.
  138.   GET PROPERTY OF gv_zl 'Font' = gv_f.
  139.   PERFORM err_hdl.
  140.   SET PROPERTY OF gv_f 'Bold' = bold .
  141.   PERFORM err_hdl.
  142. ENDFORM. "fill_cell

  143. * output column headings to active Excel sheet
  144. *&---------------------------------------------------------------------*
  145. *& Form err_hdl
  146. *&---------------------------------------------------------------------*
  147. * text
  148. *----------------------------------------------------------------------*
  149. FORM err_hdl.
  150.   IF sy-subrc <> 0.
  151.     WRITE: / 'Fehler bei OLE-Automation:'(010), sy-subrc.
  152.     STOP.
  153.   ENDIF.
  154. ENDFORM. " ERR_HDL

  155. *&---------------------------------------------------------------------*
  156. *& Form frm_get_file_path
  157. *&---------------------------------------------------------------------*
  158. * text
  159. *----------------------------------------------------------------------*
  160. *
  161. *----------------------------------------------------------------------*
  162. FORM frm_get_file_path CHANGING cv_file TYPE rlgrap-filename.
  163.   DATA:lv_rc TYPE i,
  164.         lv_filename TYPE string,
  165.         lv_path TYPE string,
  166.         lv_fullpath TYPE string.

  167.   CALL METHOD cl_gui_frontend_services=>file_save_dialog
  168.     EXPORTING
  169.       window_title = '选择文件'
  170.       file_filter = '(*.xls)|*.xls|'
  171.     CHANGING
  172.       filename = lv_filename
  173.       path = lv_path
  174.       fullpath = lv_fullpath
  175.     EXCEPTIONS
  176.       OTHERS = 5.

  177.   IF sy-subrc EQ 0.
  178.     cv_file = lv_fullpath.
  179.   ENDIF.
  180. ENDFORM. "FRM_GET_FILE_PATH

10-12 08:48