CSVファイルデータを取込した後、編集やチェックができるようにする例。
↓コピぺはここからどうぞ↓
REPORT FILEINPUTTEST.
*----------------------------------------------------------------------*
* CONSTANTS
*----------------------------------------------------------------------*
CONSTANTS:
CNS_RESULT(1) TYPE C VALUE 'X'. "チェック用
*----------------------------------------------------------------------*
* TYPE
*----------------------------------------------------------------------*
TYPES:
* データ読込用
BEGIN OF TYP_FILE,
LINE TYPE TEXT1000,
END OF TYP_FILE,
* 入力読込用
BEGIN OF TYP_INPUTDATA,
INPUT1(1) TYPE C,
INPUT2(10) TYPE C,
INPUT3(30) TYPE C,
INPUT4(5) TYPE C,
INPUT5(10) TYPE C,
INPUT6(10) TYPE N,
END OF TYP_INPUTDATA.
*----------------------------------------------------------------------*
* WORKS
*----------------------------------------------------------------------*
DATA:
W_FILE TYPE STRING,
W_PATH TYPE STRING,
W_INFILE TYPE STRING.
*----------------------------------------------------------------------*
* INTERNAL TABLE
*----------------------------------------------------------------------*
DATA:
TD_FILE TYPE STANDARD TABLE OF TYP_FILE,
TH_FILE TYPE TYP_FILE,
TD_INPUTDATA TYPE TABLE OF TYP_INPUTDATA,
TH_INPUTDATA TYPE TYP_INPUTDATA.
*----------------------------------------------------------------------*
* PARAMETERS 入力画面
*----------------------------------------------------------------------*
PARAMETERS:
P_FILE TYPE LOCALFILE OBLIGATORY.
*----------------------------------------------------------------------*
* INITIALIZATION
*----------------------------------------------------------------------*
INITIALIZATION.
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN OUTPUT
*----------------------------------------------------------------------*
* P_FILEの検索ヘルプ
AT SELECTION-SCREEN ON VALUE-REQUEST FOR : P_FILE.
PERFORM GET_FILENME_PATH USING P_FILE.
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
* 入力チェック
PERFORM FRM_CHECK_INPUT.
*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
* ファイルデータをPGM内に取込
PERFORM FRM_GET_INFILE.
* 取込データを各項目に割当&編集
PERFORM FRM_EDIT_INFILE.
*&---------------------------------------------------------------------*
*& Form GET_FILENME_PATH
*&---------------------------------------------------------------------*
* 入力ファイル検索ヘルプ
*----------------------------------------------------------------------*
FORM GET_FILENME_PATH USING I_FNAME.
* ローカル変数定義
DATA: LW_RC TYPE I,
LTD_FILETABLE TYPE FILETABLE.
* クラス呼出
CALL METHOD cl_gui_frontend_services=>file_open_dialog
CHANGING
file_table = LTD_FILETABLE
rc = LW_RC
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5.
* 判定
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
READ TABLE LTD_FILETABLE INTO P_FILE INDEX 1.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CHECK_INPUT
*&---------------------------------------------------------------------*
* 入力チェック処理
*----------------------------------------------------------------------*
FORM FRM_CHECK_INPUT .
* CHAR型からSTRING型に変換
W_INFILE = P_FILE.
* INPUTファイル存在チェック
PERFORM FRM_CHECK_INFILE.
ENDFORM. " FRM_CHECK_INPUT
*&---------------------------------------------------------------------*
*& Form F_CHECK_INFILE
*&---------------------------------------------------------------------*
* 入力ファイルの存在チェック
*----------------------------------------------------------------------*
FORM FRM_CHECK_INFILE.
* ローカル定義
DATA:
LW_FILE TYPE STRING,
LW_RESULT(1) TYPE C.
* 入力ファイル名の分割
CALL FUNCTION 'TRINT_SPLIT_FILE_AND_PATH'
EXPORTING
FULL_NAME = P_FILE "全パス
IMPORTING
STRIPPED_NAME = W_FILE "ファイル名のみ
FILE_PATH = W_PATH "パス(ファイル名の前まで)
EXCEPTIONS
X_ERROR = 1
OTHERS = 2.
* 入力されたファイルが存在しない場合終了
IF SY-SUBRC <> 0.
* エラーメッセージ出力
MESSAGE E000(SU) WITH '入力ファイルが存在しません。'.
ENDIF.
* 入力ファイル存在チェック
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_EXIST
EXPORTING
FILE = W_INFILE
RECEIVING
RESULT = LW_RESULT
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
WRONG_PARAMETER = 3
NOT_SUPPORTED_BY_GUI = 4
OTHERS = 5.
* 'X'以外の場合存在しない。
IF LW_RESULT <> CNS_RESULT.
* エラーメッセージ出力
MESSAGE E000(SU) WITH '取込ファイルが存在しません。'.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_INFILE
*&---------------------------------------------------------------------*
* データ取込処理
*----------------------------------------------------------------------*
FORM FRM_GET_INFILE.
* ローカル定義
DATA:
LW_FILENAME TYPE STRING.
* データ読込
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD
EXPORTING
FILENAME = LW_FILENAME
FILETYPE = 'ASC'
CHANGING
DATA_TAB = TD_FILE
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
NOT_SUPPORTED_BY_GUI = 17
ERROR_NO_GUI = 18
others = 19.
* エラー処理
IF SY-SUBRC <> 0.
MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_EDIT_INFILE
*&---------------------------------------------------------------------*
* 取込データを各項目に割当&編集
*----------------------------------------------------------------------*
FORM FRM_EDIT_INFILE .
* LOOP処理
LOOP AT TD_FILE INTO TH_FILE.
* クリア処理
CLEAR: TH_INPUTDATA.
* 1行目は読まない(取込CSVファイルの1行目に項目名などをSETの場合)
CHECK SY-TABIX > 1.
* データ区切り処理
SPLIT TH_FILE AT ',' INTO TH_INPUTDATA-INPUT1
TH_INPUTDATA-INPUT2
TH_INPUTDATA-INPUT3
TH_INPUTDATA-INPUT4
TH_INPUTDATA-INPUT5.
* この時点でTH_INPUTDATAに値が入るので、以降自由にチェック&編集。
* APPEND処理
APPEND TH_INPUTDATA TO TD_INPUTDATA.
ENDLOOP.
ENDFORM. " FRM_EDIT_INFILE
0 件のコメント :
コメントを投稿