• abap 导出数据到excle,粘贴复制法


    1:编写公共的include

    include ZGNPUBI000

    include 实现:

    types:begin of g_line,
           row(4096type c,
          end of g_line.
    types:g_field(1024type c.
    types:g_lines type table of g_line initial size 100.
    types:tt_chart type table of bapi_char.
    data:g_workbook type ole2_object,
         g_app type ole2_object.
    data:g_sheet1 type ole2_object.
    data: has_data type c.
    *DATA: tabulator TYPE c.
    *DATA: tabulator_hex(2) TYPE x VALUE 9.
    data:g_title1(72type c value '开始导出...'.
    data:g_referr type ref to cx_root.
    data:lt_chart type table of bapi_char.
    data:lt_fcat type lvc_t_fcat.
    data:lf_data type ref to data.
    data:l_ref type ref to data.
    data:l_tabnm type dd02l-tabname.
    data:l_fldnm type fieldname.
    field-symbols:<lt_fs> type standard table.
    data:lt_asc type g_lines .
    *&---------------------------------------------------------------------*
    *&      Form  FM_GET_CONV_DATA
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    form fm_get_conv_data.
    *  CLEAR:tabulator_hex,tabulator.
    *  CALL METHOD cl_abap_conv_in_ce=>uccp
    *    EXPORTING
    *      uccp = tabulator_hex
    *    RECEIVING
    *      char = tabulator.
    endform.                    "FM_GET_CONV_DATA
    *&---------------------------------------------------------------------*
    *&      Form  fm_get_workbook
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_APP      text
    *      -->P_WORKBOOK text
    *----------------------------------------------------------------------*
    form fm_get_workbook changing p_app type ole2_object
                                  p_workbook type ole2_object.
      data: h_app type ole2_object,
            h_temp type ole2_object."temporary variat    .
      clear p_workbook.
      create object p_app 'Excel.Application' .
      set property of p_app 'DisplayAlerts' = 0.
    *  SET PROPERTY OF p_app 'Visible' = 1.
      call method of
          p_app
          'Workbooks' = h_temp.
      call method of
          h_temp
          'Add'  = p_workbook.
    endform.                    "fm_init_ole2
    *&---------------------------------------------------------------------*
    *&      Form  fm_show_progress
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_LINE     text
    *      -->P_TITLE    text
    *----------------------------------------------------------------------*
    form fm_show_progress using p_line p_title.
      call function 'SAPGUI_PROGRESS_INDICATOR'
        exporting
          percentage = p_line
          text       = p_title.
    endform.                    "fm_show_PROGRESS
    *&---------------------------------------------------------------------*
    *&      Form  fm_show_app
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_WORKBOOK text
    *----------------------------------------------------------------------*
    form fm_show_app  using    p_workbook type ole2_object.
      data:h_temp type ole2_object."temporary variat
      check p_workbook is not initial.
      get property of   p_workbook 'Application' = h_temp.
    *  CALL METHOD OF h_temp 'Close'.
      set property of h_temp 'Visible' = 1.
    endform.                    " FM_SHOW_APP
    *&---------------------------------------------------------------------*
    *&      Form  fm_close_app
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_WORKBOOK text
    *----------------------------------------------------------------------*
    form fm_close_app  using  p_workbook type ole2_object.
      data:h_temp type ole2_object."temporary variat
      check p_workbook is not initial.
      get property of   p_workbook 'Application' = h_temp.
    *  CALL METHOD OF h_temp 'Close'.
      call method of
          h_temp
          'QUIT'.
    endform.                    " FM_CLOSE_APP
    *&---------------------------------------------------------------------*
    *&      Form  fm_free_app
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    form fm_free_app.
      free object: g_app,g_workbook.
    endform.                    "fm_free_app
    *&---------------------------------------------------------------------*
    *&      Form  fm_finish_app
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    form fm_finish_app.
      perform fm_show_app using g_workbook.
      perform fm_free_app.
    endform.                    "fm_finish_app
    *&---------------------------------------------------------------------*
    *&      Form  fm_chg_sheet1_ddic
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_WORKBOOK text
    *----------------------------------------------------------------------*
    form fm_chg_sheet1_ddic using p_workbook type ole2_object.
    *  DATA:l_temp TYPE ole2_object.
      data:l_worksheets type ole2_object.
      data:l_last type ole2_object.
      data:l_sheetnum type i.
      data:l_count type i.
      data:l_temp type ole2_object.
      get property of p_workbook 'Worksheets' = l_worksheets.
      get property of l_worksheets 'Count' = l_sheetnum.
      l_count = l_sheetnum - 1.
      do l_count  times.
        call method of
            l_worksheets
            'Item'       = l_last
          exporting
            #1           = sy-index.
        call method of
            l_last
            'Activate'.
    ********    改变颜色,加框
        perform fm_chg_sheet1_color using l_last.
    ********    冻结框口
        call method of
            l_last
            'Range' = l_last
          exporting
            #1      = 'A8'.
        call method of
            l_last
            'Select'.
        get property of p_workbook 'Application' = l_last.
        get property of l_last 'ActiveWindow' = l_last.
        set property of l_last 'FreezePanes' = 1.
      enddo.
      call method of
          l_worksheets
          'Item'       = l_last
        exporting
          #1           = l_sheetnum.
    *  IF dy_bd = 'X'.
    *    DATA:l_temp TYPE ole2_object.
    *    CALL METHOD OF l_worksheets 'Item' = l_temp
    *      EXPORTING
    *      #1 = 1.
    *
    *    SET PROPERTY OF l_temp 'Name' = '设备功能位置'.
    *
    *    CALL METHOD OF l_temp 'Move'
    *      EPORTING
    *      #1 = l_last.
    *  ENDIF.
      call method of
          l_last
          'Delete'.
      call method of
          l_worksheets
          'Item'       = l_last
        exporting
          #1           = 1.
      call method of
          l_last
          'Activate'.
      free object: l_temp,l_last.
    endform.                    "fm_chg_sheet1_ddic
    *&---------------------------------------------------------------------*
    *&      Form  fm_chg_sheet1_color
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_LAST     text
    *----------------------------------------------------------------------*
    form fm_chg_sheet1_color using p_last type ole2_object.
    *    Rows("8:8").Select
    *    ActiveWindow.FreezePanes = True.
      data:l_tempx type ole2_object.
      data:l_sheet type ole2_object.
      data:l_app type ole2_object.
      call method of
          p_last
          'Rows' = l_tempx
        exporting
          #1     = '1:7'.
      perform fm_draw_line using l_tempx.
      get property of l_tempx 'Interior' = l_tempx.
      set property of l_tempx 'ColorIndex' = 6.
      set property of l_tempx 'Pattern' = -4161.
    *  FREE OBJECT l_tempx.
    endform.                    "fm_chg_sheet1_color
    *&---------------------------------------------------------------------*
    *&      Form  fm_draw_line
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_SEL      text
    *----------------------------------------------------------------------*
    form fm_draw_line using p_sel type ole2_object.
      check p_sel is not initial.
      data:l_border type ole2_object.
      databegin of ltw_style,
              style type text12,
              count type i,
            end of ltw_style.
      data:begin of ltw_count occurs 0,
              count type i,
              style like table of ltw_style,
           end of ltw_count.
    *  DATA:ltw_count TYPE TABLE OF i.
    *  APPEND 5 TO ltw_count.
    *  APPEND 6 TO ltw_count.
      ltw_count-count = 5.
      ltw_style-style = 'LineStyle'.
      ltw_style-count = -4142.
      append ltw_style to ltw_count-style.
      append ltw_count.
      ltw_count-count = 6.
      append ltw_count.
      clear ltw_count.
      ltw_style-style = 'LineStyle'.
      ltw_style-count = 1.
      append ltw_style to ltw_count-style.
      ltw_style-style = 'Weight'.
      ltw_style-count = 2.
      append ltw_style to ltw_count-style.
      ltw_style-style = 'ColorIndex'.
      ltw_style-count = -4105.
      append ltw_style to ltw_count-style.
      ltw_count-count = 7.
      append ltw_count.
      ltw_count-count = 8.
      append ltw_count.
      ltw_count-count = 9.
      append ltw_count.
      ltw_count-count = 10.
      append ltw_count.
      ltw_count-count = 11.
      append ltw_count.
      ltw_count-count = 12.
      append ltw_count.
      loop at ltw_count.
        call method of
            p_sel
            'Borders' = l_border
          exporting
            #1        = ltw_count-count.
        loop at ltw_count-style into ltw_style.
          case sy-tabix.
            when 1.set property of l_border 'LineStyle' = ltw_style-count.
            when 2.set property of l_border 'Weight' = ltw_style-count.
            when 3.set property of l_border 'ColorIndex' = ltw_style-count.
            when others.
          endcase.
        endloop.
      endloop.
    endform.                    "fm_draw_line
    *&---------------------------------------------------------------------*
    *&      Form  fm_fill_sheet
    *&--------------------------------------------------------------------*
    *       text
    *&---------------------------------------------------------------------*
    *      -->P_CLASS    text
    *      -->P_IDX      text
    *      -->P_WORKBOOK text
    *      -->PT_ASC     text
    *----------------------------------------------------------------------*
    form fm_fill_sheet  using    p_class
                                 p_idx
                                 p_workbook
                                 pt_asc type g_lines.
      data:l_sheet type ole2_object.
      data:l_count type i.
      data:l_temp type ole2_object.
      perform fm_add_sheet using p_class p_idx p_workbook
            changing l_sheet.
      data:ld_rc type i.
      call method cl_gui_frontend_services=>clipboard_export
        importing
          data                 = pt_asc[]
        changing
          rc                   = ld_rc
        exceptions
          cntl_error           = 1
          error_no_gui         = 2
          not_supported_by_gui = 3
          others               = 4.
      get property of l_sheet 'Cells' = l_temp.
      call method of
          l_temp
          'Select'.
      get property of l_sheet 'Application' = l_temp.
      get property of l_temp 'Selection' = l_temp.
      set property of l_temp 'NumberFormatLocal' = '@'.
      call method of
          l_sheet
          'Cells' = l_temp
        exporting
          #1      = 1
          #2      = 1.
      call method of
          l_temp
          'Select'.
      call method of
          l_sheet
          'Paste'.
      get property of l_sheet 'Application' = l_temp.
      data:l_selection type ole2_object.
      get property of l_temp 'Selection' = l_selection.
      get property of l_selection 'Font' = l_temp.
      set property of l_temp 'Size' = 10.
      get property of l_selection 'Columns' = l_temp.
      call method of
          l_temp
          'AutoFit'.
    endform.                    " FM_FILL_SHEET
    *&---------------------------------------------------------------------*
    *&      Form  fm_fill_sheet_name
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_CLASS    text
    *      -->P_IDX      text
    *      -->P_WORKBOOK text
    *      -->PT_ASC     text
    *----------------------------------------------------------------------*
    form fm_fill_sheet_name  using  p_class
                                 p_idx
                                 p_workbook
                                 pt_asc type g_lines.
      data:l_sheet type ole2_object.
      data:l_count type i.
      data:l_temp type ole2_object.
      perform fm_add_sheet_name using p_class p_idx p_workbook
            changing l_sheet.
      data:ld_rc type i.
      call method cl_gui_frontend_services=>clipboard_export
        importing
          data                 = pt_asc[]
        changing
          rc                   = ld_rc
        exceptions
          cntl_error           = 1
          error_no_gui         = 2
          not_supported_by_gui = 3
          others               = 4.
      get property of l_sheet 'Cells' = l_temp.
      call method of
          l_temp
          'Select'.
      get property of l_sheet 'Application' = l_temp.
      get property of l_temp 'Selection' = l_temp.
      set property of l_temp 'NumberFormatLocal' = '@'.
      call method of
          l_sheet
          'Cells' = l_temp
        exporting
          #1      = 1
          #2      = 1.
      call method of
          l_temp
          'Select'.
      call method of
          l_sheet
          'Paste'.
      get property of l_sheet 'Application' = l_temp.
      data:l_selection type ole2_object.
      get property of l_temp 'Selection' = l_selection.
      get property of l_selection 'Font' = l_temp.
      set property of l_temp 'Size' = 10.
      get property of l_selection 'Columns' = l_temp.
      call method of
          l_temp
          'AutoFit'.
    endform.                    " FM_FILL_SHEET
    *&---------------------------------------------------------------------*
    *&      Form  fm_add_sheet
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_CLASS    text
    *      -->P_IDX      text
    *      -->P_WORKBOOK text
    *      -->P_SHEET    text
    *----------------------------------------------------------------------*
    form fm_add_sheet  using p_class p_idx p_workbook
          changing p_sheet.
      data:l_count type i.
      data:l_temp type ole2_object.
      data:l_last type ole2_object.
      get property of p_workbook 'Worksheets' = l_temp.
      get property of l_temp 'Count' = l_count.
      if p_idx >= l_count."增加sheet
        call method of
            l_temp
            'Item' = l_last
          exporting
            #1     = l_count.
        call method of
            l_temp
            'Add'  = p_sheet
          exporting
            #1     = l_last.
      else.
        call method of
            p_workbook
            'Worksheets' = p_sheet
          exporting
            #1           = p_idx.
      endif.
      data:l_kschg type m_clasb-kschg.
      select single kschg into l_kschg
         from m_clasb where class = p_class.
      concatenate '(' p_class ')' l_kschg into l_kschg.
      translate l_kschg using '\_/_'.
      condense l_kschg no-gaps.
      set property of p_sheet 'Name' = l_kschg.
      call method of
          p_sheet
          'Activate'.
    endform.                   "fm_fill_sheet_name
    *&---------------------------------------------------------------------*
    *&      Form  fm_add_sheet_name
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_CLASS    text
    *      -->P_IDX      text
    *      -->P_WORKBOOK text
    *      -->P_SHEET    text
    *----------------------------------------------------------------------*
    form fm_add_sheet_name  using p_class p_idx p_workbook
          changing p_sheet.
      data:l_count type i.
      data:l_temp type ole2_object.
      data:l_last type ole2_object.
      get property of p_workbook 'Worksheets' = l_temp.
      get property of l_temp 'Count' = l_count.
      if p_idx > l_count."增加sheet
        call method of
            l_temp
            'Item' = l_last
          exporting
            #1     = l_count.
        call method of
            l_temp
            'Add'  = p_sheet
          exporting
            #1     = l_last.
      else.
        call method of
            p_workbook
            'Worksheets' = p_sheet
          exporting
            #1           = p_idx.
      endif.
      data:l_kschg type m_clasb-kschg.
    *  SELECT SINGLE kschg INTO l_kschg
    *     FROM m_clasb WHERE class = p_class.
      concatenate '(' p_class ')' l_kschg into l_kschg.
      l_kschg = p_class.
      translate l_kschg using '\_/_'.
      condense l_kschg no-gaps.
      set property of p_sheet 'Name' = l_kschg.
      call method of
          p_sheet
          'Activate'.
    endform.                    "fm_fill_sheet_name
    *&---------------------------------------------------------------------*
    *&      Form  fm_bulid_innter
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->PT_TAB     text
    *      -->PT_FCAT   text
    *      -->TABULATOR  text
    *      -->PROGNM     text
    *      -->PT_ASC     text
    *----------------------------------------------------------------------*
    form fm_bulid_innter  using pt_tab type standard table
          pt_fcat type lvc_t_fcat tabulator prognm type repid
                          changing pt_asc type g_lines.
      field-symbols:<l_cat> like line of pt_fcat,
                    <l_fs>.
      data:l_tabix type sy-tabix.
      data:l_line like line of pt_asc.
      data:l_inx like sy-tabix.
      loop at pt_tab assigning <l_fs>.
        check <l_fs> is not initial.
        clear l_line.
        clear l_inx.
        loop at pt_fcat assigning <l_cat> .
          l_inx = l_inx + 1.
          perform fm_fill_value using l_inx <l_fs> <l_cat>-fieldname tabulator prognm
                changing l_line.
        endloop.
        append l_line to pt_asc.
      endloop.
    endform.                    "fm_bulid_innter
    *&---------------------------------------------------------------------*
    *&      Form  fm_fill_value
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_WA       text
    *      -->P_FLDNM    text
    *      -->TABULATOR  text
    *      -->PROGNM     text
    *      -->P_LINE     text
    *----------------------------------------------------------------------*
    form fm_fill_value using p_inx p_wa p_fldnm tabulator prognm
          changing p_line type g_line.
      field-symbols:<l_field>.
      data:l_val type g_field.
      assign component p_fldnm of structure p_wa to <l_field>.
      if sy-subrc = 0 and <l_field> is not initial.
        l_val = <l_field>.
        condense l_val.
      endif.
      if p_inx = 1.
        p_line = l_val.
      else.
        concatenate p_line tabulator l_val into p_line.
      endif.
    endform.                    "fm_fill_value
    *&---------------------------------------------------------------------*
    *&      Form  fm_clear_variant
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    form fm_clear_variant.
      clear has_data.
      if <lt_fs> is assigned.
        free <lt_fs>.
        unassign <lt_fs>.
      endif.
      if l_ref is not initial.
        clear l_ref.
      endif.
      clear:lf_data.
      free: lt_fcat,lt_chart,lt_asc.
    endform.                    "fm_clear_variant
    *&---------------------------------------------------------------------*
    *&      Form  fm_build_structure
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_KLART    text
    *      -->P_CLASS    text
    *      -->P_TABNAME  text
    *      -->PT_FCAT    text
    *      -->PT_CHART   text
    *---------------------------------------------------------------------*
    form fm_build_structure using  p_tabname changing pt_fcat type lvc_t_fcat .
    ****根据固定表构建字段目录*************
      refresh pt_fcat.
      call function 'LVC_FIELDCATALOG_MERGE'
        exporting
    *     I_BUFFER_ACTIVE        =
          i_structure_name       = p_tabname
    *     I_CLIENT_NEVER_DISPLAY = 'X'
    *     I_BYPASSING_BUFFER     =
    *     I_INTERNAL_TABNAME     =
        changing
          ct_fieldcat            = pt_fcat
        exceptions
          inconsistent_interface = 1
          program_error          = 2
         others                 = 3.
      if sy-subrc <> 0.
        message id sy-msgid type sy-msgty number sy-msgno
               with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      endif.
    endform.                    " FM_BUILD_STRUCTURE
    *&---------------------------------------------------------------------*
    *&      Form  fm_build_append_stru
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_KLART    text
    *      -->P_CLASS    text
    *      -->PT_FCAT    text
    *      -->PT_CHART   text
    *----------------------------------------------------------------------*
    form fm_build_append_stru using p_klart p_class
          changing pt_fcat type lvc_t_fcat pt_chart type tt_chart.
    ***根据类包含的属性构建字段目录********
    *类包含参数
      data:l_class_key  type  bapi_class_key.
      l_class_key-classnum = p_class.
      l_class_key-classtype = p_klart.
      data:lt_charv type table of bapi_char_values.
      refresh pt_chart.
      call function 'BAPI_CLASS_GET_CHARACTERISTICS'
        exporting
          classnum        = l_class_key-classnum
          classtype       = l_class_key-classtype
        tables
          characteristics = pt_chart
          char_values     = lt_charv.
      if pt_chart is initial.
    *    MESSAGE e001(00) WITH '找不到特性!' RAISING no_class_data.
      endif.
      field-symbols:<l_fs> type bapi_char.
      field-symbols:<l_fcat> like line of pt_fcat.
      data:ls_fcat like line of pt_fcat.
      ls_fcat-ref_table = 'ALAVLJ2EE'.
      ls_fcat-ref_field = 'IPADDRESS'.
      ls_fcat-col_pos = lines( pt_fcat )."l_max.
      loop at pt_chart assigning <l_fs>.
        ls_fcat-col_pos = ls_fcat-col_pos + 1.
        ls_fcat-fieldname = <l_fs>-name_char.
        ls_fcat-reptext = <l_fs>-descr_char.
        append ls_fcat to pt_fcat.
      endloop.
    endform.                    "fm_build_append_stru
    *&---------------------------------------------------------------------*
    *&      Form  fm_get_tabref
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->PT_FCAT    text
    *      -->PF_DATA    text
    *----------------------------------------------------------------------*
    form fm_get_tabref using pt_fcat  type lvc_t_fcat changing pf_data.
    **根据字段目录构建内表结构****
      type-pools:abap.
      data:l_fcat like line of  pt_fcat.
      data:l_field type char128.
      data: struct_type type ref to cl_abap_structdescr,
            comp_tab    type cl_abap_structdescr=>component_table,
            comp        like line of comp_tab,
            dref        type ref to data.
      loop at pt_fcat into l_fcat.
        comp-name = l_fcat-fieldname.
        if l_fcat-ref_field is not initial.
          concatenate l_fcat-ref_table '-' l_fcat-ref_field into l_field.
        else.
          concatenate l_fcat-ref_table '-' l_fcat-fieldname into l_field.
        endif.
        comp-type ?= cl_abap_typedescr=>describe_by_name( l_field ).
        append comp to comp_tab.
      endloop.
      struct_type = cl_abap_structdescr=>create( comp_tab ).
      data:l_tab_type type ref to cl_abap_tabledescr.
      l_tab_type = cl_abap_tabledescr=>create( struct_type ).
      create data pf_data type handle l_tab_type.
    endform.                    "fm_get_tabref
    *&---------------------------------------------------------------------*
    *&      Form  fm_bulid_header
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->PT_CHART   text
    *      -->TABULATOR  text
    *      -->PROGNM     text
    *      -->PT_FCAT    text
    *      -->PT_TAB     text
    *----------------------------------------------------------------------*
    form fm_bulid_header  using    pt_chart type tt_chart
                                   tabulator prognm
                          changing pt_fcat type lvc_t_fcat
                                   pt_tab type g_lines.
      constants:c_header_line type i value 1.
      data:l_char type bapi_char.
      data:l_line like line of pt_tab.
      field-symbols:<l_line> like line of pt_tab.
      do c_header_line times.
        append l_line to pt_tab.
      enddo.
      field-symbols:<l_fs> like line of pt_fcat.
      data:l_tabix type i.
      data:l_count type i.
      data:l_obg(2type c.
      data:l_fcat like line of pt_fcat.
      clear l_tabix.
      loop at pt_fcat assigning <l_fs> .
        l_tabix = l_tabix + 1.
        "填充TAB符号
        read table pt_tab assigning <l_line> index l_tabix.
        if l_tabix = 1.
          concatenate <l_line> <l_fs>-scrtext_l into <l_line>.
        else.
          concatenate <l_line> tabulator into <l_line>.
          concatenate <l_line> <l_fs>-scrtext_l into <l_line>.
        endif.
      endloop.
    *  LOOP AT pt_tab ASSIGNING <l_line>.
    *    CASE sy-tabix.
    *      WHEN 1.CONCATENATE '字段名称' <l_line>-row INTO <l_line>-row.
    *    ENDCASE.
    *  ENDLOOP.
    endform.                    "fm_bulid_header
    *&---------------------------------------------------------------------*
    *&      Form  fm_fill_header_line
    *&---------------------------------------------------------------------*

    *       text
    *----------------------------------------------------------------------*
    *      -->P_VALUE    text
    *      -->P_IDX      text
    *      -->PT_TAB     text
    *----------------------------------------------------------------------*
    form fm_fill_header_line using p_value p_idx type i
          changing pt_tab type g_lines .
      field-symbols:<l_line> like line of pt_tab.
      data:l_val type c length 128.
      read table pt_tab assigning <l_line> index p_idx.
      l_val = p_value.
      condense l_val.
      concatenate <l_line> l_val into <l_line>.
    endform.                    "fm_fill_header_line
    *&---------------------------------------------------------------------*
    *&      Form  fm_get_charval
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->PT_TAB     text
    *      -->PT_RESULT  text
    *----------------------------------------------------------------------*
    form fm_get_charval using  p_fldnm p_tabnm pt_tab type standard table
      changing pt_result type standard table.
      field-symbols:<l_line>.
      field-symbols:<l_result_line> .
      loop at pt_tab assigning <l_line>.
        append initial line to pt_result assigning <l_result_line>.
        move-corresponding <l_line> to <l_result_line>.
    *    perform fm_get_char using <l_line> p_fldnm p_tabnm "pt_chart
    *        changing <l_result_line>.
      endloop.
    endform.                    "fm_get_charval
    *&---------------------------------------------------------------------*
    *&      Form  fm_convert_charval
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->PT_VAL     text
    *      -->P_WA       text
    *----------------------------------------------------------------------*
    form fm_convert_charval using pt_val type standard table
          changing p_wa.
      field-symbols:<l_source>.
      field-symbols:<l_line>,<l_fldnm>,<l_value>,<l_value2>,<l_dest>,<l_rel>.
      data:l_atflv(3type c.
      field-symbols:<l_unit> type any.
      data:l_dest type atwrt.
      assign l_dest to <l_dest>.
      data:l_unit type char6.
      loop at pt_val assigning <l_line>.
        assign component 'CHARACT' of structure <l_line> to <l_fldnm>.
        assign component <l_fldnm> of structure p_wa to <l_source>.
        clear <l_dest>.
        check sy-subrc = 0.
        assign component 'VALUE_FROM' of structure <l_line> to <l_value>.
        if sy-subrc <> 0.
          assign component 'VALUE_CHAR' of structure <l_line> to <l_value>.
          <l_dest> = <l_value>.
        else.
          assign component 'VALUE_TO' of structure <l_line> to <l_value2>.
          assign component 'VALUE_RELATION' of structure <l_line> to <l_rel>.
          call function 'CTBP_CONVERT_VALUE_INT_TO_EXT'
            exporting
              charactname       = <l_fldnm>
              value_from        = <l_value>
              value_to          = <l_value2>
              value_relation    = <l_rel>
    *         CHARACTDETAIL     =
            importing
              value_external    = <l_dest>
            exceptions
              charact_not_found = 1
              no_authority      = 2
              wrong_data_type   = 3
              internal_error    = 4
              wrong_input       = 5
              wrong_format      = 6
              others            = 7.
          if sy-subrc = 0.
    *        CONDENSE <l_dest> NO-GAPS.
            assign component 'UNIT_FROM' of structure <l_line> to <l_unit>.
            if sy-subrc = 0 and <l_unit> is not initial.
    *          l_unit = <l_unit>.
              call function 'CONVERSION_EXIT_LUNIT_OUTPUT'
                exporting
                  input          = <l_unit>
                  language       = '1'
                importing
    *             LONG_TEXT      =
                  output         = l_unit
    *             short_text     = l_unit
                exceptions
                  unit_not_found = 1
                  others         = 2.
              if sy-subrc = 0.
                replace first occurrence of l_unit in <l_dest> with ''.
              else.
                replace first occurrence of <l_unit> in <l_dest> with ''.
              endif.
            endif.
            assign component 'UNIT_TO' of structure <l_line> to <l_unit>.
            if sy-subrc = 0 and <l_unit> is not initial.
              call function 'CONVERSION_EXIT_LUNIT_OUTPUT'
                exporting
                  input          = <l_unit>
                  language       = '1'
                importing
    *             LONG_TEXT      =
                  output         = l_unit
    *             SHORT_TEXT     =
                exceptions
                  unit_not_found = 1
                 others         = 2.
              if sy-subrc = 0.
                replace first occurrence of l_unit in <l_dest> with ''.
              else.
                replace first occurrence of <l_unit> in <l_dest> with ''.
              endif.
            endif.
            assign component 'CURRENCY_FROM' of structure <l_line> to <l_unit>.
            if sy-subrc = 0 and <l_unit> is not initial.
              replace first occurrence of <l_unit> in <l_dest> with ''.
            endif.
            assign component 'CURRENCY_TO' of structure <l_line> to <l_unit>.
            if sy-subrc = 0 and <l_unit> is not initial.
              replace first occurrence of <l_unit> in <l_dest> with ''.
            endif.
          endif.
        endif.
        if <l_source> is not initial.
          concatenate <l_source> ';' <l_dest> into <l_source>.
        else.
          <l_source> = <l_dest>.
        endif.
      endloop.
    endform.                    "fm_convert_charval
    *&---------------------------------------------------------------------*
    *&      Form  fm_fill_sheet_PROJ
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_CLASS    text
    *      -->P_IDX      text
    *      -->P_WORKBOOK text
    *      -->PT_ASC     text
    *----------------------------------------------------------------------*
    form fm_fill_sheet_proj  using    p_class
                                 p_idx
                                 p_workbook
                                 pt_asc type g_lines.
      data:l_sheet type ole2_object.
      data:l_count type i.
      data:l_temp type ole2_object.
      perform fm_add_sheet_proj using p_class p_idx p_workbook
            changing l_sheet.
      data:ld_rc type i.
      call method cl_gui_frontend_services=>clipboard_export
        importing
          data                 = pt_asc[]
        changing
          rc                   = ld_rc
        exceptions
          cntl_error           = 1
          error_no_gui         = 2
          not_supported_by_gui = 3
          others               = 4.
      get property of l_sheet 'Cells' = l_temp.
      call method of
          l_temp
          'Select'.
      get property of l_sheet 'Application' = l_temp.
      get property of l_temp 'Selection' = l_temp.
      set property of l_temp 'NumberFormatLocal' = '@'.
      call method of
          l_sheet
          'Cells' = l_temp
        exporting
          #1      = 1
          #2      = 1.
      call method of
          l_temp
          'Select'.
      call method of
          l_sheet
          'Paste'.
      get property of l_sheet 'Application' = l_temp.
      data:l_selection type ole2_object.
      get property of l_temp 'Selection' = l_selection.
      get property of l_selection 'Font' = l_temp.
      set property of l_temp 'Size' = 10.
      get property of l_selection 'Columns' = l_temp.
      call method of
          l_temp
          'AutoFit'.
    endform.                    " FM_FILL_SHEET
    *&---------------------------------------------------------------------*
    *&      Form  fm_add_sheet_PROJ
    *&---------------------------------------------------------------------*
    *       text
    *----------------------------------------------------------------------*
    *      -->P_CLASS    text
    *     -->P_IDX      text
    *      -->P_WORKBOOK text
    *      -->P_SHEET    text
    *----------------------------------------------------------------------*
    form fm_add_sheet_proj  using p_class p_idx p_workbook
          changing p_sheet.
      data:l_count type i.
      data:l_temp type ole2_object.
      data:l_last type ole2_object.
      get property of p_workbook 'Worksheets' = l_temp.
      get property of l_temp 'Count' = l_count.
      if p_idx >= l_count."增加sheet
        call method of
            l_temp
            'Item' = l_last
          exporting
            #1     = l_count.
        call method of
            l_temp
            'Add'  = p_sheet
          exporting
            #1     = l_last.
      else.
        call method of
            p_workbook
            'Worksheets' = p_sheet
          exporting
            #1           = p_idx.
      endif.
      data:l_kschg type tcj4t-profi_txt.
      select single profi_txt into l_kschg
         from tcj4t where profidproj = p_class and spras = '1'.
      concatenate '(' p_class ')' l_kschg into l_kschg.
      translate l_kschg using '\_/_'.
      condense l_kschg no-gaps.
      set property of p_sheet 'Name' = l_kschg.
      call method of
          p_sheet
          'Activate'.
    endform.                    "fm_fill_sheet_name

    功能实现

    FORM sub_write_data .
      DATA:l_idx TYPE int4.
      DATA:g_title(80).
      DATA:gnum(4).
      DATA:lt_odata LIKE zgns_mf001 OCCURS 0 WITH HEADER LINE.
      DATA:lt_down_item LIKE zgns_mf001 OCCURS 0 WITH HEADER LINE.
      CLEAR:l_idx,gnum.
      LOOP AT gt_tab.
        lt_odata-sel = gt_tab-sel.
        lt_odata-xabln = gt_tab-xabln.
        lt_odata-mblnr = gt_tab-mblnr.
        lt_odata-zeile = gt_tab-zeile.
        lt_odata-bwart = gt_tab-bwart.
        lt_odata-xblnr = gt_tab-xblnr.
        lt_odata-lfbnr = gt_tab-lfbnr.
        lt_odata-ebeln = gt_tab-ebeln.
        lt_odata-ebelp = gt_tab-ebelp.
        lt_odata-matnr = gt_tab-matnr.
        lt_odata-txz01 = gt_tab-txz01.
        lt_odata-ferth = gt_tab-ferth.
        lt_odata-charg = gt_tab-charg.
        lt_odata-lifnr = gt_tab-lifnr.
        lt_odata-name1 = gt_tab-name1.
        lt_odata-werks = gt_tab-werks.
        lt_odata-lgort = gt_tab-lgort.
        lt_odata-lgobe = gt_tab-lgobe.
        lt_odata-erfme_out = gt_tab-erfme_out.
        lt_odata-menge = gt_tab-menge.
        lt_odata-bldat = gt_tab-bldat.
        lt_odata-budat = gt_tab-budat.
        lt_odata-cpudt = gt_tab-cpudt.
        lt_odata-cputm = gt_tab-cputm.
        lt_odata-usnam = gt_tab-usnam.
        APPEND lt_odata.
      ENDLOOP.
      FIELD-SYMBOLS:<t_fcat> TYPE lvc_s_fcat.
      CLEAR lt_down_item .REFRESH lt_down_item .
      TRY.
          PERFORM fm_show_progress USING 10 g_title.
          PERFORM fm_get_conv_data.
          PERFORM fm_get_workbook CHANGING g_app g_workbook.
          l_idx = l_idx + 1.
          PERFORM fm_clear_variant.
    *根据类别构建输出数据结构***************************************
          PERFORM fm_build_structure USING 'ZGNS_MF001' CHANGING lt_fcat.
    ***********************************************************************
    ****必须在此处声明并使用************************************************
          DATA: has_data TYPE c.
          DATA: tabulator TYPE c.
          DATA: tabulator_hex(2TYPE x VALUE 9.
          CALL METHOD cl_abap_conv_in_ce=>uccp
            EXPORTING
              uccp = tabulator_hex
            RECEIVING
              char = tabulator.
    ***********************************************************************
    *根据字段目录构建内表****************************************************
          PERFORM fm_get_tabref USING lt_fcat CHANGING lf_data.
    ************************************************************************
    *构建导出数据*************************************************************
          CREATE DATA l_ref TYPE TABLE OF zgns_mf001.
          FIELD-SYMBOLS:<lt_fs> TYPE STANDARD TABLE .
          ASSIGN l_ref->* TO <lt_fs>.
          <lt_fs> = lt_odata[].
    *************************************************************************
    *******构建导出EXCEL第一行数据(字段信息)************************************
          IF lt_asc IS INITIAL.
    *        perform fm_bulid_header using lt_chart tabulator sy-repid
    *              changing lt_fcat lt_asc.
            DATA:l_asc TYPE g_line.
            CLEAR l_asc.
            CONCATENATE
                    '选择'
                    '收货单编号'
                    '入仓单号'
                    '序号'
                    '移动类型'
                    '送货单号'
                    '送检单号'
                    '采购订单号'
                    '采购行项目'
                    '物料编码'
                    '品名规格'
                    '内部型号'
                    '批次'
                    '供应商'
                    '供应商名称'
                    '工厂'
                    '仓位'
                    '仓位名称'
                    '单位'
                    '实收数量'
                    '凭证日期'
                    '过账日期'
                    '开单日期'
                    '开单时间'
                   '开单人'
            INTO l_asc-row  SEPARATED BY tabulator.
            APPEND l_asc TO lt_asc.
          ENDIF.
    **************************************************************************
    *******构建导出数据********************************************************
          IF <lt_fs> IS ASSIGNED.
            PERFORM fm_bulid_innter USING <lt_fs> lt_fcat tabulator sy-repid
                  CHANGING lt_asc.
          ENDIF.
    *************************************************************************
    *****根据不同的类导出数据到不同的标签**************************************
          PERFORM fm_fill_sheet_name USING 'Sheet1' 1  g_workbook lt_asc.
    ************************************************************************
          g_title = '导出完毕,开始展示...'.
          PERFORM fm_show_progress USING 100 g_title.
    *      PERFORM fm_chg_sheet_ddic USING g_workbook.
          PERFORM fm_finish_app.
        CATCH cx_root INTO g_referr.
          PERFORM fm_finish_app.
          RAISE process_error.
      ENDTRY.
    ENDFORM.    

  • 相关阅读:
    C#计算代码的执行耗时
    c#值类型和引用类型
    C#类、接口、虚方法和抽象方法
    15,了解如何在闭包里使用外围作用域中的变量
    函数闭包,golbal,nonlocal
    init())函数和main()函数
    函数的命名空间
    函数的默认参数是可变不可变引起的奇怪返回值
    遍历目录
    super顺序
  • 原文地址:https://www.cnblogs.com/szfeng/p/2838558.html
Copyright © 2020-2023  润新知