导出SAP表结构到EXCEl

时间:2023-01-02 00:49:29

将SE11中看到的数据表结构,制作成EXCEL文件,以便查阅。CPOY的话得一个字段一个元素的COPY再一个个粘过去,不仅麻烦,如果一个结构有上百个元素,工作量也是很大的。

研究整理了一下各位达人的程序,写了一个新的导出程序。本程序只需在选择界面输入要导出的结构名及保存路径,就在该路径下生成一个以该结构命名的EXCEL文件。也可以指定输出结构的单独字段。代码如下:

REPORT  YTABLE no standard page heading
                 message-id y2
                 line-size  200
                 line-count 65 .

tables: dfies, x030l,rlgrap.

data: begin of itab occurs 0.
        include structure dfies.
data: end of itab.
data: g_file like rlgrap-filename.   "下载保存路径
data:begin of itab1 occurs 0,
     fieldname like dfies-fieldname,  "Fieldname
     keyflag(4),    "KEY
     rollname(12),   "Data Element
     datatype(8),   "Data Type
     leng(6),       "Length
     decimals(6),   "Decimal Place
     fieldtext like dfies-fieldtext,  "Short Description
end of itab1.

*********************定义屏幕
selection-screen begin of block blk1 with frame title text-001.
parameters:table type ddobjname default 'VBAK',
           field type dfies-fieldname,
           p_dnfile like rlgrap-filename default 'D:/'.
selection-screen end of block blk1.

start-of-selection.
  perform read_data."从表中读取数据

end-of-selection.
  perform write_data."输出数据
*&---------------------------------------------------------------------*
*&      Form  read_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form read_data .
*****CALL FUNCTION*****
  call function 'DDIF_FIELDINFO_GET'
    exporting
      tabname        = table
      fieldname      = field
      langu          = sy-langu "这个可以改成别的语言,For Short Descriptions
    tables
      dfies_tab      = itab " like table dfies.
    exceptions
      not_found      = 1
      internal_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.

  itab1-fieldname = '字段'.  "Fieldname
  itab1-keyflag = '主键'.    "KEY
  itab1-rollname = '数据元素'.   "Data Element
  itab1-datatype = '数据类型'.   "Data Type
  itab1-leng = '长度'.       "Length
  itab1-decimals = '小数位'.  "Decimal Place
  itab1-fieldtext = '短文本'.  "Short Description
  append itab1.
  clear itab1.

  loop at itab.
    itab1-fieldname = itab-fieldname.
    itab1-keyflag = itab-keyflag.
    itab1-rollname = itab-rollname.
    itab1-datatype = itab-datatype.
    itab1-leng = itab-leng.
    itab1-decimals = itab-decimals.
    itab1-fieldtext = itab-fieldtext.
    append itab1.
    clear itab1.
  endloop.

  concatenate p_dnfile table '.xls' into g_file. "这里为将内表数据下载到本地D盘,名字为表名,类型为EXCEL
  call function 'WS_DOWNLOAD'
    exporting
      filename = g_file
      filetype = 'DAT'
    tables
      data_tab = itab1.  "被下载的内表

endform.                    " read_data
*&---------------------------------------------------------------------*
*&      Form  write_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form write_data .
  loop at itab1.
    write:/ itab1-fieldname,  "Fieldname
            itab1-keyflag,    "KEY
            itab1-rollname,   "Data Element
            itab1-datatype,   "Data Type
            itab1-leng,       "Length
            itab1-decimals,   "Decimal Place
            itab1-fieldtext.  "Short Description
  endloop.
endform.                    " write_data