ABAP关于文件操作

  • A+
所属分类:Notes

SAP ABAP文件操作示例

在SAP的ABAP编程中,经常涉及到文件的处理,SAP的按照不同分类,有服务端文件和客户端文件,文本文件和二进制文件(EXE文件,Office文件,PDF文件等)。
服务器文件的操作
服务器文件的浏览
可以是用TCode:AL11查看服务器的文件夹和文件,可以点击配置“Configure”创建和管理文件夹。
创建和修改服务器文件
使用ABAP的OPEN DATASET带FOR OUTPUT语句打开文件;
使用TRANSFER语句写入文件;
使用CLOSE DATASET关闭文件(保存)。
DATA: gdf_filepath type dxfile-filename
DATA: gdt_data type table of gts_data.
DATA: ldf_length type i.
FIELD-SYMBOLS: <lfs_outfile> TYPE gts_data
START-OF-SELECTION.
gdf_filepath = 'C:\mydata.txt'.
OPEN DATASET gdf_filepath FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT prt_data ASSIGNING <lfs_outfile>.
DESCRIBE FIELD <lfs_outfile> LENGTH ldf_length IN BYTE MODE.
TRANSFER <lfs_outfile> TO prf_file LENGTH ldf_length.
ENDLOOP.
CLOSE DATASET gdf_filepath.
读取服务器文件
使用ABAP的OPEN DATASET带FOR INPUT语句打开文件;
使用READ DATASET语句读取文件;
使用CLOSE DATASET关闭文件。
PARAMETERS p_file TYPE dxfile-filename.
START-OF-SELECTION.
OPEN DATASET p_file IN TEXT MODE ENCODING DEFAULT FOR INPUT.
DO.
READ DATASET p_file INTO gds_data.
IF sy-subrc <> 0.
EXIT.
ENDIF.
APPEND gds_data TO gdt_data.
ENDDO.
CLOSE DATASET p_file.
检查服务器文件是否存在
使用函数OCS_GET_FILE_INFO
DATA: gdt_ocs_file TYPE TABLE OF ocs_file.
PARAMETERS p_file TYPE dxfile-filename.
AT SELECTION-SCREEN.
CALL FUNCTION 'OCS_GET_FILE_INFO'
EXPORTING
dir_name = p_file
file_name = '*'
TABLES
dir_list = gdt_ocs_file
或 使用OPEN DATASET,判断系统返回值判断文件
PARAMETERS p_file TYPE dxfile-filename.
AT SELECTION-SCREEN.
OPEN DATASET p_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc = 0.
CLOSE DATASET p_file.
ELSE.
MESSAGE 'the input file does not exist.' TYPE 'E'.
ENDIF.
读取服务器文件夹的文件列表
使用SUBST_GET_FILE_LIST 或 EPS_GET_DIRECTORY_LISTING函数
如何使用Search Help
使用'F4_DXFILENAME_4_DYNP'函数
PARAMETERS p_file TYPE dxfile-filename.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_DXFILENAME_4_DYNP'
EXPORTING
dynpfield_filename = 'P_FILE'
dyname = sy-repid
dynumb = sy-dynnr
filetype = 'P'
location = 'A'
server = ' '.
filetype: 'P' represents Physical file name; 'L' represents Logical file name.
location: 'A' represents Application Server; 'P' represents Presentation server.
使用F4_DXFILENAME_TOPRECURSION函数创建自定义的输入帮助
代码参见:http://wiki.sdn.sap.com/wiki/dis ... +Application+Server
注意
为了避免文件名称可变化,我们应该使用逻辑文件。使用TCODE:FILE可以定义逻辑文件,使用函数FILE_GET_NAME去获取文件名;

