ABAP OLE导出Excel

INCLUDE 程序代码如下:


 TYPE-POOLS: soi,ole2.


 DATA: lo_application TYPE ole2_object,
       lo_workbook    TYPE ole2_object,
       lo_workbooks   TYPE ole2_object,
       lo_range       TYPE ole2_object,
       lo_worksheet   TYPE ole2_object,
       lo_worksheets  TYPE ole2_object,
       lo_column      TYPE ole2_object,
       lo_row         TYPE ole2_object,
       lo_cell        TYPE ole2_object,
       lo_font        TYPE ole2_object.

 DATA: lo_cellstart  TYPE ole2_object,
       lo_cellend    TYPE ole2_object,
       lo_selection  TYPE ole2_object,
       lo_validation TYPE ole2_object.

 DATA: lv_ole_selected_folder TYPE string,  "选择文件夹路径
       lv_ole_complete_path   TYPE char256, "完整路径
       lv_ole_titulo          TYPE string.  "选择文件夹窗口标题。
 TYPES:ty_pastedata(1500) TYPE c.
 DATA: lt_pastedata TYPE ty_pastedata OCCURS 0 WITH HEADER LINE.

 FORM frm_getpath.  "获取文件夹路径。

   "获取文件路径。
   CALL METHOD cl_gui_frontend_services=>directory_browse
     EXPORTING
       window_title    = lv_ole_titulo
       initial_folder  = 'C:\'
     CHANGING
       selected_folder = lv_ole_selected_folder
     EXCEPTIONS
       cntl_error      = 1
       error_no_gui    = 2
       OTHERS          = 3.

 ENDFORM.

 FORM frm_createole.  "创建OLE对象。

   "创建OLE EXCEL 对应 lo_application.
   CREATE OBJECT lo_application 'Excel.Application'.
   "指定OLE Workbooks 对象 lo_workbooks.
   CALL METHOD OF lo_application 'Workbooks' = lo_workbooks.
   "Workbooks 对象方法,新增。
   CALL METHOD OF lo_workbooks 'Add' = lo_workbook.
   "设置OLE对象 属性。
   SET PROPERTY OF lo_application 'Visible' = 0.
   "获取OLE对象 活动Sheet 对象 lo_worksheet.
   GET PROPERTY OF lo_application 'ACTIVESHEET' = lo_worksheet.

 ENDFORM.

 FORM frm_save USING p_name.

   "设置文件路径+名称
   CONCATENATE lv_ole_selected_folder '\' p_name INTO lv_ole_complete_path.

   "保存文件
   CALL METHOD OF lo_workbook 'SaveAs'
     EXPORTING
       #1 = lv_ole_complete_path.
   IF sy-subrc EQ 0.
     MESSAGE 'File downloaded successfully' TYPE 'S'.
   ELSE.
     MESSAGE 'Error downloading the file' TYPE 'E'.
   ENDIF.

   "释放对象。
   CALL METHOD OF lo_workbook 'CLOSE'.
   CALL METHOD OF lo_application 'QUIT'.
   FREE OBJECT lo_worksheet.
   FREE OBJECT lo_workbook.
   FREE OBJECT lo_application.

 ENDFORM.

 FORM frm_setcell USING p_row p_col p_value.
   CALL METHOD OF lo_worksheet 'Cells' = lo_cell
       EXPORTING
       #1 = p_row  "Row
       #2 = p_col. "Column

   SET PROPERTY OF lo_cell 'Value' = p_value.
 ENDFORM.

 FORM frm_paste.
* Copy to clipboard into ABAP
   CALL FUNCTION 'CONTROL_FLUSH'
     EXCEPTIONS
       OTHERS = 3.
   CALL FUNCTION 'CLPB_EXPORT'
     TABLES
       data_tab   = lt_pastedata
     EXCEPTIONS
       clpb_error = 1
       OTHERS     = 2.

* Select the cell A1
   CALL METHOD OF lo_worksheet 'Cells' = lo_cell
     EXPORTING
     #1 = 2  "Row
     #2 = 1. "Column

* Paste clipboard from cell A1
   CALL METHOD OF lo_cell 'SELECT'.
   CALL METHOD OF lo_worksheet 'PASTE'.
 ENDFORM.

具体功能代码段如下:(将内表数据一次性粘贴到新的Excel表中)


FORM frm_expexcel.
    DATA:lv_row   TYPE i,
         lv_col   TYPE i,
         lv_value TYPE string.

    DATA ls_char TYPE char2. "记录回车、换行符 
    DATA:p_hc TYPE char1,    "回车的16进制
         p_hh TYPE char1.    "换行的16进制


    PERFORM frm_getpath.

    IF lv_ole_selected_folder IS INITIAL.
      MESSAGE w001(00) WITH 'err:未选择文件路径'.
      RETURN.
    ENDIF.

    PERFORM frm_createole.

    DEFINE f_setcell.
      ADD 1 TO lv_col.
      PERFORM frm_setcell USING lv_row lv_col &1.
    END-OF-DEFINITION.

    DEFINE f_paste.
      lv_value = &1.

          ls_char = cl_abap_char_utilities=>cr_lf.  " 获取回车、换行符
          p_hc = ls_char+0(1). "回车
          p_hh = ls_char+1(1). "换行

                "回车符号处理
      REPLACE ALL OCCURRENCES OF p_hc IN lv_value WITH ' '.
      "换行符号处理
      REPLACE ALL OCCURRENCES OF p_hh IN lv_value WITH ' '.


      CONCATENATE lt_pastedata lv_value INTO lt_pastedata SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
    END-OF-DEFINITION.

    lv_row = 1.
    CLEAR lv_col.
    "根据fieldcat 设置的字段和描述 设置第一行列名
    LOOP AT it_fieldcat INTO lw_fiedcat.
      f_setcell:lw_fiedcat-seltext_l.
    ENDLOOP.

    lw_fiedcat-fieldname = 'IHREZ'.

    LOOP AT gt_data INTO gs_data.
      LOOP AT it_fieldcat INTO lw_fiedcat.
        IF lw_fiedcat-fieldname NE 'BEIZHU'.
          ASSIGN COMPONENT lw_fiedcat-fieldname OF STRUCTURE gs_data TO FIELD-SYMBOL().
        ELSE.
          ASSIGN COMPONENT 'BEIZHU2' OF STRUCTURE gs_data TO .
        ENDIF.
        IF  IS NOT ASSIGNED.
           = ''.
        ENDIF.
        f_paste:.
      ENDLOOP.

      SHIFT lt_pastedata BY 1 PLACES LEFT.
      APPEND lt_pastedata. CLEAR lt_pastedata.
    ENDLOOP.

    PERFORM frm_paste.

    DATA(lv_fname) = 'EXP_EXCEL' && sy-datum.
    PERFORM frm_save USING lv_fname .

ENDFORM.

你可能感兴趣的:(ABAP,sap,abap)