【转自http://www.cnblogs.com/VerySky/articles/2170014.html】
*----------------------------------------------------------------------*
* Program Name <程序名称> : Z_FI_DCV_0001 *
* Purpose <程序用途> : 资产负债表导出为EXCEL *
* Project Name <项目名称> : ITG-TRAINING *
* Created by <代码编写人> : YANGMIN *
* Create on <创建日期> : 20071207 *
* Functional Consultant <顾问> : *
* Description <功能描述> : 从服务器下载EXCEL到用户本地, *
* 打开本地EXCEL,将CELL内公式转换为参数 *
* 根据参数调用函数,返回结果回写入CELL *
*----------------------------------------------------------------------*
* Modification Log <程序修改日志> *
*Date Programmer Corr. # Description *
*2007.12.07 YANGMIN Initial pgm dev *
*----------------------------------------------------------------------*
REPORT z_fi_dcv_0001.
*&---------------------------------------------------------------------*
*& 变量声明 *---服务器上文件路径,客户端临时文件夹声明
*&---------------------------------------------------------------------*
DATA: g_user_file_path(60) TYPE c."存放登陆用户的系统临时文件夹路径
INCLUDE ole2incl.
DATA: excel TYPE ole2_object,
workbook TYPE ole2_object, " Workbook object
book TYPE ole2_object,
sheet TYPE ole2_object, " Worksheet object
range TYPE ole2_object, " Range object
cell TYPE ole2_object. " Cell object
DATA: sheet_name(20) VALUE 'SHEET1'. "Excel文件里的sheet name
DATA local_file LIKE rcgfiletr-ftfront."下载文件类型
DATA: gt_data TYPE alsmex_tabline OCCURS 0 WITH HEADER LINE. "便历EXCEL 所有数据
DATA: gchange_data TYPE alsmex_tabline OCCURS 0 WITH HEADER LINE."存放需要修改的单元格数据
DATA: g_sql TYPE string."测试拼接SQL语句
DATA: str_name TYPE string."存放本地的EXCEL名字
*****传递给函数的内表 结构 参考zsfagl****************************************
data: new_table_to_function LIKE zsfagl OCCURS 0 WITH HEADER LINE.
DATA: xin_table_to_function LIKE new_table_to_function OCCURS 0 WITH HEADER LINE."传递给函数的内表参数
DATA: t1 LIKE SY-UZEIT,"运行开始时间
t2 LIKE SY-UZEIT,"运行结束
t3 LIKE SY-UZEIT."运行时间差
t1 = SY-UZEIT.
***********定义屏幕元素********************************************************************************************************
*&---------------------------------------------------------------------*
*& Select-OPTIONS
*&---------------------------------------------------------------------*
SelectION-SCREEN BEGIN OF BLOCK zblock003 WITH FRAME TITLE text-001.
PARAMETERS:p_bukrs LIKE ekko-bukrs,"公司代码
p_gjahr LIKE bkpf-gjahr,"会计凭证年度
p_rpmax LIKE faglflext-rpmax,"起始 月份
p_xend LIKE faglflext-rpmax."终止 月份
*****选择报表类型 下拉GRID 方式1*****************************
TABLES: ZTFIBBK.
DATA:
filepath_real(60) TYPE c,"通过SQL语句再获得 真实路径
lc_filepath LIKE zfitable00-filepath,
ltab_fields LIKE help_value OCCURS 0 WITH HEADER LINE,
BEGIN OF ltab_values OCCURS 0,
feld(40) TYPE c,
END OF ltab_values.
PARAMETERS: filepath(60) TYPE c OBLIGATORY."获得文件名
SelectION-SCREEN END OF BLOCK zblock003.
AT SelectION-SCREEN ON VALUE-REQUEST FOR filepath.
*获得文件名
PERFORM get_path_value USING filepath text-006.
*------调用事件----------------------------------------
*INITIALIZATION."初始化事件
INITIALIZATION .
ltab_fields-tabname = 'ZTFIBBK '."'ZFITABLE00'.
ltab_fields-fieldname = 'BBMC'."'TABLENAME'.
ltab_fields-selectflag = 'X'.
APPEND ltab_fields.
ltab_fields-tabname = 'ZTFIBBK'.
ltab_fields-fieldname = 'FILEPATH'.
ltab_fields-selectflag = space.
APPEND ltab_fields.
Select * FROM ZTFIBBK.
ltab_values-feld = ZTFIBBK-BBMC.
APPEND ltab_values.
ltab_values-feld = ZTFIBBK-filepath.
APPEND ltab_values.
ENDSelect.
*----------------------------------------------------------------------*
*START-OF-SelectION
*----------------------------------------------------------------------*
START-OF-SelectION.
*获得用户TEMP路径
PERFORM get_user_info USING g_user_file_path.
*根据选择的 会计表名,查找出表名真实路径
Select SINGLE filepath FROM ZTFIBBK
INTO filepath_real
Where bbmc = filepath.
*读本地服务器文件到本地
PERFORM read_serverfile_to_local USING filepath_real str_name.
*把STRING类型 赋值给 rcgfiletr-ftfront 类型,否则函数参数类型不同
local_file = str_name.
PERFORM get_data_to_innertable TABLES gt_data USING local_file." 从本地文件local_file 读 EXCEL模版单元格到内表gt_data
*PERFORM display TABLES gt_data."测试显示结果 取的表名,字段名
* gt_data放初始EXCEL内表, table_to_function 为处理后内表
* PERFORM processdata_to_function TABLES gt_data table_to_function.
PERFORM processdata_to_function TABLES gt_data xin_table_to_function.
* 调用庄闽程序 暂时缺少输出结果表参数
PERFORM call_new TABLES xin_table_to_function USING p_bukrs p_gjahr p_rpmax p_xend.
* PERFORM CALL_SECOND TABLES
* 将函数结果表传递给要更新的内表
* PERFORM processdata_from_function TABLES table_from_function gchange_data .
* 根据内表数据 替换本地EXCEL数据
PERFORM write_to_local_excel TABLES gchange_data USING local_file ."将需要替换的数据 写回EXCEL相应单元格
*更新启群的 模版路径表
PERFORM process_data_test.
*将屏幕元素写入EXCEL
*PERFORM fill_fix_value_to_innertable.
t2 = SY-UZEIT.
t3 = t2 - t1.
WRITE:'程序运行时间为:',t3.
*&---------------------------------------------------------------------*
*& Form read_serverfile_to_local
*&---------------------------------------------------------------------*
* text 读读服务器上文件并下载到本地
*----------------------------------------------------------------------*
* -->F_NAME text 服务器路径+文件名
* -->F_DIR text 本地文件名 利用CHARGING 回写 f_local_file
*----------------------------------------------------------------------*
FORM read_serverfile_to_local USING f_server_file TYPE c CHANGING f_local_file TYPE string .
DATA: f_itab TYPE TABLE OF x WITH HEADER LINE.
DATA: f_length TYPE i.
DATA:f_name LIKE epsf-epsfilnam, "类型参考 函数 EPS_GET_FILE_ATTRIBUTES
f_dir LIKE epsf-epsdirnam.
DATA:o_dir TYPE string,
o_name TYPE string.
DATA: f_11 TYPE sy-fdpos.
DATA: f_itabnew TYPE TABLE OF string.
SPLIT f_server_file AT '\' INTO TABLE f_itabnew."分离字符串到内表
LOOP AT f_itabnew INTO o_name."读到最后一个 \
* WRITE / o_name.
ENDLOOP.
SEARCH f_server_file FOR o_name.
IF sy-subrc = 0.
f_11 = sy-fdpos.
o_dir = f_server_file+0(f_11).
ENDIF.
f_dir = o_dir."请注意本步转换
f_name = o_name.
****读服务器上文件到内表********************
OPEN DATASET f_server_file FOR INPUT IN BINARY MODE.
DO.
READ DATASET f_server_file INTO f_itab.
IF sy-subrc <> 0 .
EXIT.
ENDIF.
APPEND f_itab.
ENDDO.
****计算下载文件大小********************
CALL FUNCTION 'EPS_GET_FILE_ATTRIBUTES'
EXPORTING
file_name = f_name "str_name_new " '出口统计.xls' "类型:epsf-epsfilnam
dir_name = f_dir "str_dir_new " "'\usr\sap\ids\dvebmgs00\log\' "类型:epsf-epsdirnam
IMPORTING
file_size = f_length
* FILE_OWNER =
* FILE_MODE =
* FILE_TYPE =
* FILE_MTIME =
* FILE_SIZE_LONG =
EXCEPTIONS
read_directory_failed = 1
read_attributes_failed = 2
OTHERS = 3
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
*连接字符串组成文件路径 用户本地TEMP路径+文件名
CONCATENATE g_user_file_path '\' o_name INTO o_name.
f_local_file = o_name.
g_sql = f_local_file.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
bin_filesize = f_length "56018
filename = f_local_file "'d:\9_new.xls' "str_name "
filetype = 'BIN'
TABLES
data_tab = f_itab
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.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
***************************************************************************
ENDFORM. "read_serverfile_to_local
*& Form write_to_local_excel
*&---------------------------------------------------------------------*
* text 打开EXCEL,并将将数据写回EXCEL
* f_table 存放需要替换的内容组成的内表 f_local_file 本地指定的EXCEL文件
*----------------------------------------------------------------------*
FORM write_to_local_excel TABLES f_table STRUCTURE alsmex_tabline USING f_local_file LIKE rcgfiletr-ftfront.
Create OBJECT excel 'EXCEL.APPLICATION'. "Create EXCEL OBJECT
IF sy-subrc NE 0.
EXIT.
ENDIF.
SET PROPERTY OF excel 'Visible' = 1. "是否显示EXCEL
CALL METHOD OF excel 'Workbooks' = workbook.
DATA h TYPE i.
CALL METHOD OF workbook 'Open'
EXPORTING
#1 = f_local_file.
CALL METHOD OF excel 'Sheets' = sheet
EXPORTING
#1 = 1.
CALL METHOD OF sheet 'Select' .
WRITE:/,'写入固定数据'.
*填充
* PERFORM fill_range USING 8 1 g_sql ."'100'. "第8行第1列
* PERFORM fill_range USING 2 1 '20' ."'100'. "第2行第1列
* PERFORM fill_range USING 2 2 '-4' ."'100'. "第2行第2列
LOOP AT f_table.
PERFORM fill_range USING f_table-row f_table-col f_table-value ." "在这框的下面10列显示结果
WRITE:/,'写入数据:','行:',f_table-row ,'列:',f_table-col,'值',f_table-value.
ENDLOOP.
GET PROPERTY OF excel 'ActiveWorkbook' = sheet.
* CALL METHOD OF sheet 'printout'. "打印
* CALL METHOD OF excel 'SAVE'. "保存
* CALL METHOD OF excel 'Quit'. "退出
FREE OBJECT cell.
FREE OBJECT range.
FREE OBJECT sheet.
FREE OBJECT workbook.
FREE OBJECT excel.
ENDFORM. "write_to_local_excel
*&---------------------------------------------------------------------*
*& Form fill_range
*&---------------------------------------------------------------------*
* text 填充EXCEL 单元格
*----------------------------------------------------------------------*
* -->VALUE(F_ROW) text
* -->VALUE(F_COL) text
* -->VALUE(F_VALUE) text
*----------------------------------------------------------------------*
FORM fill_range USING value(f_row)
value(f_col)
value(f_value).
DATA:
row TYPE i,
col TYPE i.
row = f_row.
col = f_col.
CALL METHOD OF excel 'CELLS' = cell
EXPORTING
#1 = row
#2 = col.
SET PROPERTY OF cell 'VALUE' = f_value.
ENDFORM. "fill_range
*&---------------------------------------------------------------------*
*& Form Get_data_to_innerTable
*&---------------------------------------------------------------------*
* text 打开本地EXCEL文件 并传递到内表
*----------------------------------------------------------------------*
FORM get_data_to_innertable TABLES f_tab STRUCTURE alsmex_tabline USING f_file_name LIKE rlgrap-filename.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = f_file_name
i_begin_col = 1
i_begin_row = 1
i_end_col = 100
i_end_row = 30000
TABLES
intern = f_tab"gt_data
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
SORT gt_data BY row col value."按行,列排序
ENDFORM. "Get_data_to_innerTable
*&---------------------------------------------------------------------*
*& Form ProcessData_to_function
*&---------------------------------------------------------------------*
* text 将EXCEL读出的内表 筛选为传递参数内表
*----------------------------------------------------------------------*
*FORM processdata_to_function TABLES f_table STRUCTURE alsmex_tabline f_table_to_function STRUCTURE type_table_to_function.
FORM processdata_to_function TABLES f_table STRUCTURE alsmex_tabline f_table_to_function STRUCTURE new_table_to_function.
DATA: f_1 TYPE sy-fdpos,
f_2 TYPE sy-fdpos,
f_3 TYPE sy-fdpos.",
* f_4 TYPE sy-fdpos,
* f_5 TYPE sy-fdpos,
* f_6 TYPE sy-fdpos.
DATA: s_1 TYPE char10,
s_1_new LIKE s_1,
s_2 TYPE char20,
s_2_new LIKE s_2,
s_3 TYPE char10,
s_3_new LIKE s_2.",
* s_4 TYPE char10,
* s_4_new LIKE s_2,
* s_5 TYPE char10,
* s_5_new LIKE s_2,
* s_6 TYPE char10,
* s_6_new TYPE char10.
DATA: ln_spaces TYPE i.
ln_spaces = 1.
DATA: new_col LIKE alsmex_tabline-col.
DATA: new_row LIKE alsmex_tabline-row.
DATA: s_sql TYPE string.
DATA: s_value TYPE f.
DATA: flag TYPE i."单元格正负
ULINE.
WRITE:/,'EXCEL 中初始数据为:'.
LOOP AT f_table.
*去除前面的0,否则显示 1,4单元格 为0001,0004
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = f_table-row
IMPORTING
output = f_table-row.
*去除前面的0
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = f_table-col
IMPORTING
output = f_table-col.
WRITE:/,'行:',f_table-row,'列:',f_table-col,'值:',f_table-value.
*~defined(zcfz营业收入)_bq
**************
SEARCH f_table-value FOR '('. "先搜索 ( 号
IF sy-subrc = 0. "如果找到
f_1 = sy-fdpos.
SEARCH f_table-value FOR '_'.
IF sy-subrc = 0 .
f_2 = sy-fdpos.
* 找到 营业收入
f_3 = f_1 + 5."
s_3 = f_table-value+f_3(4).
WRITE:'取得的项目名为:',s_3."营业收入
f_1 = f_1 + 1."现在假设表名字为ZCFZ + 5. "(ZCFZ 共5个字符
s_1 = f_table-value+f_1(4). "zcfz 4个字符 "营业收入 4个字符
WRITE:'取得的表名为:',s_1."zcfz
f_2 = f_2 + 1.
s_2 = f_table-value+f_2(2).
WRITE:'取得的期间为:',s_2. "bq 2个字符
s_2_new+ln_spaces = s_2 ."补充前面空格
s_1_new+ln_spaces = s_1.
**********形成传递给庄闽的结果内表********************************************************
* f_table_to_function-row = f_table-row.
* f_table_to_function-col = f_table-col.
* f_table_to_function-old_value = f_table-value.
* f_table_to_function-xiangmu = s_3.
* f_table_to_function-qijian = s_2.
* APPEND f_table_to_function.
* item type char50,
* qsff TYPE char10,
* wrbtr LIKE bseg-wrbtr ,
* rtcur LIKE bseg-PSWSL,
** exrow TYPE numeric,
** excol TYPE numeric,
* exrow TYPE i,
* excol TYPE i,
* olval TYPE char50,
f_table_to_function-exrow = f_table-row.
f_table_to_function-excol = f_table-col.
f_table_to_function-olval = f_table-value.
f_table_to_function-item = s_3.
f_table_to_function-qsff = s_2.
APPEND f_table_to_function.
ENDIF.
ENDIF.
ENDLOOP.
*PERFORM fill_fix_value_to_innertable.
*****测试传给庄闽的内表数据**********************************
WRITE:/,'******这是向庄闽传递的内表参数*******'.
LOOP AT f_table_to_function.
* WRITE:/,'row',f_table_to_function-row,'col',f_table_to_function-col,'old_value',f_table_to_function-old_value,'xiangmu',f_table_to_function-xiangmu,'qijian',f_table_to_function-qijian.
WRITE:/,'row',f_table_to_function-exrow,'col',f_table_to_function-excol,'old_value',f_table_to_function-olval,'xiangmu',f_table_to_function-item,'qijian',f_table_to_function-qsff.
ENDLOOP.
ENDFORM. "ProcessData_to_function
*&---------------------------------------------------------------------*
*& Form ProcessData_from_function
*&---------------------------------------------------------------------*
* text 从函数取回结果内表附加到需要修改的内表
*----------------------------------------------------------------------*
FORM processdata_from_function TABLES f_table STRUCTURE new_table_to_function f_outab STRUCTURE alsmex_tabline.
DATA: flag TYPE i.
ULINE.
WRITE:/,'函数传递来的结果内表为:'.
LOOP AT f_table.
flag = 1."默认CELL单元格数值为正
SEARCH f_table-olval FOR ':=-1*'.
IF sy-subrc = 0.
flag = 0. "进行标记 CELL 中添充结果为负
ENDIF.
IF flag = 0.
f_outab-value = f_table-wrbtr.
ELSEIF flag = 1.
* f_table-b = f_table-b * -1. "这样写 传递到EXCEL竟然成了 12-
f_outab-value = f_table-wrbtr.
CONCATENATE '-' f_outab-value INTO f_outab-value.
ENDIF.
f_outab-row = f_table-exrow.
f_outab-col = f_table-excol.
APPEND f_outab.
ENDLOOP.
* MODIFY f_outab."因为是引用传递,所以这里不能用MODIFY 否则有BUG
*****测试庄闽函数传出的内表数据**********************************
WRITE:/,'******这是要替换EXCEL内容的内表*******'.
LOOP AT f_outab.
WRITE:/,'row:',f_outab-row,'col:',f_outab-col,'NEW_value:',f_outab-value.
ENDLOOP.
PERFORM fill_fix_value_to_innertable.
ENDFORM. "ProcessData_from_function
*&---------------------------------------------------------------------*
*& Form fill_fix_value_to_innertable
*&---------------------------------------------------------------------*
* text 写入固定数据到内表
*----------------------------------------------------------------------*
FORM fill_fix_value_to_innertable.
*****屏幕元素写入指定单元格*************************************
gchange_data-row = 12 .
gchange_data-col = 1 .
gchange_data-value = '系统当前日期为:' .
WRITE:/,'追加数据:','行:',gchange_data-row ,'列:',gchange_data-col,'值',gchange_data-value.
APPEND gchange_data.
gchange_data-row = 12 .
gchange_data-col = 2 .
gchange_data-value = sy-datum .
WRITE:/,'追加数据:','行:',gchange_data-row ,'列:',gchange_data-col,'值',gchange_data-value.
APPEND gchange_data.
gchange_data-row = 12 .
gchange_data-col = 3 .
gchange_data-value = '当前时间:'.
APPEND gchange_data.
gchange_data-row = 12 .
gchange_data-col = 4 .
gchange_data-value = sy-uzeit .
APPEND gchange_data.
gchange_data-row = 12 .
gchange_data-col = 5 .
gchange_data-value = '当前用户为:'.
APPEND gchange_data.
gchange_data-row = 12 .
gchange_data-col = 6 .
gchange_data-value = sy-uname .
APPEND gchange_data.
gchange_data-row = 13 .
gchange_data-col = 1 .
gchange_data-value = '屏幕选择的公司代码为'.
APPEND gchange_data.
gchange_data-row = 13 .
gchange_data-col = 2 .
gchange_data-value = p_bukrs .
APPEND gchange_data.
* s_GJAHR LIKE BKPF-GJAHR,"会计凭证年度
* S_RPMAX LIKE FAGLFLEXT-RPMAX."期间 月份
gchange_data-row = 14 .
gchange_data-col = 1 .
gchange_data-value = '屏幕选择的会计年度为'.
APPEND gchange_data.
gchange_data-row = 14 .
gchange_data-col = 2 .
gchange_data-value = p_gjahr .
APPEND gchange_data.
gchange_data-row = 15 .
gchange_data-col = 1 .
gchange_data-value = '屏幕选择的期间为'.
APPEND gchange_data.
gchange_data-row = 15 .
gchange_data-col = 2 .
gchange_data-value = p_rpmax .
APPEND gchange_data.
gchange_data-row = 16 .
gchange_data-col = 1 .
gchange_data-value = '1 选择的文件为:'.
APPEND gchange_data.
gchange_data-row = 16 .
gchange_data-col = 2 .
gchange_data-value = filepath .
APPEND gchange_data.
* gchange_data-row = 17 .
* gchange_data-col = 1 .
* gchange_data-value = '2 选择的文件路径为:'.
* APPEND gchange_data.
* gchange_data-row = 17 .
* gchange_data-col = 2 .
* gchange_data-value = ps_parm .
* APPEND gchange_data.
ENDFORM. "fill_fix_value_to_innertable
*&---------------------------------------------------------------------*
*& Form get_path_value
*&---------------------------------------------------------------------*
* text 文件路径 自定义搜索帮助
*----------------------------------------------------------------------*
* -->P_WERKS text
* -->P_TEXT_006 text
*----------------------------------------------------------------------*
FORM get_path_value USING p_table
p_text_006.
CALL FUNCTION 'HELP_VALUES_GET_WITH_TABLE'
EXPORTING
fieldname = 'BBMC' "'TABLENAME' "'WERKS'
tabname = 'ZTFIBBK'" 'ZFITABLE00' "'T001W'
title_in_values_list = 'Select a value'
IMPORTING
select_value = p_table "p_werks
TABLES
fields = ltab_fields
valuetab = ltab_values
EXCEPTIONS
field_not_in_ddic = 01
more_then_one_selectfield = 02
no_selectfield = 03.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form get_user_info
*&---------------------------------------------------------------------*
* text 得到用户 TEMP路径
*----------------------------------------------------------------------*
* -->F_TEMP_INFO text
*----------------------------------------------------------------------*
FORM get_user_info CHANGING f_temp_info TYPE c.
* DATA: f_temp_info(255) TYPE C.
CALL FUNCTION 'GUI_GET_DESKTOP_INFO'
EXPORTING
type = '4'
CHANGING
return = f_temp_info.
ENDFORM. "get_user_info
*&---------------------------------------------------------------------*
*& Form DISPLAY
*&---------------------------------------------------------------------*
* text 纯粹测试无其他意义
*----------------------------------------------------------------------*
FORM display TABLES f_table STRUCTURE alsmex_tabline .
DATA: f_1 TYPE sy-fdpos,
f_2 TYPE sy-fdpos,
f_3 TYPE sy-fdpos,
f_4 TYPE sy-fdpos,
f_5 TYPE sy-fdpos,
f_6 TYPE sy-fdpos.
DATA: s_1 TYPE char10,
s_1_new LIKE s_1,
s_2 TYPE char20,
s_2_new LIKE s_2,
s_3 TYPE char10,
s_3_new LIKE s_2,
s_4 TYPE char10,
s_4_new LIKE s_2,
s_5 TYPE char10,
s_5_new LIKE s_2,
s_6 TYPE char10,
s_6_new TYPE char10.
DATA: ln_spaces TYPE i.
ln_spaces = 1.
DATA: new_col LIKE alsmex_tabline-col.
DATA: new_row LIKE alsmex_tabline-row.
DATA: s_sql TYPE string.
DATA: s_value TYPE f.
DATA: flag TYPE i."单元格正负
*****测试写参数表 并传递给庄闽的函数*************************************************
DATA: BEGIN OF f_table_to_function OCCURS 0,
row TYPE i,
col TYPE i,
old_value TYPE char50,
xiangmu TYPE char20,
qijian TYPE char20,
* new_value TYPE char50, "新的结果值要等 庄闽返回结果内表 去找
END OF f_table_to_function.
f_table_to_function-row = 20.
f_table_to_function-col = 1.
f_table_to_function-old_value = '~defined(zcfz营业收入)_bq'.
f_table_to_function-xiangmu = '营业收入'.
f_table_to_function-qijian = 'bq'.
* f_table_to_Function-new_value = '209556.89'.
APPEND f_table_to_function.
****************************************************************
*****测试读 庄闽返回的结果内表*************************************************
DATA: BEGIN OF zcfz OCCURS 0,
a TYPE string,
b TYPE i,
c TYPE string,
END OF zcfz.
zcfz-a = 'test1'.
zcfz-b = 12.
zcfz-c = '~defined(zcfz营业收入)_bq'.
APPEND zcfz.
zcfz-a = '内表中存的数据:test2'.
zcfz-b = 24.
zcfz-c = '~defined(ZCFZ销售费用_bq)~'.
APPEND zcfz.
****************************************************************
ULINE.
WRITE:/,'EXCEL 中初始数据为:'.
LOOP AT f_table.
flag = 1."默认CELL单元格数值为正
SEARCH f_table-value FOR ':=-1*'.
IF sy-subrc = 0.
flag = 0. "进行标记 CELL 中添充结果为负
ENDIF.
*去除前面的0,否则显示 1,4单元格 为0001,0004
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = f_table-row
IMPORTING
output = f_table-row.
*去除前面的0
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = f_table-col
IMPORTING
output = f_table-col.
WRITE:/,'行:',f_table-row,'列:',f_table-col,'值:',f_table-value.
*~defined(zcfz营业收入)_bq
**************
SEARCH f_table-value FOR '('. "先搜索 ( 号
IF sy-subrc = 0. "如果找到
f_1 = sy-fdpos.
SEARCH f_table-value FOR '_'.
IF sy-subrc = 0 .
f_2 = sy-fdpos.
* 找到 营业收入
f_3 = f_1 + 5."
s_3 = f_table-value+f_3(4).
WRITE:'取得的项目名为:',s_3."营业收入
f_1 = f_1 + 1."现在假设表名字为ZCFZ + 5. "(ZCFZ 共5个字符
s_1 = f_table-value+f_1(4). "zcfz 4个字符 "营业收入 4个字符
WRITE:'取得的表名为:',s_1."zcfz
f_2 = f_2 + 1.
s_2 = f_table-value+f_2(2).
WRITE:'取得的期间为:',s_2. "bq 2个字符
s_2_new+ln_spaces = s_2 ."补充前面空格
s_1_new+ln_spaces = s_1.
**********形成传递给庄闽的结果内表********************************************************
f_table_to_function-row = f_table-row.
f_table_to_function-col = f_table-col.
f_table_to_function-old_value = f_table-value.
f_table_to_function-xiangmu = s_3.
f_table_to_function-qijian = s_2.
APPEND f_table_to_function.
****************************************************************
** CONCATENATE ' select' s_2_new ' from' s_1_new INTO g_sql. "且该命令忽略被连接的各个字符串的尾部空格 注意是尾部!!
* CONCATENATE 'READ TABLE ' s_1_new ' INDEX 1' INTO g_sql. "且该命令忽略被连接的各个字符串的尾部空格 注意是尾部!!
* WRITE:/,'SQL IS:',g_sql.
* 测试从庄闽传递来的记过内表中 检索出数据 插入要替换的内表 ************************************
CLEAR g_sql.
READ TABLE zcfz WITH KEY a = 'test1' . "b = 100.
IF sy-subrc = 0.
g_sql = zcfz-b.
* CONCATENATE '=-1*' g_sql INTO g_sql.
IF flag = 0. "FALG 为 0 时表负数
CONCATENATE '=-1*' g_sql INTO g_sql.
ENDIF.
ENDIF.
WRITE:/,'现在从内表中读到的数据为:',g_sql.
* 将需要转换的单元格汇总,加载到 需要修改数据的内表
CLEAR s_sql.
s_sql = g_sql.
new_col = f_table-col. " + 4.
* new_row = f_table-row + 10.
new_row = f_table-row + 1.
gchange_data-row = new_row .
gchange_data-col = new_col .
gchange_data-value = s_sql .
WRITE:/,'追加数据:','行:',gchange_data-row ,'列:',gchange_data-col,'值',gchange_data-value.
APPEND gchange_data.
ENDIF.
ENDIF.
ENDLOOP.
*****屏幕元素写入指定单元格*************************************
gchange_data-row = 12 .
gchange_data-col = 1 .
gchange_data-value = '系统当前日期为:' .
WRITE:/,'追加数据:','行:',gchange_data-row ,'列:',gchange_data-col,'值',gchange_data-value.
APPEND gchange_data.
gchange_data-row = 12 .
gchange_data-col = 2 .
gchange_data-value = sy-datum .
WRITE:/,'追加数据:','行:',gchange_data-row ,'列:',gchange_data-col,'值',gchange_data-value.
APPEND gchange_data.
gchange_data-row = 12 .
gchange_data-col = 3 .
gchange_data-value = '当前时间:'.
APPEND gchange_data.
gchange_data-row = 12 .
gchange_data-col = 4 .
gchange_data-value = sy-uzeit .
APPEND gchange_data.
gchange_data-row = 12 .
gchange_data-col = 5 .
gchange_data-value = '当前用户为:'.
APPEND gchange_data.
gchange_data-row = 12 .
gchange_data-col = 6 .
gchange_data-value = sy-uname .
APPEND gchange_data.
gchange_data-row = 13 .
gchange_data-col = 1 .
gchange_data-value = '屏幕选择的公司代码为'.
APPEND gchange_data.
gchange_data-row = 13 .
gchange_data-col = 2 .
gchange_data-value = p_bukrs .
APPEND gchange_data.
* s_GJAHR LIKE BKPF-GJAHR,"会计凭证年度
* S_RPMAX LIKE FAGLFLEXT-RPMAX."期间 月份
gchange_data-row = 14 .
gchange_data-col = 1 .
gchange_data-value = '屏幕选择的会计年度为'.
APPEND gchange_data.
gchange_data-row = 14 .
gchange_data-col = 2 .
gchange_data-value = p_gjahr .
APPEND gchange_data.
gchange_data-row = 15 .
gchange_data-col = 1 .
gchange_data-value = '屏幕选择的期间为'.
APPEND gchange_data.
gchange_data-row = 15 .
gchange_data-col = 2 .
gchange_data-value = p_rpmax .
APPEND gchange_data.
gchange_data-row = 16 .
gchange_data-col = 1 .
gchange_data-value = '1 选择的文件为:'.
APPEND gchange_data.
gchange_data-row = 16 .
gchange_data-col = 2 .
gchange_data-value = filepath .
APPEND gchange_data.
* gchange_data-row = 17 .
* gchange_data-col = 1 .
* gchange_data-value = '2 选择的文件路径为:'.
* APPEND gchange_data.
* gchange_data-row = 17 .
* gchange_data-col = 2 .
* gchange_data-value = ps_parm .
* APPEND gchange_data.
*****测试传给庄闽的内表数据**********************************
WRITE:/,'******这是向庄闽传递的内表参数*******'.
LOOP AT f_table_to_function.
WRITE:/,'row',f_table_to_function-row,'col',f_table_to_function-col,'old_value',f_table_to_function-old_value,'xiangmu',f_table_to_function-xiangmu,'qijian',f_table_to_function-qijian.
ENDLOOP.
ENDFORM. "DISPLAY
*&---------------------------------------------------------------------*
*& Form call_new
*&---------------------------------------------------------------------*
* text 调用庄闽函数 输入,输出同为 f_tab表,不知是否可行
*----------------------------------------------------------------------*
* -->F_TAB text
* -->F_BUKRS text
* -->F_YEAR text
* -->F_FR text
* -->F_TO text
*----------------------------------------------------------------------*
*FORM call_new TABLES f_tab STRUCTURE type_table_to_function USING f_bukrs LIKE ekko-bukrs f_year LIKE bkpf-gjahr f_fr LIKE faglflext-rpmax f_to LIKE faglflext-rpmax .
FORM call_new TABLES f_tab STRUCTURE new_table_to_function USING f_bukrs LIKE ekko-bukrs f_year LIKE bkpf-gjahr f_fr LIKE faglflext-rpmax f_to LIKE faglflext-rpmax .
CALL FUNCTION 'Z_FI_FAGL_RPT_ITEM'
EXPORTING
i_ryear = f_year
i_bukrs = f_bukrs
i_frper = f_fr
i_toper = f_to
TABLES
t_fitab = f_tab.
LOOP AT f_tab.
gchange_data-row = f_tab-exrow.
gchange_data-col = f_tab-excol.
gchange_data-value = f_tab-wrbtr.
APPEND gchange_data.
ENDLOOP.
ENDFORM. "call_new
*&---------------------------------------------------------------------*
*& Form process_data_test
*&---------------------------------------------------------------------*
* text 测试表添加,修改,删除 主要与新同事交流用
*----------------------------------------------------------------------*
FORM process_data_test.
***测试删除数据*****************
Delete FROM zfitable00 Where bukrs <> '1000'.
* Delete sflight FROM TABLE sflight_key_tab. "另外一种删除表的语法
***测试添加数据******
* DATA: z_temp LIKE zfitable00 OCCURS 0 WITH HEADER LINE.
* z_temp-bukrs = '1000'.
* z_temp-tablename = 'A05'.
* z_temp-uname = 'YANGMIN'.
* z_temp-filepath = '\usr\sap\ids\dvebmgs00\log\9.xls'.
** Insert line INTO TABLE itab.
* Insert into zfitable00 values z_temp .
*****测试更新数据*************
Update ztfibbk" zfitable00
SET filepath = '\usr\sap\ids\dvebmgs00\log\资产负债表.xls'
* tablename = '资产负债表'
Where bbdm = 'A01'.
Update ztfibbk" zfitable00
SET filepath = '\usr\sap\ids\dvebmgs00\log\利润表.xls'
* tablename = '资产负债表'
Where bbdm = 'A03'.
* Select *
* FROM zfitable00
* INTO TABLE z_temp.
*
** modify zfitable00 from z_temp .
*
* LOOP AT z_temp.
* WRITE:/,'TABLENAME:',z_temp-tablename,'path:',z_temp-filepath.
* ENDLOOP.
ENDFORM. "process_data_test
来源:https://www.cnblogs.com/rainysblog/p/4139493.html