客户端文件的操作
创建客户端文件
使用函数 GUI_DOWNLOAD
*DATA DECLARATION for GUI_DOWNLOAD.
DATA : lv_filename type STRING.
lv_filename = file.
CONSTANTS: LV_FILETYPE TYPE CHAR10 VALUE 'ASC'.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME = lv_filename
FILETYPE = 'ASC'
CODEPAGE = 'IBM'
TABLES
DATA_TAB = DATA_TAB
EXCEPTIONS
FILE_WRITE_ERROR = 1
NO_BATCH = 2
GUI_REFUSE_FILETRANSFER = 3
INVALID_TYPE = 4
NO_AUTHORITY = 5
UNKNOWN_ERROR = 6
HEADER_NOT_ALLOWED = 7
SEPARATOR_NOT_ALLOWED = 8
FILESIZE_NOT_ALLOWED = 9
HEADER_TOO_LONG = 10
DP_ERROR_CREATE = 11
DP_ERROR_SEND = 12
DP_ERROR_WRITE = 13
UNKNOWN_DP_ERROR = 14
ACCESS_DENIED = 15
DP_OUT_OF_MEMORY = 16
DISK_FULL = 17
DP_TIMEOUT = 18
FILE_NOT_FOUND = 19
DATAPROVIDER_EXCEPTION = 20
CONTROL_FLUSH_ERROR = 21
OTHERS = 22.
file = lv_filename.
使用函数CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
DATA : filename_new TYPE string,
l_filename TYPE string,
l_fullpath TYPE string,
l_path TYPE string.
MOVE filename TO filename_new.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
default_file_name = filename_new
CHANGING
filename = l_filename
path = l_path
fullpath = l_fullpath.
IF sy-subrc <> 0.
ENDIF.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = l_fullpath
filetype = 'ASC'
append = 'X'
TABLES
data_tab = lt_datatab
EXCEPTIONS
OTHERS = 1.
注意: DOWNLOAD和WS_DOWNLOAD不建议使用
读取客户端文件
使用'GUI_UPLOAD'函数
DATA : lv_filename type STRING.
CONSTANTS: LV_FILETYPE TYPE CHAR10 VALUE 'ASC'.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = lv_filename
FILETYPE = 'DAT'
CODEPAGE = 'IBM'
TABLES
DATA_TAB = data_tab
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_READ_ERROR = 2
NO_BATCH = 3
GUI_REFUSE_FILETRANSFER = 4
INVALID_TYPE = 5
NO_AUTHORITY = 6
UNKNOWN_ERROR = 7
BAD_DATA_FORMAT = 8
HEADER_NOT_ALLOWED = 9
SEPARATOR_NOT_ALLOWED = 10
HEADER_TOO_LONG = 11
UNKNOWN_DP_ERROR = 12
ACCESS_DENIED = 13
DP_OUT_OF_MEMORY = 14
DISK_FULL = 15
DP_TIMEOUT = 16
OTHERS = 17.
使用CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD 方法
注意: UPLOAD 和 WS_UPLOAD 函数不建议使用
检查客户端文件是否存在
使用DX_FILE_EXISTENCE_CHECK 函数
DATA: gdf_file_exist(1) TYPE c.
PARAMETERS p_file TYPE dxfile-filename.
AT SELECTION-SCREEN.
CALL FUNCTION 'DX_FILE_EXISTENCE_CHECK'
EXPORTING
filename = p_file
pc = 'X'
* SERVER =
IMPORTING
file_exists = gdf_file_exist
IF NOT ( sy-subrc = 0 and gdf_file_exist = 'X' )
MESSAGE 'the input file does not exist.' TYPE 'E'.
ENDIF.
Pay attention to the importing parameter 'pc', it should be set as 'X'.
使用CL_GUI_FRONTEND_SERVICES=>FILE_EXIST函数
DATA: gdf_file_exist(1) TYPE c.
PARAMETERS p_file TYPE dxfile-filename.
AT SELECTION-SCREEN.
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = p_file
RECEIVING
result = gdf_file_exist
IF NOT ( sy-subrc = 0 and gdf_file_exist = 'X' )
MESSAGE 'the input file does not exist.' TYPE 'E'.
ENDIF.
读取客户端文件列表
使用 TMP_GUI_DIRECTORY_LIST_FILES 函数
或 CL_GUI_FRONTEND_SERVICES=>DIRECTORY_LIST_FILES 方法
使用search Help
使用CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG 方法
DATA: gdt_filetable TYPE filetable.
DATA: gdf_rc TYPE I.
PARAMETERS p_file TYPE dxfile-filename.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
WINDOW_TITLE = 'Choose a file'
CHANGING
file_table = gdt_filetable
rc = gdf_rc
IF sy-subrc = 0.
READ TABLE gdt_filetable
INTO gds_filetable INDEX 1.
p_file = gds_filetable-filename.
ENDIF.
使用CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG 方法
使用WS_FILENAME_GET函数
使用F4_FILENAME 函数(版本要求:R/3 and ECC only)
PARAMETERS p_file TYPE dxfile-filename.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
* EXPORTING
* PROGRAM_NAME =
* DYNPRO_NUMBER =
* FIELD_NAME =
IMPORTING
file_name = p_file.
注意对于选取文件夹请使用CL_GUI_FRONTEND_SERVICES=>DIRECTORY_BROWSE方法
服务器端和客户端之间的文件拷贝
TCode: CG3Y (from server to frontend)
TCode:CG3Z (from frontend to server)
使用CL_GUI_FRONTEND_SERVICES=>FILE_COPY 方法;
使用上文提到服务器和客户端操作的组合来实现;
文件的压缩
使用CL_ABAP_ZIP;
使用CL_ABAP_GZIP,这个类只能在内存中进行数据的压缩和解压处理;
对服务器端我们可以使用OPEN DATASET带FILTER实现
DATA file TYPE string VALUE /usr/test.Z.
OPEN DATASET file FOR OUTPUT IN BINARY MODE
FILTER 'compress'.
...
CLOSE DATASET file.
OPEN DATASET file FOR INPUT IN BINARY MODE
FILTER 'uncompress'.
...
CLOSE DATASET file.
其他操作
使用OLE/DOI for Office native files (Excel, Word, etc.)
CL_GUI_FRONTEND_SERVICES类提供大量有关文件操作的方法
相关链接:
http://wiki.sdn.sap.com/wiki/display/Snippets/Working+With+Files
http://wiki.sdn.sap.com/wiki/display/ABAP/Excel+file+types
http://wiki.sdn.sap.com/wiki/dis ... +Application+Server

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:1   其中:访客  1   博主  0

    • ag

      DATA lv_string TYPE string.
      DATA lv_string_tmp TYPE string.
      DATA gv_filea TYPE rlgrap-filename. “structure文件路径.
      DATA gv_fileb TYPE rlgrap-filename. “person文件路径.
      DATA gv_filename TYPE ocs_file-name.
      DATA: gdt_ocs_file TYPE TABLE OF ocs_file.

      gv_filea = ‘/usr/sap/hr/HR_STRUCT_’ && sy-datum && ‘.txt’. “服务器保存的目录和文件名
      gv_fileb = ‘/usr/sap/hr/HR_PERSONS_’ && sy-datum && ‘.txt’. “服务器保存的目录和文件名
      gv_filename = ‘HR_STRUCT_’ && sy-datum && ‘.txt’.

      “由于每次都更新txt太影响性能,所以只有传入标识的时候才更新txt文件!
      IF p_update_txt = ‘X’.
      “运行下面的程序后会在SAP服务器上生成两个文件
      “‘/usr/sap/hr/HR_STRUCT_’ && sy-datum && ‘.txt’.
      “‘/usr/sap/hr/HR_PERSONS_’ && sy-datum && ‘.txt’.

      SUBMIT zr_hr_interface
      WITH p_objid = ‘50010000’ “人事组织最顶层架构编号
      WITH p_date = sy-datum
      AND RETURN.
      ELSE.
      “判断文件是否存在(判断任意一个文件即可)
      CALL FUNCTION ‘OCS_GET_FILE_INFO’
      EXPORTING
      dir_name = ‘/usr/sap/hr/’
      file_name = gv_filename
      TABLES
      dir_list = gdt_ocs_file
      EXCEPTIONS
      no_authority = 1
      activity_unknown = 2
      not_a_directory = 3
      no_media_in_drive = 4
      too_many_errors = 5
      too_many_files = 6
      bracket_error_in_filename = 7
      no_such_parameter = 8
      OTHERS = 9.
      IF sy-subrc 0.
      * Implement suitable error handling here
      ENDIF.

      IF gdt_ocs_file[] IS INITIAL.
      SUBMIT zr_hr_interface
      WITH p_objid = ‘50010000’ “人事组织最顶层架构编号
      WITH p_date = sy-datum
      AND RETURN.
      ENDIF.
      ENDIF.

      OPEN DATASET gv_filea FOR INPUT IN TEXT MODE ENCODING DEFAULT.
      DO.
      READ DATASET gv_filea INTO lv_string.
      IF sy-subrc = 0.
      CONCATENATE lv_string ‘xiayige’ INTO lv_string_tmp.
      CONCATENATE z_rfc_hr_00001_fil01 lv_string_tmp INTO z_rfc_hr_00001_fil01.
      lv_string = ”.
      lv_string_tmp = ”.
      ELSE.
      EXIT.
      ENDIF.
      ENDDO.
      CLOSE DATASET gv_filea.
      lv_string = ”.
      lv_string_tmp = ”.

      “员工信息
      OPEN DATASET gv_fileb FOR INPUT IN TEXT MODE ENCODING DEFAULT.
      DO.
      READ DATASET gv_fileb INTO lv_string.
      IF sy-subrc = 0.
      CONCATENATE lv_string ‘xiayige’ INTO lv_string_tmp.
      CONCATENATE z_rfc_hr_00001_fil02 lv_string_tmp INTO z_rfc_hr_00001_fil02.
      lv_string = ”.
      lv_string_tmp = ”.
      ELSE.
      EXIT.
      ENDIF.
      ENDDO.
      CLOSE DATASET gv_fileb.
      lv_string = ”.
      lv_string_tmp = ”.

      *方式二————————–:
      *通过下面的bapi即可非常简单地获取ppose完整组织架构信息(structuraldata).
      “BAPI_ORGUNITEXT_DATA_GET.
      CALL FUNCTION ‘BAPI_ORGUNITEXT_DATA_GET’
      EXPORTING
      plvar = ’01’
      otype = ‘O’
      objid = ‘50010000’ “全集团最顶层人事组织代码.
      keydate = sy-datum
      * SCENARIO = ‘MDT1’
      * EVALPATH =
      * EVALDEPTH = 0
      * IMPORTING
      * RETURN =
      TABLES
      * ACTORTAB =
      structuraldata = gt_hr_qxr[]
      objectsdata = gt_hr_qxs[]
      * FIELDCATALOGUE =
      * FIELDDATA =