导语:最近在做固定资产批导,使用到BAPI:【BAPI_FIXEDASSET_OVRTAKE_CREATE】,做个记录,批导模板的字段是wa_excel这个结构的全部字段。
代码
*&---------------------------------------------------------------------*
*& Report ZFIR003A
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zfir003a MESSAGE-ID zsfi.
TYPE-POOLS:slis.
"加载需要表
TABLES: anlb.
"Excel上传标准内表
DATA: BEGIN OF iexcel OCCURS 0.
INCLUDE STRUCTURE alsmex_tabline.
DATA: END OF iexcel.
TABLES sscrfields. "定义按钮
DATA: ls_textfield LIKE smp_dyntxt.
"导入Excel转换后内表格式
DATA: BEGIN OF wa_excel,
assetclass LIKE bapi1022_feglg001-assetclass, "资产分类
companycode LIKE bapi1022_key-companycode, "公司代码
descript LIKE bapi1022_feglg001-descript, "资产描述
descript2 LIKE bapi1022_feglg001-descript2, "附加资产描述
main_descript LIKE bapi1022_feglg001-main_descript, "资产主号说明
serial_no LIKE bapi1022_feglg001-serial_no, "序列号
invent_no LIKE bapi1022_feglg001-invent_no, "存货号
quantity LIKE bapi1022_feglg001-quantity, "数量
base_uom LIKE bapi1022_feglg001-base_uom, "基本计量单位
cap_date LIKE bapi1022_feglg002-cap_date, "资产资本化日期
costcenter LIKE bapi1022_feglg003-costcenter, "成本中心
resp_cctr LIKE bapi1022_feglg003-resp_cctr, "归口成本中心
plant LIKE bapi1022_feglg003-plant, "工厂
* room LIKE bapi1022_feglg003-room, "房间
vendor LIKE bapi1022_feglg009-vendor, "供应商描述
* area LIKE bapi1022_dep_areas-area, "折旧范围
dep_key LIKE bapi1022_dep_areas-dep_key, "折旧码
ulife_yrs LIKE bapi1022_dep_areas-ulife_yrs, "使用年限
ulife_prds LIKE bapi1022_dep_areas-ulife_prds, "期间
exp_ulife_yrs LIKE bapi1022_dep_areas-exp_ulife_yrs, "在财会年度开始时到期的有用年限
exp_ulife_prds LIKE bapi1022_dep_areas-exp_ulife_prds, "会计年起始时到期使用期限
acq_value LIKE bapi1022_cumval-acq_value, "原值
l_ord_dep LIKE bapi1022_cumval-ord_dep, "累计折旧
m_ord_dep LIKE bapi1022_postval-ord_dep, "本年折旧
scrapvalue LIKE bapi1022_dep_areas-scrapvalue, "资产残值
* area1 LIKE bapi1022_dep_areas-area, "折旧范围
dep_key1 LIKE bapi1022_dep_areas-dep_key, "折旧码
ulife_yrs1 LIKE bapi1022_dep_areas-ulife_yrs, "使用年限
ulife_prds1 LIKE bapi1022_dep_areas-ulife_prds, "期间
exp_ulife_yrs1 LIKE bapi1022_dep_areas-exp_ulife_yrs, "在财会年度开始时到期的有用年限
exp_ulife_prds1 LIKE bapi1022_dep_areas-exp_ulife_prds, "会计年起始时到期使用期限
acq_value1 LIKE bapi1022_cumval-acq_value, "原值
l_ord_dep1 LIKE bapi1022_cumval-ord_dep, "累计折旧
m_ord_dep1 LIKE bapi1022_postval-ord_dep, "本年折旧
scrapvalue1 LIKE bapi1022_dep_areas-scrapvalue, "资产残
ord41 LIKE anla-ord41, "评审小组1
ord42 LIKE anla-ord42, "评审小组2
intern_ord LIKE bapi1022_feglg003-intern_ord, "内部订单
wbs_element_cost LIKE bapi1022_feglg003-wbs_element_cost, "内部订单
zccwz LIKE anlu-zccwz, "位置
END OF wa_excel,
it_excel LIKE STANDARD TABLE OF wa_excel.
"以下定义BAPI导入结构
DATA: key LIKE bapi1022_key, "要创建的关键字字段(公司代码、主资产号)
generaldata LIKE bapi1022_feglg001, "资产通用数据
generaldatax LIKE bapi1022_feglg001x, "资产通用数据确定
postinginformation LIKE bapi1022_feglg002, "过帐数据
postinginformationx LIKE bapi1022_feglg002x, "过帐数据确定
timedependentdata LIKE bapi1022_feglg003, "资产时间相关数据
timedependentdatax LIKE bapi1022_feglg003x, "资产时间相关数据确定
origin LIKE bapi1022_feglg009, "来源数据
originx LIKE bapi1022_feglg009x, "来源数据确定
allocations LIKE bapi1022_feglg004,
allocationsx LIKE bapi1022_feglg004x.
"以下定义BAPI导入内表
DATA: depreciationareas LIKE STANDARD TABLE OF bapi1022_dep_areas, "资产折旧信息
wa_depreciationareas LIKE bapi1022_dep_areas, "资产折旧信息
depreciationareasx LIKE STANDARD TABLE OF bapi1022_dep_areasx, "资产折旧住处确定
wa_depreciationareasx LIKE bapi1022_dep_areasx, "资产折旧住处确定
cumulatedvalues LIKE STANDARD TABLE OF bapi1022_cumval, "往年折旧计算值
wa_cumulatedvalues LIKE bapi1022_cumval, "往年折旧计算值
postedvalues LIKE STANDARD TABLE OF bapi1022_postval, "当年折旧计算值
wa_postedvalues LIKE bapi1022_postval, "当年折旧计算值
transactions LIKE STANDARD TABLE OF bapi1022_trtype, "当年折旧金额
wa_transactions LIKE bapi1022_trtype, "当年折旧金额
wa_extensionin TYPE bapiparex , "增强字段
extensionin LIKE STANDARD TABLE OF bapiparex . "增强字段
DATA: ls_te_anlu TYPE bapi_te_anlu.
"以下定义BAPI导入时的输出结果
DATA: return LIKE STANDARD TABLE OF bapiret2,
wa_return LIKE bapiret2,
companycode LIKE bapi1022_1-comp_code,
asset LIKE bapi1022_1-assetmaino,
subnumber LIKE bapi1022_1-assetsubno.
"定义消息输出内表
DATA: BEGIN OF wa_tab,
assetclass LIKE bapi1022_feglg001-assetclass, "资产分类
companycode LIKE bapi1022_key-companycode, "公司代码
asset LIKE bapi1022_1-assetmaino,
subnumber LIKE bapi1022_1-assetsubno,
invent_no LIKE bapi1022_feglg001-invent_no,
descript LIKE bapi1022_feglg001-descript, "资产描述
type LIKE bapiret2-type,
id LIKE bapiret2-id,
number LIKE bapiret2-number,
message LIKE bapiret2-message,
END OF wa_tab,
it_tab LIKE STANDARD TABLE OF wa_tab.
"定义ALV输出所需变量
DATA: fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE, " 定义 ALV的有关数据
layout TYPE slis_layout_alv, " 定义 ALV的有关数据
ivariant LIKE disvariant, " 定义 ALV的有关数据
i_repid LIKE sy-repid. " 定义 ALV的有关数据
DATA:g_file TYPE sapb-sappfad.
* 处理Tab分割符
CLASS cl_abap_char_utilities DEFINITION LOAD.
CONSTANTS: c_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.
"定义临时变量
DATA msg TYPE string.
"导入文件选择
SELECTION-SCREEN BEGIN OF BLOCK 1 WITH FRAME TITLE TEXT-001.
*PARAMETERS: P_FNAME LIKE RLGRAP-FILENAME MEMORY ID M01 OBLIGATORY. " 获取文件路径
PARAMETERS: p_file TYPE localfile .
SELECTION-SCREEN END OF BLOCK 1.
"附加条件选择
SELECTION-SCREEN BEGIN OF BLOCK 2 WITH FRAME TITLE TEXT-002.
PARAMETERS: p_ly TYPE gjahr OBLIGATORY,
p_my TYPE gjahr OBLIGATORY,
p_vd TYPE bf_bzdat.
SELECT-OPTIONS: dep_area FOR anlb-afabe NO INTERVALS.
SELECTION-SCREEN END OF BLOCK 2.
"其它附加条件选择
SELECTION-SCREEN BEGIN OF BLOCK 3 WITH FRAME TITLE TEXT-003.
PARAMETERS: r_l TYPE c RADIOBUTTON GROUP r1,
r_m TYPE c RADIOBUTTON GROUP r1.
PARAMETERS: tr AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN END OF BLOCK 3.
SELECTION-SCREEN FUNCTION KEY 1.
*
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_FILE.
*PERFORM get_p_fname. " 把需要导入的文件的路径 存放在P_name 中
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
* Get file full name using dialog
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
mask = p_file
static = ' '
CHANGING
file_name = p_file.
INITIALIZATION.
ls_textfield-icon_id = '@EZ@'.
ls_textfield-icon_text = TEXT-004. "下载导数模板
sscrfields-functxt_01 = ls_textfield.
"自动计算本年与往年
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
* Get file full name using dialog
* CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
* EXPORTING
* mask = p_file
* static = ' '
* CHANGING
* file_name = p_file.
AT SELECTION-SCREEN.
PERFORM frm_download_mod.
START-OF-SELECTION.
MOVE sy-datum+0(4) TO p_my.
* p_ly = p_my - 1.
PERFORM frm_check.
* PERFORM FORM_UPLOAD. "通过路径 把EXCEL中的数据导入到 内表iexcel中
PERFORM upload_file USING p_file CHANGING it_excel[].
PERFORM frm_bapi_ext. "执行BAPI,并输出结果信息
PERFORM frm_alv. "结果输出显示
*FORM get_p_fname. " 获取文件路径
* CALL FUNCTION 'WS_FILENAME_GET'
* EXPORTING
* mask = ',Excel Files,*.xls,All Files,*.csv,*.*.'
* title = '请选择要导入的文件'
* IMPORTING
* filename = p_fILE
* EXCEPTIONS
* inv_winsys = 1
* no_batch = 2
* selection_cancel = 3
* selection_error = 4
* OTHERS = 5.
* IF sy-subrc <> 0 AND sy-subrc <> 3.
* MESSAGE e100(zdev) WITH 'error'(007).
* ENDIF.
*ENDFORM.
*---------------------------------------------------------------------*
FORM upload_file USING in_file TYPE localfile
CHANGING data_tab TYPE STANDARD TABLE.
DATA: lt_excel TYPE TABLE OF alsmex_tabline WITH HEADER LINE.
FIELD-SYMBOLS: <fs_val>.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_file
i_begin_col = '2' "从第一列开始
i_begin_row = '3' "从第二行开始
i_end_col = '50'
i_end_row = '50000'
TABLES
intern = lt_excel
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
LOOP AT lt_excel.
CONDENSE lt_excel-value.
ASSIGN COMPONENT lt_excel-col OF STRUCTURE wa_excel TO <fs_val>.
<fs_val> = lt_excel-value.
AT END OF row.
APPEND wa_excel TO data_tab.
CLEAR wa_excel.
ENDAT.
ENDLOOP.
* DATA: l_filetype TYPE char10.
* DATA: l_file TYPE string.
* DATA: l_itab TYPE STANDARD TABLE OF itab.
*
** cl_gui_frontend_services=>gui_upload will call GUI_UPLOAD function
** in which the filename parameter is of type String
** so we need to convert
* l_file = in_file.
*
* CALL METHOD cl_gui_frontend_services=>gui_upload
* EXPORTING
* filename = l_file " upload file
* filetype = 'ASC'
* has_field_separator = c_tab " Has separator
* CHANGING
* data_tab = data_tab " import to this intenal table
* EXCEPTIONS
* file_open_error = 1
* file_read_error = 2
* invalid_type = 3
* no_batch = 4
* unknown_error = 5
* OTHERS = 6.
*
* IF sy-subrc <> 0.
* MESSAGE e001 WITH '文件上传出错.'.
*
* ENDIF.
ENDFORM.
FORM frm_alv.
PERFORM fieldcat_init.
layout-colwidth_optimize = 'X'. "设置自动调整列宽
layout-zebra = 'X'. " 设置相邻行颜色不同 这样便于观看
i_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = i_repid
it_fieldcat = fieldcat[]
is_variant = ivariant
is_layout = layout
TABLES
t_outtab = it_tab
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM.
FORM fieldcat_init.
PERFORM frm_catlg_set USING:
'ASSETCLASS' '资产分类', " '' 里面的字母需要大写
'COMPANYCODE' '公司代码',
'ASSET' '资产编码',
'SUBNUMBER' '资产子编码',
'INVENT_NO' '存货号',
'DESCRIPT' '资产描述',
'TYPE' '消息类型',
'ID' '消息ID',
'NUMBER' '消息编码',
'MESSAGE' '消息文本'.
ENDFORM.
FORM frm_catlg_set USING p_field p_text.
DATA: temp_fieldcat TYPE slis_fieldcat_alv.
temp_fieldcat-fieldname = p_field.
temp_fieldcat-seltext_l = p_text.
APPEND temp_fieldcat TO fieldcat.
CLEAR temp_fieldcat.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_BAPI_EXT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_bapi_ext .
"循环内表数据,准备导入
LOOP AT it_excel INTO wa_excel.
"输出处理信息
msg = sy-tabix.
CONCATENATE TEXT-s03 msg TEXT-s04 INTO msg.
"处理数据提示
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
text = msg.
"处理关键值数据
key-companycode = wa_excel-companycode.
"对资产类增加前导0
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = wa_excel-assetclass
IMPORTING
output = generaldata-assetclass.
"处理一般数据
* GENERALDATA-ASSETCLASS = WA_EXCEL-ASSETCLASS.
generaldata-descript = wa_excel-descript.
generaldata-descript2 = wa_excel-descript2.
generaldata-main_descript = wa_excel-main_descript.
generaldata-serial_no = wa_excel-serial_no.
generaldata-invent_no = wa_excel-invent_no.
generaldata-quantity = wa_excel-quantity.
generaldata-base_uom = wa_excel-base_uom.
generaldatax-assetclass = 'X'.
generaldatax-descript = 'X'.
generaldatax-descript2 = 'X'.
generaldatax-main_descript = 'X'.
generaldatax-serial_no = 'X'.
generaldatax-invent_no = 'X'.
generaldatax-quantity = 'X'.
generaldatax-base_uom = 'X'.
"处理过帐数据
postinginformation-cap_date = wa_excel-cap_date.
postinginformationx-cap_date = 'X'.
"成本中心数据
timedependentdata-costcenter = wa_excel-costcenter.
timedependentdata-resp_cctr = wa_excel-resp_cctr.
timedependentdata-plant = wa_excel-plant.
timedependentdata-intern_ord = wa_excel-intern_ord.
timedependentdata-wbs_element_cost = wa_excel-wbs_element_cost.
* timedependentdata-room = wa_excel-room.
timedependentdatax-costcenter = 'X'.
timedependentdatax-resp_cctr = 'X'.
timedependentdatax-plant = 'X'.
timedependentdatax-intern_ord = 'X'.
timedependentdatax-wbs_element_cost = 'X'.
* timedependentdatax-room = 'X'.
"处理供应商数据
origin-vendor = wa_excel-vendor.
originx-vendor = 'X'.
"循环处理折旧范围数据
* LOOP AT dep_area.
"增加折旧范围数据
wa_depreciationareas-area = '01'.
wa_depreciationareas-dep_key = wa_excel-dep_key.
wa_depreciationareas-ulife_yrs = wa_excel-ulife_yrs.
wa_depreciationareas-ulife_prds = wa_excel-ulife_prds.
wa_depreciationareas-exp_ulife_yrs = wa_excel-exp_ulife_yrs.
wa_depreciationareas-exp_ulife_prds = wa_excel-exp_ulife_prds.
wa_depreciationareas-scrapvalue = wa_excel-scrapvalue.
wa_depreciationareasx-area = '01'.
wa_depreciationareasx-dep_key = 'X'.
wa_depreciationareasx-ulife_yrs = 'X'.
wa_depreciationareasx-ulife_prds = 'X'.
wa_depreciationareasx-exp_ulife_yrs = 'X'.
wa_depreciationareasx-exp_ulife_prds = 'X'.
wa_depreciationareasx-scrapvalue = 'X'.
APPEND wa_depreciationareas TO depreciationareas.
APPEND wa_depreciationareasx TO depreciationareasx.
"处理资产价值接管数据
IF r_l = 'X'. "处理往年数据
"增加累计折旧
wa_cumulatedvalues-fisc_year = p_ly.
wa_cumulatedvalues-area = '01'.
wa_cumulatedvalues-acq_value = wa_excel-acq_value.
wa_cumulatedvalues-ord_dep = wa_excel-l_ord_dep.
APPEND wa_cumulatedvalues TO cumulatedvalues.
"增加正常折旧
wa_postedvalues-fisc_year = p_my.
wa_postedvalues-area = '01'.
wa_postedvalues-ord_dep = wa_excel-m_ord_dep.
APPEND wa_postedvalues TO postedvalues.
ELSE. "处理当年数据
"增加正常折旧
wa_postedvalues-fisc_year = p_my.
wa_postedvalues-area = '01'.
wa_postedvalues-ord_dep = wa_excel-m_ord_dep.
APPEND wa_postedvalues TO postedvalues.
"增加购置价值
wa_transactions-fisc_year = p_my.
wa_transactions-area = '01'.
wa_transactions-current_no = sy-tabix.
wa_transactions-valuedate = p_vd.
wa_transactions-assettrtyp = '100'.
wa_transactions-amount = wa_excel-acq_value.
APPEND wa_transactions TO transactions.
ENDIF.
"清空结构体中的数据
CLEAR: wa_depreciationareas,
wa_depreciationareasx,
wa_cumulatedvalues,
wa_postedvalues,
wa_transactions.
* ENDLOOP.
wa_depreciationareas-area = '80'.
wa_depreciationareas-dep_key = wa_excel-dep_key1.
wa_depreciationareas-ulife_yrs = wa_excel-ulife_yrs1.
wa_depreciationareas-ulife_prds = wa_excel-ulife_prds1.
wa_depreciationareas-exp_ulife_yrs = wa_excel-exp_ulife_yrs1.
wa_depreciationareas-exp_ulife_prds = wa_excel-exp_ulife_prds1.
wa_depreciationareas-scrapvalue = wa_excel-scrapvalue1.
wa_depreciationareasx-area = '80'.
wa_depreciationareasx-dep_key = 'X'.
wa_depreciationareasx-ulife_yrs = 'X'.
wa_depreciationareasx-ulife_prds = 'X'.
wa_depreciationareasx-exp_ulife_yrs = 'X'.
wa_depreciationareasx-exp_ulife_prds = 'X'.
wa_depreciationareasx-scrapvalue = 'X'.
APPEND wa_depreciationareas TO depreciationareas.
APPEND wa_depreciationareasx TO depreciationareasx.
CLEAR :allocationsx,allocations.
allocations-evalgroup1 = wa_excel-ord41.
allocations-evalgroup2 = wa_excel-ord42.
allocationsx-evalgroup1 = 'X'.
allocationsx-evalgroup2 = 'X'.
"处理资产价值接管数据
IF r_l = 'X'. "处理往年数据
"增加累计折旧
wa_cumulatedvalues-fisc_year = p_ly.
wa_cumulatedvalues-area = '80'.
wa_cumulatedvalues-acq_value = wa_excel-acq_value1.
wa_cumulatedvalues-ord_dep = wa_excel-l_ord_dep1.
APPEND wa_cumulatedvalues TO cumulatedvalues.
"增加正常折旧
wa_postedvalues-fisc_year = p_my.
wa_postedvalues-area = '80'.
wa_postedvalues-ord_dep = wa_excel-m_ord_dep1.
APPEND wa_postedvalues TO postedvalues.
ELSE. "处理当年数据
"增加正常折旧
wa_postedvalues-fisc_year = p_my.
wa_postedvalues-area = '80'.
wa_postedvalues-ord_dep = wa_excel-m_ord_dep1.
APPEND wa_postedvalues TO postedvalues.
"增加购置价值
wa_transactions-fisc_year = p_my.
wa_transactions-area = '80'.
wa_transactions-current_no = sy-tabix.
wa_transactions-valuedate = p_vd.
wa_transactions-assettrtyp = '100'.
wa_transactions-amount = wa_excel-acq_value1.
APPEND wa_transactions TO transactions.
ENDIF.
ls_te_anlu-comp_code = wa_excel-companycode.
* ls_te_anlu-assetmaino = wa_excel-anln1.
ls_te_anlu-assetsubno = '0000'.
ls_te_anlu-zccwz = wa_excel-zccwz.
wa_extensionin-structure = 'BAPI_TE_ANLU'.
wa_extensionin-valuepart1 = ls_te_anlu.
APPEND wa_extensionin TO extensionin.
"清空结构体中的数据
CLEAR: wa_depreciationareas,
wa_depreciationareasx,
wa_cumulatedvalues,
wa_postedvalues,
wa_transactions.
"调用系统标准BAPI
CALL FUNCTION 'BAPI_FIXEDASSET_OVRTAKE_CREATE'
EXPORTING
key = key
* REFERENCE =
* CREATESUBNUMBER =
* CREATEGROUPASSET =
testrun = tr
generaldata = generaldata
generaldatax = generaldatax
* INVENTORY =
* INVENTORYX =
postinginformation = postinginformation
postinginformationx = postinginformationx
timedependentdata = timedependentdata
timedependentdatax = timedependentdatax
allocations = allocations
allocationsx = allocationsx
origin = origin
originx = originx
* INVESTACCTASSIGNMNT =
* INVESTACCTASSIGNMNTX =
* NETWORTHVALUATION =
* NETWORTHVALUATIONX =
* REALESTATE =
* REALESTATEX =
* INSURANCE =
* INSURANCEX =
* LEASING =
* LEASINGX =
IMPORTING
companycode = companycode
asset = asset
subnumber = subnumber
* ASSETCREATED =
TABLES
depreciationareas = depreciationareas
depreciationareasx = depreciationareasx
* INVESTMENT_SUPPORT =
extensionin = extensionin
cumulatedvalues = cumulatedvalues
postedvalues = postedvalues
transactions = transactions
* PROPORTIONALVALUES =
return = return.
"提交作业
COMMIT WORK.
"处理输出消息
wa_tab-assetclass = wa_excel-assetclass.
wa_tab-companycode = wa_excel-companycode.
wa_tab-asset = asset.
wa_tab-subnumber = subnumber.
wa_tab-invent_no = wa_excel-invent_no.
wa_tab-descript = wa_excel-descript.
READ TABLE return INTO wa_return INDEX 1.
IF sy-subrc = 0.
wa_tab-type = wa_return-type.
wa_tab-id = wa_return-id.
wa_tab-number = wa_return-number.
wa_tab-message = wa_return-message.
ENDIF.
APPEND wa_tab TO it_tab.
"清空所有临时数据
CLEAR: wa_excel,
generaldata,
generaldatax,
postinginformation,
postinginformationx,
timedependentdata,
timedependentdatax,
origin,
originx,
depreciationareas,
depreciationareasx,
cumulatedvalues,
postedvalues,
transactions,
return,
wa_return,
wa_tab.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CHECK
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_check .
"当如果为本年时,参考日期为必输
IF r_m = 'X'.
IF p_vd IS INITIAL.
MESSAGE TEXT-m02 TYPE 'I'.
STOP.
ENDIF.
ENDIF.
ENDFORM.
FORM frm_get_excel_templet USING objid filename.
DATA i_objid TYPE string VALUE ''. "模板对象名
DATA is_back_run. " "是否显示路径选择
DATA c_destination TYPE string VALUE 'D:\'. " 路径
DATA i_filename TYPE string VALUE ''. "模板的EXCEL文件名
DATA: l_objdata TYPE wwwdatatab, "查找Web资源对象关键字
l_rc LIKE sy-subrc,
lc_filename_temp TYPE string,
lc_fullpath TYPE string VALUE '',
lc_path TYPE string VALUE '',
ls_destination TYPE rlgrap-filename.
"取得对应的模板,并判断是否存在
i_objid = objid.
i_filename = filename.
SELECT SINGLE relid objid FROM wwwdata INTO CORRESPONDING FIELDS OF l_objdata WHERE srtf2 = 0 AND relid = 'HT' AND objid = i_objid.
IF sy-subrc NE 0 AND l_objdata-objid EQ space.
MESSAGE '没有找到相应的模板!' TYPE 'S' DISPLAY LIKE 'E'.
STOP.
* EXIT.
ENDIF.
IF is_back_run = 'X'.
CONCATENATE c_destination '\' i_filename '.xls' INTO lc_fullpath.
ELSE.
"用户选择对应的路径
lc_filename_temp = i_filename.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
default_extension = 'xls'
default_file_name = i_filename
initial_directory = c_destination
CHANGING
filename = lc_filename_temp
path = lc_path
fullpath = lc_fullpath.
IF lc_fullpath EQ ''.
MESSAGE '模板未能成功保存' TYPE 'S' DISPLAY LIKE 'E'.
STOP.
* EXIT.
ENDIF.
ENDIF.
"如果模板存在,则下载到用户电脑本地
ls_destination = lc_fullpath.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = l_objdata
destination = ls_destination
IMPORTING
rc = l_rc.
"当l_rc不为0的时候表示对象不存在
IF l_rc NE 0.
MESSAGE '模板下载失败!' TYPE 'S' DISPLAY LIKE 'E'.
STOP.
* EXIT.
ENDIF.
CLEAR: i_objid, "模板对象名
i_filename, "模板的EXCEL文件名
is_back_run, "是否显示路径选择
c_destination. " 路径
ENDFORM. "FRM_get_excel_templet
*&---------------------------------------------------------------------*
*& Form FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_G_FILE text
*----------------------------------------------------------------------*
FORM frm_get_filepath USING p_l_str CHANGING p_file.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_filename = p_l_str "
def_path = p_file
mask = ',*.xls,*.xls,*.xlsx,*.xlsx.'
mode = 'S'
title = '保存路径'
IMPORTING
filename = p_file
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
ENDFORM. "FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD_EXCEL_FROMSERVER
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_L_TEMP text
* -->P_P_FILE text
* <--P_L_ERROR text
*----------------------------------------------------------------------*
FORM frm_download_excel_fromserver USING p_file LIKE sapb-sappfad
p_temp.
DATA:l_temp TYPE wwwdatatab-objid,
lo_objdata LIKE wwwdatatab,
lo_mime LIKE w3mime,
ls_destination LIKE rlgrap-filename,
ls_objnam TYPE string,
li_rc LIKE sy-subrc,
ls_errtxt TYPE string,
l_error TYPE char255.
CLEAR:l_temp,l_error,lo_objdata,ls_objnam.
IF p_temp = '1'.
l_temp = 'ZFIR003'.
ENDIF.
CONCATENATE l_temp '.XLS' INTO ls_objnam.
SELECT SINGLE relid objid FROM wwwdata
INTO CORRESPONDING FIELDS OF lo_objdata
WHERE srtf2 = 0
AND relid = 'MI'
AND objid = l_temp.
IF sy-subrc NE 0 OR lo_objdata-objid EQ space.
CONCATENATE '模板文件:' ls_objnam '不存在,请用TCODE:SMW0进行加载' INTO l_error.
MESSAGE e000(zre) WITH l_error.
ENDIF.
ls_destination = p_file.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = lo_objdata
destination = ls_destination
IMPORTING
rc = li_rc.
IF li_rc NE 0.
CONCATENATE '模板文件:' ls_objnam '下载失败' INTO l_error.
MESSAGE e000(zre) WITH l_error.
ENDIF.
ENDFORM. " FRM_DOWNLOAD_EXCEL_FROMSERVER
*&---------------------------------------------------------------------*
*& Form frm_download_mod
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_download_mod .
DATA:l_temp TYPE c,
l_str TYPE string.
CASE sscrfields-ucomm. " sy-dynnr sscrfields-ucomm
WHEN 'FC01'. " 该user command为系统规定
CLEAR:g_file,l_temp,l_str.
"获取文件路径
CONCATENATE '固定资产导入模板-' sy-datum INTO l_str.
l_temp = '1'.
PERFORM frm_get_filepath USING l_str CHANGING g_file.
"下载模板
CHECK sy-subrc = 0 AND NOT g_file IS INITIAL.
PERFORM frm_download_excel_fromserver USING g_file l_temp.
ENDCASE.
ENDFORM.
作者:小飞猪猪猪猪猪猪猪–CSDN