dsfsfs 发表于 2019-2-2 15:16:32

SAP中本地文件的upload和download的方法收藏

文件UPLOAD方法(转成内表):  一、最常用FM的是:GUI_UPLOAD
  有同等作用的是CLASS cl_gui_frontend_services的静态方法gui_upload,文件可以按二进制或文本格式上传,数值和日期等依赖于用户的设置。
  上传二进制文件时,内表通常定义为只含一个数据类型为X的field,如:
  data:begin of itab occurs 0,
  raw(255) type X,
  end fo itab.
  CALL FUNCTION 'GUI_UPLOAD'
  exporting
  filetype = 'BIN'
  filename = 'C:\DOWNLOAD.BIN'
  tables
  data_tab = itab.
  主要参数:输入有filename 要上传文件的完成路径, filetype 包括'BIN' 'DAT' 'ASC'等,codepage 以数字编号的字符集,如8400是简体中文
  输出有data_tab 储存文件中数据的内表
  二、对上传excel数据可用FM TEXT_CONVERT_XLS_TO_SAP
  因为输出的I_TAB_CONVERTED_DATA类型为任意的standard table,所以此FM能方便的把excel中内容转化成格式相同的内表中。
  field-symbols:type standard table.
  call function 'TEXT_CONVERT_XLS_TO_SAP'
  exporting
  *         I_FIELD_SEPERATOR          =
  *         I_LINE_HEADER            =
  i_tab_raw_data             = raw_data
  i_filename               = excel_file
  tables
  i_tab_converted_data       =
  exceptions
  conversion_failed          = 1
  others                     = 2.
  参数i_tab_raw_data虽为改办field,但好像没什么作用,传入一个空数据也可正常使用。
  三、以另外途径上传excel文件:ALSM_EXCEL_TO_INTERNAL_TABLE
  call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
  exporting
  filename                = p_file
  i_begin_col             = p_scol
  i_begin_row             = p_srow
  i_end_col               = p_ecol
  i_end_row               = p_erow
  tables
  intern                  = i_intern
  exceptions
  inconsistent_parameters = 1
  upload_ole            = 2
  others                  = 3.
  此FM可以限制从excel文件中第几行第几列开始,一次上传多少行多少列,但一次上传的最大行数为9999,是由接收数据的内表intern的定义来限制的。
  所以如果要上传多于9999行的数据,要编码分批上传实现的。而intern不是直接与excel数据格式相符的内表,是系统定义好的,包含了行号、列号、数据值等,具体的
  请参照系统定义,因此要想转成自己定义符合格式的内表,可用下面代码实现:
  field-symbols : .
  data exception type ref to cx_root.
  sort i_intern by row col.
  try.
  loop at i_intern.
  move i_intern-col to index.
  assign component index of structure i_table to .
  move i_intern-value to .
  at end of row.
  append i_table. "user defined internal table to store excel data
  clear i_table.
  endat.
  endloop.
  catch cx_root into exception.
  message 'Excel data format is wrong,please check' type 'E'.
  endtry.

页: [1]
查看完整版本: SAP中本地文件的upload和download的方法收藏