BDC另一使用方法
1、使用SHDB录屏后保存文件,点Program首先生成如下代码:
report ZTESTBDC
no standard page heading line-size 255.
include bdcrecx1.
parameters: dataset(132) lower case.
*** DO NOT CHANGE - the generated data section - DO NOT CHANGE ***
*
* If it is nessesary to change the data section use the rules:
* 1.) Each definition of a field exists of two lines
* 2.) The first line shows exactly the comment
* '* data element: ' followed with the data element
* which describes the field.
* If you don't have a data element use the
* comment without a data element name
* 3.) The second line shows the fieldname of the
* structure, the fieldname must consist of
* a fieldname and optional the character '_' and
* three numbers and the field length in brackets
* 4.) Each field must be type C.
*
*** Generated data section with specific formatting - DO NOT CHANGE ***
data: begin of record,
* data element: KUN16
KUNNR_001(016),
* data element: BUKRS
BUKRS_002(004),
* data element: VKORG
VKORG_003(004),
* data element: VTWEG
VTWEG_004(002),
* data element: VKORG
VKORG_005(004),
* data element: VTWEG
VTWEG_006(002),
* data element: KTOKD
KTOKD_007(032),
* data element: NAME1_GP
NAME1_008(035),
* data element: SORTL
SORTL_009(010),
* data element: ORT01_GP
ORT01_010(035),
* data element: LAND1_GP
LAND1_011(003),
* data element: SPRAS
SPRAS_012(002),
* data element: PSTLZ
PSTLZ_013(010),
* data element: CIVVE
CIVVE_014(001),
* data element: AKONT
AKONT_015(010),
* data element: DZTERM
ZTERM_016(004),
end of record.
*** End generated data section ***
start-of-selection.
perform open_dataset using dataset.
perform open_group.
do.
read dataset dataset into record.
if sy-subrc <> 0. exit. endif.
perform bdc_dynpro using 'SAPMF02D' '0100'.
perform bdc_field using 'BDC_CURSOR'
'RF02D-VTWEG'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_field using 'RF02D-KUNNR'
record-KUNNR_001.
perform bdc_field using 'RF02D-BUKRS'
record-BUKRS_002.
perform bdc_field using 'RF02D-VKORG'
record-VKORG_003.
perform bdc_field using 'RF02D-VTWEG'
record-VTWEG_004.
perform bdc_dynpro using 'SAPMF02D' '7100'.
perform bdc_field using 'RF02D-VKORG'
record-VKORG_005.
perform bdc_field using 'RF02D-VTWEG'
record-VTWEG_006.
perform bdc_field using 'RF02D-KTOKD'
record-KTOKD_007.
perform bdc_dynpro using 'SAPMF02D' '0110'.
perform bdc_field using 'BDC_CURSOR'
'KNA1-SPRAS'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_field using 'KNA1-NAME1'
record-NAME1_008.
perform bdc_field using 'KNA1-SORTL'
record-SORTL_009.
perform bdc_field using 'KNA1-ORT01'
record-ORT01_010.
perform bdc_field using 'KNA1-LAND1'
record-LAND1_011.
perform bdc_field using 'KNA1-SPRAS'
record-SPRAS_012.
perform bdc_field using 'KNA1-PSTLZ'
record-PSTLZ_013.
perform bdc_dynpro using 'SAPMF02D' '0120'.
perform bdc_field using 'BDC_CURSOR'
'KNA1-LIFNR'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_dynpro using 'SAPMF02D' '0125'.
perform bdc_field using 'BDC_CURSOR'
'KNA1-NIELS'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_dynpro using 'SAPMF02D' '0130'.
perform bdc_field using 'BDC_CURSOR'
'KNBK-BANKS(01)'.
perform bdc_field using 'BDC_OKCODE'
'=ENTR'.
perform bdc_dynpro using 'SAPMF02D' '0340'.
perform bdc_field using 'BDC_CURSOR'
'RF02D-KUNNR'.
perform bdc_field using 'BDC_OKCODE'
'=ENTR'.
perform bdc_dynpro using 'SAPMF02D' '0370'.
perform bdc_field using 'BDC_CURSOR'
'RF02D-KUNNR'.
perform bdc_field using 'BDC_OKCODE'
'=ENTR'.
perform bdc_field using 'KNA1-CIVVE'
record-CIVVE_014.
perform bdc_dynpro using 'SAPMF02D' '0360'.
perform bdc_field using 'BDC_CURSOR'
'KNVK-NAMEV(01)'.
perform bdc_field using 'BDC_OKCODE'
'=ENTR'.
perform bdc_dynpro using 'SAPMF02D' '0210'.
perform bdc_field using 'BDC_CURSOR'
'KNB1-AKONT'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_field using 'KNB1-AKONT'
record-AKONT_015.
perform bdc_dynpro using 'SAPMF02D' '0215'.
perform bdc_field using 'BDC_CURSOR'
'KNB1-ZTERM'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_field using 'KNB1-ZTERM'
record-ZTERM_016.
perform bdc_dynpro using 'SAPMF02D' '0220'.
perform bdc_field using 'BDC_CURSOR'
'KNB5-MAHNA'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_dynpro using 'SAPMF02D' '0230'.
perform bdc_field using 'BDC_CURSOR'
'KNB1-VRSNR'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_transaction using 'XD01'.
enddo.
perform close_group.
perform close_dataset using dataset.
-------------------------------------------------------------------------
2、按相似处(红色)拷贝至以下代码中:
*&---------------------------------------------------------------------*
*& Report ZBXK01
*&
*&---------------------------------------------------------------------*
*&
*& Author: Sunjj 2009.06.10
*&
*& 从SHDB录屏后保存,点选”Program"生成的程序后,直接拷贝到以下两处
*&---------------------------------------------------------------------*
REPORT zwunjj003.
DATA:BEGIN OF bdcdata OCCURS 0.
INCLUDE STRUCTURE bdcdata.
DATA:END OF bdcdata.
DATA: BEGIN OF record OCCURS 0, "<-----修改以下代码
* data element: KUN16
kunnr_001(016),
* data element: BUKRS
bukrs_002(004),
* data element: VKORG
vkorg_003(004),
* data element: VTWEG
vtweg_004(002),
* data element: VKORG
vkorg_005(004),
* data element: VTWEG
vtweg_006(002),
* data element: KTOKD
ktokd_007(032),
* data element: NAME1_GP
name1_008(035),
* data element: SORTL
sortl_009(010),
* data element: ORT01_GP
ort01_010(035),
* data element: LAND1_GP
land1_011(003),
* data element: SPRAS
spras_012(002),
* data element: PSTLZ
pstlz_013(010),
* data element: CIVVE
civve_014(001),
* data element: AKONT
akont_015(010),
* data element: DZTERM
zterm_016(004),
END OF record.
PARAMETERS :p_file(100) TYPE c OBLIGATORY.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM select_file.
START-OF-SELECTION.
PERFORM upload_data.
LOOP AT record.
REFRESH bdcdata. "<-----修改以下代码
PERFORM bdc_dynpro USING 'SAPMF02D' '0100'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RF02D-VTWEG'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_field USING 'RF02D-KUNNR'
record-kunnr_001.
PERFORM bdc_field USING 'RF02D-BUKRS'
record-bukrs_002.
PERFORM bdc_field USING 'RF02D-VKORG'
record-vkorg_003.
PERFORM bdc_field USING 'RF02D-VTWEG'
record-vtweg_004.
PERFORM bdc_dynpro USING 'SAPMF02D' '7100'.
PERFORM bdc_field USING 'RF02D-VKORG'
record-vkorg_005.
PERFORM bdc_field USING 'RF02D-VTWEG'
record-vtweg_006.
PERFORM bdc_field USING 'RF02D-KTOKD'
record-ktokd_007.
PERFORM bdc_dynpro USING 'SAPMF02D' '0110'.
PERFORM bdc_field USING 'BDC_CURSOR'
'KNA1-SPRAS'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_field USING 'KNA1-NAME1'
record-name1_008.
PERFORM bdc_field USING 'KNA1-SORTL'
record-sortl_009.
PERFORM bdc_field USING 'KNA1-ORT01'
record-ort01_010.
PERFORM bdc_field USING 'KNA1-LAND1'
record-land1_011.
PERFORM bdc_field USING 'KNA1-SPRAS'
record-spras_012.
PERFORM bdc_field USING 'KNA1-PSTLZ'
record-pstlz_013.
PERFORM bdc_dynpro USING 'SAPMF02D' '0120'.
PERFORM bdc_field USING 'BDC_CURSOR'
'KNA1-LIFNR'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_dynpro USING 'SAPMF02D' '0125'.
PERFORM bdc_field USING 'BDC_CURSOR'
'KNA1-NIELS'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_dynpro USING 'SAPMF02D' '0130'.
PERFORM bdc_field USING 'BDC_CURSOR'
'KNBK-BANKS(01)'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ENTR'.
PERFORM bdc_dynpro USING 'SAPMF02D' '0340'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RF02D-KUNNR'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ENTR'.
PERFORM bdc_dynpro USING 'SAPMF02D' '0370'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RF02D-KUNNR'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ENTR'.
PERFORM bdc_field USING 'KNA1-CIVVE'
record-civve_014.
PERFORM bdc_dynpro USING 'SAPMF02D' '0360'.
PERFORM bdc_field USING 'BDC_CURSOR'
'KNVK-NAMEV(01)'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ENTR'.
PERFORM bdc_dynpro USING 'SAPMF02D' '0210'.
PERFORM bdc_field USING 'BDC_CURSOR'
'KNB1-AKONT'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_field USING 'KNB1-AKONT'
record-akont_015.
PERFORM bdc_dynpro USING 'SAPMF02D' '0215'.
PERFORM bdc_field USING 'BDC_CURSOR'
'KNB1-ZTERM'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_field USING 'KNB1-ZTERM'
record-zterm_016.
PERFORM bdc_dynpro USING 'SAPMF02D' '0220'.
PERFORM bdc_field USING 'BDC_CURSOR'
'KNB5-MAHNA'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_dynpro USING 'SAPMF02D' '0230'.
PERFORM bdc_field USING 'BDC_CURSOR'
'KNB1-VRSNR'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
*perform bdc_transaction using 'XD01'.
CALL TRANSACTION 'XD01'
USING bdcdata
MODE 'E'. "A 显示所有信息, E 仅显示错误信息 N 不显示信息
* update <update mode>
* message into <message table> .
ENDLOOP.
*设置屏幕
*&---------------------------------------------------------------------*
*& Action : 添加Program/DynPro操作到BatchInput序列的内部表BDCDATA *
*& Input : PROGRAM - 程序名(长度为8的字符串)
*& DYNPRO - 画面号 *
*& Output : 无 *
*&---------------------------------------------------------------------*
FORM bdc_dynpro USING program
dynpro.
CLEAR bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
APPEND bdcdata.
ENDFORM. "BDC_DYNPRO
*设置屏幕字段
*&---------------------------------------------------------------------*
*& Action : 添加屏幕数据操作到BatchInput序列的内部表BDCDATA *
*& Input : FNAM - 屏幕项目名 *
*& FVAL - 填写项目值 *
*& Output : 无 *
*&---------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
CLEAR bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
APPEND bdcdata.
ENDFORM. "BDC_FIELD
*&---------------------------------------------------------------------*
*& Form upload_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM upload_data .
DATA l_fname TYPE string.
l_fname = p_file.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = l_fname
filetype = 'ASC'
has_field_separator = 'X'
TABLES
data_tab = record
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.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. " upload_data
*&---------------------------------------------------------------------*
*& Form SELECT_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM select_file .
DATA: l_filetab TYPE filetable,
l_waftab LIKE LINE OF l_filetab,
l_rc TYPE i.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = '打开文件'
* DEFAULT_EXTENSION =
* DEFAULT_FILENAME =
* FILE_FILTER =
* WITH_ENCODING =
initial_directory = 'C:/'
* MULTISELECTION =
CHANGING
file_table = l_filetab
rc = l_rc
* USER_ACTION =
* FILE_ENCODING =
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.
EXIT.
ELSE.
READ TABLE l_filetab INTO l_waftab INDEX 1.
p_file = l_waftab-filename.
CLEAR: l_filetab,
l_waftab.
ENDIF.
ENDFORM. "select_file
3、按录屏时输入字段顺序制作EXCEL文件,把需要导入的数据从EXCEL文件中拷贝到TXT文件中,去掉标题保存。点击程序选择TXT文件,执行,导入完毕。