批量修改数据
第一种:LSMW
LSMW不需要写代码可直接执行(事务代码)
–步骤
(1)、点击新建按钮—输入要新建文件的名字和描述
(2)、点击执行(录屏)
(3)、点击修改
(4)、点击Batch Input Recording(批量录频)— 点击小山形状的图形按钮
(5)、点击新建 — 为录屏创建名称和描述 — 点击确认 — 输入要录频的事务代码 — 回车后开始录频
(6)、执行需要录屏的步骤 — 完成后删除不需要的步骤
(7)、返回批量录频的界面,输入刚才录频的名称,点击保存
(8)、再点击执行—新建—输入名称和描述
(9)、再点击执行—选择表单—填入我要修改的字段名、描述和类型
(10)、再点击执行—在分节点下点新建
第二种:BDC
1、BDC的基本步骤:
(1)BDC录制,记录屏幕操作;
(2)产生相关的程序及数据格式文件;
(3)利用程序将相关单据信息读取到内表,并对内表的数据进行调整逻辑处理(数据检查或数据转换);
(4)调用BDC录制程序导入数据;
(5)输出消息列表。
2、主要事务代码
(1)SHDB(录屏)
(2)SM35(查看会话)
3、BDC实现的两种方式
(1)CALL TRANSACTION(调用事务)
(2)BATCH INPUT SESSION(批输入会话)
4、录屏的具体步骤:
(1)打开SHDB事务代码—点击左上角的创建记录(new recording)—输入记录的名称和需要记录哪个事务代码(记录参数–A:异步的;S:同步的;L:本地的)—点击开始记录 — 执行需要录制的操作(注:不要做多余的操作,系统会自动记录下来)—执行完之后点击保存,录屏结束
录屏结束之后在记录概览里选中刚才录的屏,点击菜单栏的程序,输入程序名,会自动生成录屏代码。
(2)录屏结果
PROGRAM—程序名称
SCREEN—界面编号
START ID—开始标志(T–事务代码的开始;X–新的界面的开始)
FIELD NAME—界面中的字段
FIELD VALUE—字段的值
(3)CALL TRANSACTION ’ 批处理的事务代码 ’ USING BDCDATA
MODE ‘A’.
——用内表BDCDATA里的数据调用批处理的事务代码
——MODE是显示模式 ( A:前台;N:后台;E:只显示错误)。
注:被调用的程序 MM02 一定要大写
DATA:messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
CALL TRANSACTION 'MM02' USING bdcdata
UPDATE 'S' "更新模式 : S--同步;A--异步
MODE 'N' "显示模式 :A--前台; N--后台 ;E--只显示错误
MESSAGES INTO messtab. "返回消息
5、数据上传,导入本地文件
(1)获取本地文件路径(F4搜索帮助)
第一种方法:通过函数WS_FILENAME_GET实现
"选择框
PARAMETERS: P_FILRS TYPE RLGRAP-FILENAME. "文件路径显示框
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_field."F4搜索帮助
PERFORM get_filename.
*&---------------------------------------------------------------------*
*& Form GET_FILENAME
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_FILENAME.
CALL FUNCTION 'WS_FILENAME_GET' "用于选择本地文件,获取本地文件的路径
EXPORTING
* DEF_FILENAME = ' ' "默认的文件名称
* DEF_PATH = ' ' "默认的文件地址
* MASK = ',EXCEL.XLS,*.XLSX,*.XLS,TEXT.TXT,*.TXT.'"每两个逗号显示一组
mode = 'O' "S 保存 O 打开
title = '选择文件' "窗口的显示名称
IMPORTING "传出给选择屏幕上的文本框
filename = p_field "PARAMETERS的值
* RC =
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
第二种方法:通过面向对象实现
PARAMETERS: P_FILRS TYPE RLGRAP-FILENAME. "文件路径显示框
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_field."F4搜索帮助
PERFORM get_filename.
*&---------------------------------------------------------------------*
*& Form GET_FILENAME
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_filename .
DATA: lit_file TYPE filetable,
l_file_filter TYPE string,
l_rc TYPE i.
FIELD-SYMBOLS: <fs_file> LIKE LINE OF lit_file.
l_file_filter = 'All files (*.*)|*.*|Excel Template (*.xlt)|*.xlt|Excel Workbook (*.xls)|*.xls'."默认文件类型 ,'|'前的为一组
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = '选择文件'
default_extension = '*.*'
" file_filter = l_file_filter "放开则只显示默认文件类型,注释则会显示所有文件类型
CHANGING
file_table = lit_file
rc = l_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.
READ TABLE lit_file ASSIGNING <fs_file> INDEX 1.
IF sy-subrc = 0.
p_field = <fs_file>-filename.
ENDIF.
ENDIF.
(2)从文件中获取数据
*1、TXT文件的上传
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = ”string类型
* FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = 'X '
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* VIRUS_SCAN_PROFILE =
* NO_AUTH_CHECK = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
tables
data_tab =
* CHANGING
* ISSCANPERFORMED = ' '
* 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.
* Implement suitable error handling here
ENDIF.
*2、EXCEL文件的上传
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
* I_FIELD_SEPERATOR =
* I_LINE_HEADER =
i_tab_raw_data =
i_filename = “ 上传文件路径
TABLES
i_tab_converted_data =
* EXCEPTIONS
* CONVERSION_FAILED = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
6、BDC批量修改数据执行时,退出当前BDC的方法
在t_code输入:
/N — 退出当前步骤
/O — 退出之后的所有步骤
7、源代码:
可直接执行,但是必须要在SHDB中创建MM02的录屏
录屏修改一下字段:
REPORT z_test_wj03.
TABLES:sscrfields.
TYPES:BEGIN OF ts_bdc,
matnr TYPE char18,
maktx TYPE char40,
brgew TYPE char13,
ntgew TYPE char13,
gewei TYPE char3,
END OF ts_bdc.
DATA:gt_bdc TYPE TABLE OF ts_bdc.
DATA:gs_bdc TYPE ts_bdc.
DATA:BEGIN OF gt_template OCCURS 0,
tab1 TYPE char10 VALUE '物料代码',
tab2 TYPE char10 VALUE '物料描述',
tab3 TYPE char10 VALUE '毛重量',
tab4 TYPE char10 VALUE '净重量',
tab5 TYPE char10 VALUE '重量单位',
END OF gt_template.
DATA:bdcdata TYPE TABLE OF bdcdata WITH HEADER LINE.
*DATA: BEGIN OF bdcdata OCCURS 0.
* INCLUDE STRUCTURE bdcdata.
*DATA: END OF bdcdata.
DATA: BEGIN OF messtab OCCURS 0.
INCLUDE STRUCTURE bdcmsgcoll.
DATA: END OF messtab.
PARAMETERS:p_field TYPE rlgrap-filename.
SELECTION-SCREEN FUNCTION KEY 1."数字1~5,对应界面中可以增加的五个按钮。
INITIALIZATION.
WRITE icon_change_text AS ICON TO sscrfields-functxt_01+0(4).
sscrfields-functxt_01+4(*) = '下载模板'.
AT SELECTION-SCREEN.
PERFORM selection_screen_pai.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_field."F4搜索帮助
PERFORM get_filename.
START-OF-SELECTION.
PERFORM get_data.
PERFORM set_data.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_data .
*从文件中获取数据
*1、上传TXT文件
*(1)由于函数GUI_UPLOAD里的文件路径P_FIELD的类型是String,
* 而筛选界面PARAMETERS的文件路径P_FIELD是Char类型,
* 所以要进行类型转换
DATA:lv_files TYPE string.
* lv_hz0 TYPE string,
* lv_hz TYPE string.
CHECK p_field IS NOT INITIAL."检查上传的文件路径是否为空,如果为空则不执行以下语句。
TRANSLATE p_field TO UPPER CASE."将p_field中的文件路径转换为大写
SPLIT p_field AT '.' INTO DATA(lv_hz0) DATA(lv_hz).
CASE lv_hz.
WHEN 'TXT'.
* FIND '.TXT' IN p_field.
* IF sy-subrc = 0.
lv_files = p_field.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = lv_files "选择上传的路径
* FILETYPE = 'ASC'
has_field_separator = 'X'
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* VIRUS_SCAN_PROFILE =
* NO_AUTH_CHECK = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
data_tab = gt_bdc "输出内表
* CHANGING
* isscanperformed = ' '
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.
* ELSE.
WHEN 'XLS' OR 'XLSX'.
*2、上传EXCEl文件
* FIND '.XLS' IN p_field.
* IF sy-subrc = 0 .
DATA:lt_raw TYPE truxs_t_text_data."EXCEL的内表
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
i_field_seperator = 'X'
i_line_header = 'X' "上传的EXCEL是否有表头
i_tab_raw_data = lt_raw
i_filename = p_field "上传的文件路径
TABLES
i_tab_converted_data = gt_bdc "输出内表
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
* ENDIF.
* ENDIF.
ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM set_data .
IF gt_bdc IS NOT INITIAL.
LOOP AT gt_bdc INTO gs_bdc.
CLEAR:bdcdata,bdcdata[],messtab,messtab[].
PERFORM append_screen USING 'SAPLMGMM' '0060'.
PERFORM append_fields USING 'BDC_CURSOR' 'RMMG1-MATNR'.
PERFORM append_fields USING 'BDC_OKCODE' '=ENTR'.
PERFORM append_fields USING 'RMMG1-MATNR' gs_bdc-matnr.
PERFORM append_screen USING 'SAPLMGMM' '0070'.
PERFORM append_fields USING 'BDC_CURSOR' 'MSICHTAUSW-DYTXT(01)'.
PERFORM append_fields USING 'BDC_OKCODE' '=ENTR'.
PERFORM append_fields USING 'MSICHTAUSW-KZSEL(01)' 'X'.
PERFORM append_screen USING 'SAPLMGMM' '4004'.
PERFORM append_fields USING 'BDC_OKCODE' '=BU'.
PERFORM append_fields USING 'BDC_CURSOR' 'MAKT-MAKTX'.
PERFORM append_fields USING 'MAKT-MAKTX' gs_bdc-maktx.
PERFORM append_fields USING 'MARA-BRGEW' gs_bdc-brgew.
PERFORM append_fields USING 'MARA-GEWEI' gs_bdc-gewei.
PERFORM append_fields USING 'MARA-NTGEW' gs_bdc-ntgew.
CALL TRANSACTION 'MM02' USING bdcdata
UPDATE 'S' "更新模式 : S--同步;A--异步
MODE 'N' "显示模式 :A--前台; N--后台 ;E--只显示错误
MESSAGES INTO messtab.
IF sy-subrc = 0.
READ TABLE messtab WITH KEY msgid = 'M3'
msgnr = '801'."物料修改成功
IF sy-subcs = 0 .
WRITE:/ '物料' , gs_bdc-matnr,'修改成功!'.
ELSE.
WRITE:/ '物料修改失败'.
ENDIF.
ELSE.
MESSAGE '物料修改失败!' TYPE 'E'.
ENDIF.
CLEAR:gs_bdc.
ENDLOOP.
ELSE.
MESSAGE '修改失败,没有数据!' TYPE 'E'.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form APPEND_SCREEN
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0089 text
* -->P_0090 text
*----------------------------------------------------------------------*
FORM append_screen USING VALUE(p_program)
VALUE(p_dynpro).
CLEAR:bdcdata.
bdcdata-program = p_program.
bdcdata-dynpro = p_dynpro.
bdcdata-dynbegin = 'X'.
APPEND bdcdata.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form APPEND_FIELDS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0094 text
* -->P_0095 text
*----------------------------------------------------------------------*
FORM append_fields USING VALUE(p_fnam)
VALUE(p_fval).
CLEAR:bdcdata.
bdcdata-fnam = p_fnam.
bdcdata-fval = p_fval.
APPEND bdcdata.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form GET_FILENAME
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_filename .
"1、面向对象的搜索帮助方法
DATA: lit_file TYPE filetable,
l_file_filter TYPE string,
l_rc TYPE i.
FIELD-SYMBOLS: <fs_file> LIKE LINE OF lit_file.
l_file_filter = 'All files (*.*)|*.*|Excel Template (*.xlt)|*.xlt|Excel Workbook (*.xls)|*.xls'."默认文件类型
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = '选择文件'
default_extension = '*.*' "默认扩展名
* file_filter = l_file_filter "放开则只显示默认文件类型,注释则会显示所有文件类型
CHANGING
file_table = lit_file
rc = l_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.
READ TABLE lit_file ASSIGNING <fs_file> INDEX 1.
IF sy-subrc = 0.
p_field = <fs_file>-filename.
ENDIF.
ENDIF.
"2、普通的搜索帮助方法
* CALL FUNCTION 'WS_FILENAME_GET' "用于选择本地文件,获取本地文件的路径
* EXPORTING
** DEF_FILENAME = ' ' "默认的文件名称
** DEF_PATH = ' ' "默认的文件地址
** MASK = ',EXCEL.XLS,*.XLSX,*.XLS,TEXT.TXT,*.TXT.'
* mode = 'O' "S 保存 O 打开
* title = '选择文件' "窗口的显示名称
* IMPORTING "传出给选择屏幕上的文本框
* filename = p_field
** RC =
* EXCEPTIONS
* inv_winsys = 1
* no_batch = 2
* selection_cancel = 3
* selection_error = 4
* OTHERS = 5.
* IF sy-subrc <> 0.
** Implement suitable error handling here
* ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SELECTION_SCREEN_PAI
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM selection_screen_pai .
CASE sscrfields-ucomm.
WHEN 'FC01'.
PERFORM download_template."模板下载的功能
ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_TEMPLATE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM download_template .
DATA: l_filename TYPE string,
l_path TYPE string,
l_fullpath TYPE string.
REFRESH gt_template."清空一下表体,然后从表头行增加这个表体内容
APPEND gt_template.
"1、获取文件保存路径
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = '下载文件'
default_extension = '*.*' "默认扩展名
* default_file_name =
* with_encoding =
file_filter = 'Excel Workbook (*.xls)|*.xls' "默认文件类型
* initial_directory =
* prompt_on_overwrite = 'X'
CHANGING
filename = l_filename
path = l_path
fullpath = l_fullpath
* user_action =
* file_encoding =
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
invalid_default_file_name = 4
OTHERS = 5.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CHECK l_filename IS NOT INITIAL.
"2、将内表下载到指定路径的文件下
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
* BIN_FILESIZE =
filename = l_filename
* FILETYPE = 'ASC'
* APPEND = ' '
write_field_separator = 'X'
* HEADER = '00'
* TRUNC_TRAILING_BLANKS = ' '
* WRITE_LF = 'X'
* COL_SELECT = ' '
* COL_SELECT_MASK = ' '
* DAT_MODE = ' '
* CONFIRM_OVERWRITE = ' '
* NO_AUTH_CHECK = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* WRITE_BOM = ' '
* TRUNC_TRAILING_BLANKS_EOL = 'X'
* WK1_N_FORMAT = ' '
* WK1_N_SIZE = ' '
* WK1_T_FORMAT = ' '
* WK1_T_SIZE = ' '
* WRITE_LF_AFTER_LAST_LINE = ABAP_TRUE
* SHOW_TRANSFER_STATUS = ABAP_TRUE
* VIRUS_SCAN_PROFILE = '/SCET/GUI_DOWNLOAD'
* IMPORTING
* FILELENGTH =
TABLES
data_tab = gt_template
* FIELDNAMES = "每列描述的内表(CHAR类型 一个字段)
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.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.