在做ABAP开发时,有的时候我们需要将数据整理成文件然后上传至SAP,整理后的文件可以是文本格式或者EXCEL文件格式等,这也要看具体的业务要求了,本篇文件将以实例介绍如何读取文本文件。嘎嘎。。。
首先要介绍一下CL_GUI_FRONTEND_SERVICES类,这个类提供了很多对操作系统文件的操作,例如拷贝,列出文件名,打开文件等。当在从客户端打开一个文件是我们要用到它的静太方法FILE_OPEN_DIALOG,将文本文件读取到内表时又要用到它的静态方法GUI_UPLOAD。GUI_UPLOAD是个比较特殊的方法,可以直接被FUNCTION直接调用类似的还有GUI_DOWNLOAD等,可能是静态方法系统中又封装了一次,当然你也可以直接通过类来调用静态方法两都都可以实现。最后将读入到内表中的数据在ALV中列出,具体程式如下。
[FOR EXAMPLE]
TYPE-POOLS:SLIS. "ALV要用到的类型池
DATA:I_FIELD_CAT TYPE SLIS_T_FIELDCAT_ALV, "ALV是显示时要的属性
FIELD_CAT TYPE SLIS_FIELDCAT_ALV,
I_LAYOUT TYPE SLIS_LAYOUT_ALV,
I_REPID LIKE SY-REPID.
DATA:BEGIN OF TXT_READ_DATA OCCURS 0, "定义一个内表,用于存放从TXT上传的数据
ZH_NAME(30) TYPE C,
EN_NAME(30) TYPE C,
SEX(10) TYPE C,
DATE LIKE SY-DATUM,
END OF TXT_READ_DATA.
DATA:LV_FILETABLE TYPE FILETABLE, "打开文件的信息放入此列表中
LV_FILENAME TYPE STRING. "定义一字段用于放置上传的文件名称
SELECTION-SCREEN BEGIN OF BLOCK myBlock WITH FRAME TITLE title. "創建上傳文件的屏幕
PARAMETERS:P_FILE LIKE RLGRAP-FILENAME OBLIGATORY LOWER CASE.
SELECTION-SCREEN END OF BLOCK myBlock.
INITIALIZATION. "初始化屏幕时
title = 'GUI UPLOAD'.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE."按F4鍵時也可觸發該事件
PERFORM F_OPEN_FILENAME CHANGING P_FILE.
START-OF-SELECTION.
PERFORM READ_DATA_IN.
PERFORM BUILD_LAYOUT.
PERFORM BUILD_FIELDCAT.
PERFORM DISPLAY_ALV.
*&---------------------------------------------
*&F_OPEN_FILENAME FOR OPEN FILE
*&---------------------------------------------
FORM F_OPEN_FILENAME CHANGING P_FILE.
DATA: LV_RC TYPE I.
"调用METHOD 打开对话框
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG "OPEN FILE DIALOG 打开上传文件的对话框
EXPORTING
WINDOW_TITLE = '選擇上傳文件'
FILE_FILTER = 'All Files (*.*)|*.*|NotePad Files(*.txt)|*.txt|Excel Files(*.xls)|*.xls|Word files(*.doc)|*.doc' "过滤上传文件的类型
DEFAULT_EXTENSION = '*.txt'
DEFAULT_FilENAME = '1.txt' "默认打开的文件 "d:\Users\wei.sunqing\Desktop\1.txt
"INITIAL_DIRECTORY = 'C:/' "初始化的目录
"MULTISELECTION = 'X' "是否可以同时打开多个文件
CHANGING
FILE_TABLE = LV_FILETABLE "你打开文件名的列表
RC = LV_RC "返回打开文件的数量
.
IF SY-SUBRC <> 0.
MESSAGE 'FILE DOES NOT EXIST!' TYPE 'E'.
ELSEIF LV_FILETABLE[] IS NOT INITIAL. "不為空
READ TABLE LV_FILETABLE INDEX 1 INTO LV_FILENAME. "读取打開文件内表的第一个文件信息
P_FILE = LV_FILENAME.
ENDIF.
ENDFORM.
*&---------------------------------------------
*& READ DATA IN
*&---------------------------------------------
FORM READ_DATA_IN .
IF P_FILE CS '.TXT' ."判断文件名称是否包含.txt EXCEL:OR P_FILE CS '.XLS'
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = LV_FILENAME "要讀取的文件
FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB "字段間按TAB鍵分隔開來
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* CODEPAGE = '8400' "如果在不能正常显示中文的情况下设置此项
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* VIRUS_SCAN_PROFILE =
* NO_AUTH_CHECK = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
DATA_TAB = TXT_READ_DATA "寫入相應的內表中
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
.
IF SY-SUBRC <> 0.
IF SY-SUBRC = 1.
MESSAGE 'IMPORT FILE OPEN ERROR' TYPE 'I'.
EXIT.
ENDIF.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ELSE.
MESSAGE 'IMPORT FILE SHOULD BE A TEXT FILE' TYPE 'I'.
ENDIF.
ENDFORM.
*&---------------------------------------------
*& BUILD ALV LAYOUT
*&---------------------------------------------
FORM BUILD_LAYOUT.
I_REPID = SY-REPID.
I_LAYOUT-ZEBRA = 'X'.
I_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
ENDFORM.
*&---------------------------------------------
*& BUILD ALV FIELDCAT
*&---------------------------------------------
FORM BUILD_FIELDCAT.
CLEAR I_FIELD_CAT[].
CLEAR FIELD_CAT.
FIELD_CAT-FIELDNAME = 'ZH_NAME'.
FIELD_CAT-SELTEXT_L = '中文名'.
APPEND FIELD_CAT TO I_FIELD_CAT.
CLEAR FIELD_CAT.
FIELD_CAT-FIELDNAME = 'EN_NAME'.
FIELD_CAT-SELTEXT_L = '英文名'.
APPEND FIELD_CAT TO I_FIELD_CAT.
CLEAR FIELD_CAT.
FIELD_CAT-FIELDNAME = 'SEX'.
FIELD_CAT-SELTEXT_L = '姓别'.
APPEND FIELD_CAT TO I_FIELD_CAT.
CLEAR FIELD_CAT.
FIELD_CAT-FIELDNAME = 'DATE'.
FIELD_CAT-SELTEXT_L = '时间'.
APPEND FIELD_CAT TO I_FIELD_CAT.
CLEAR FIELD_CAT.
ENDFORM.
*&---------------------------------------------
*& DISPLAY DATA WITH ALV
*&---------------------------------------------
FORM DISPLAY_ALV.
DATA I_LINE TYPE I.
DESCRIBE TABLE TXT_READ_DATA LINES I_LINE. "将内表数据行数写入变量I_LINE中
"IF IT_FILEDATA[] IS NOT INITIAL.
IF I_LINE > 0.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' "调用函数用ALV显示出数据
EXPORTING
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER = ' '
* I_BUFFER_ACTIVE = ' '
I_CALLBACK_PROGRAM = I_REPID
* I_CALLBACK_PF_STATUS_SET = ' '
* I_CALLBACK_USER_COMMAND = ' '
* I_CALLBACK_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_END_OF_LIST = ' '
* I_STRUCTURE_NAME =
* I_BACKGROUND_ID = ' '
* I_GRID_TITLE =
* I_GRID_SETTINGS =
IS_LAYOUT = I_LAYOUT
IT_FIELDCAT = I_FIELD_CAT
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
* IT_SORT =
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
* I_SAVE = ' '
* IS_VARIANT =
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT =
* IS_REPREP_ID =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* I_HTML_HEIGHT_TOP = 0
* I_HTML_HEIGHT_END = 0
* IT_ALV_GRAPHICS =
* IT_HYPERLINK =
* IT_ADD_FIELDCAT =
* IT_EXCEPT_QINFO =
* IR_SALV_FULLSCREEN_ADAPTER =
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
T_OUTTAB = TXT_READ_DATA
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2
.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDIF.
ENDFORM.
运行界面如下:
在ALV中显示出的数据:
最后符上上面程式中测试用的数据(中间以TAB符分隔):
巴门尼德 Parmenides Male 20120812
赫利克里特 Heractitus Male 20120812
马可·奥勒留 Marcus Aurelius Male 20120812
柏拉图 Plato Male 20120812
亚里士多德 Aristotle Male 20120812
苏格拉底 Socrates Male 20120812
笛卡尔 Descartes Male 20120812
GUI_UPLOAD也可以将EXCEL读取入内表中,上面代码中加入判断是否是EXCEL文件后上传即可读取。