SAP中使用动态内表

文章目录

      • 1 Introduction
      • 2 Code
        • 2.1 Create internal table
        • 2.2 Export data

1 Introduction

In the sap we can use dnynamic internal table for it . This is a exampe for
it .

2 Code

2.1 Create internal table
  DATA: lv_col       TYPE i VALUE 1,
        lv_fieldname TYPE lvc_fname,
        lv_text      TYPE char30,
        lv_times     TYPE n LENGTH 3,
        lv_num       TYPE n LENGTH 2,
        lv_date      TYPE n LENGTH 2,
        lv_bh        TYPE n LENGTH 2.

  DEFINE mcr_field.

    CLEAR gs_fieldcat.
    gs_fieldcat-fieldname  = &1.
    gs_fieldcat-col_pos    = lv_col.
    gs_fieldcat-ref_table  = &2.
    gs_fieldcat-ref_field  = &3.
    gs_fieldcat-reptext    = &4.
    gs_fieldcat-scrtext_l  = &4.
    gs_fieldcat-scrtext_m  = &4.
    gs_fieldcat-scrtext_s  = &4.
    gs_fieldcat-fix_column = &5.
    APPEND gs_fieldcat TO gt_fieldcat.
    lv_col = lv_col + 1.

  END-OF-DEFINITION.

  IF rd_bt1 IS NOT INITIAL.
    mcr_field 'LIGHT'   'ZPPS201_DIS' 'LIGHT'   '' 'X'.
    mcr_field 'MESSAGE' 'ZPPS201_DIS' 'MESSAGE' '' 'X'.
  ELSE.
    mcr_field 'AUART' 'T003P' 'AUART' ''       'X'.
    mcr_field 'AUTXT' 'T003P' 'TXT'   TEXT-004 'X'.
  ENDIF.
  mcr_field 'FEVOR' 'ZPPS201_DIS' 'FEVOR' TEXT-001 'X'.
  mcr_field 'TXT'   'ZPPS201_DIS' 'TXT'   TEXT-002 'X'.

  CLEAR: lv_times, lv_num.
  lv_times = gv_date+6(2).
  CASE p_werks.
    WHEN 'CN10'. lv_num = c_cn10.
    WHEN 'CN11'. lv_num = c_cn11.
  ENDCASE.
  lv_times = lv_times * lv_num.
  .

  DO lv_times TIMES.

    CLEAR: lv_date, lv_bh, lv_text.

    lv_date = ( lv_col - 5 ) DIV lv_num + 1.
    lv_bh = ( lv_col - 5 ) MOD lv_num + 1.
    lv_fieldname = gv_date(6) && lv_date && lv_bh.
    lv_text = lv_fieldname+6(2).
    CLEAR gs_fieldcat.
    gs_fieldcat-fieldname = lv_fieldname.
    gs_fieldcat-col_pos   = lv_col.
    IF rd_bt1 IS NOT INITIAL.
      gs_fieldcat-inttype   = 'C'.
    ELSE.
      gs_fieldcat-inttype   = 'N'.
    ENDIF.
    gs_fieldcat-intlen    = 3.
    gs_fieldcat-outputlen = 3.
    gs_fieldcat-reptext   = lv_text.
    gs_fieldcat-scrtext_l = lv_text.
    gs_fieldcat-scrtext_m = lv_text.
    gs_fieldcat-scrtext_s = lv_text.
    APPEND gs_fieldcat TO gt_fieldcat.
    lv_col = lv_col + 1.
  ENDDO.
  IF rd_bt1 IS NOT INITIAL.
    mcr_field 'COLOR' 'MASS_MARC'   'COLINFO' '' ''.
    mcr_field 'SEL'   'ZPPS201_DIS' 'SEL'     '' ''.
    mcr_field 'ZFLAG' 'ZPPS201_DIS' 'SEL'     '' ''.
  ENDIF.
* 创建动态内表
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = gt_fieldcat
    IMPORTING
      ep_table        = gt_dyntable.

* 用表类型指针<fs_table> 指向数据对象的内容.
  ASSIGN gt_dyntable->* TO <fs_table>.

* 建立一个与动态内表结构相同的数据对象,且数据对象为是一个结构
  CREATE DATA gs_dynline LIKE LINE OF <fs_table>.
  ASSIGN gs_dynline->* TO <fs_wa>.

  IF rd_bt1 IS NOT INITIAL.
    DELETE gt_fieldcat WHERE fieldname = 'COLOR' OR fieldname = 'SEL' OR fieldname = 'ZFLAG'.

* 创建日志结构
    DATA: lo_table TYPE REF TO data,
          lo_line  TYPE REF TO data,
          lt_stru  LIKE gt_fieldcat.

    lt_stru = gt_fieldcat.

    DELETE lt_stru WHERE fieldname(1) <> '2'.

    FIELD-SYMBOLS: <fs_logtable> TYPE STANDARD TABLE.
* 创建动态内表
    CALL METHOD cl_alv_table_create=>create_dynamic_table
      EXPORTING
        it_fieldcatalog = lt_stru
      IMPORTING
        ep_table        = lo_table.

* 用表类型指针<dyn_table> 指向数据对象的内容.
    ASSIGN lo_table->* TO <fs_logtable>.

    CREATE DATA lo_line LIKE LINE OF <fs_logtable>.
    ASSIGN lo_line->* TO <fs_log>.

  ENDIF.
2.2 Export data
 DATA: lt_data TYPE TABLE OF zalsmex_tabline,
        ls_data TYPE zalsmex_tabline.

  DATA l_data TYPE truxs_t_text_data.

  DATA: lv_end_col TYPE int4,
        lv_col     TYPE int4,
        lv_num     TYPE zenumj.

  lv_end_col = gv_date+6(2).

  CASE p_werks.
    WHEN 'CN10'. lv_end_col = lv_end_col * c_cn10 + 1.
    WHEN 'CN11'. lv_end_col = lv_end_col * c_cn11 + 1.
  ENDCASE.

  CLEAR lt_data.
  CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_file
      i_begin_col             = 1
      i_begin_row             = 2
      i_end_col               = lv_end_col
      i_end_row               = 5000
    TABLES
      intern                  = lt_data
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 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.

  CLEAR: gt_aufk.
  LOOP AT lt_data INTO ls_data.
    CONDENSE  ls_data-value NO-GAPS.
    TRANSLATE ls_data-value TO UPPER CASE.
    CASE  ls_data-col.
      WHEN '1'.
        ASSIGN COMPONENT 'FEVOR' OF STRUCTURE <fs_wa> TO <fs_field>.
        <fs_field> = ls_data-value.
      WHEN OTHERS.
        lv_col = ls_data-col + 3.
        ASSIGN COMPONENT lv_col OF STRUCTURE <fs_wa> TO <fs_field>.
        IF ls_data-value CO '1234567890 '.
          lv_num = ls_data-value.
          <fs_field> = lv_num.
        ELSE.
          <fs_field> = ls_data-value.
        ENDIF.
    ENDCASE.
    AT END OF row.
      PERFORM frm_check.
      APPEND <fs_wa> TO <fs_table>.
      FREE <fs_wa>.
    ENDAT .
  ENDLOOP.

  SORT gt_aufk BY fevor gstrp zznum.

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