report zalv_test.
include zmm_olemehode.
include zalv_top.
include zalv_data.
include zalv_alv.
include zalv_excel.
include zalv_print.
initialization.
s_cityto-low = 'ACAPULCO'.
s_cityto-high = 'ZURICH'.
append s_cityto.
at selection-screen on value-request for p_cityfr.
perform sub_pop_f4value.
at selection-screen.
start-of-selection.
perform get_data.
perform process_data.
perform display_data.
end-of-selection.
zalv_top:
tables: spfli, sflight, scarr, usr21, adrp.
type-pools slis.
types: begin of ty_data,
sel type c,
carrid like scarr-carrid, "航班编号
carrname like scarr-carrname, "航班名称
cityfrom like spfli-cityfrom, "起飞城市
airpfrom like spfli-airpfrom, "起飞机场
cityto like spfli-cityto, "目标城市
airpto like spfli-airpto, "目的机场
deptime like spfli-deptime, "启程时间
arrtime like spfli-arrtime, "到达时间
paymentsum like sflight-paymentsum, " 当前预定总数
fldate like sflight-fldate, " 当前预定总数
end of ty_data.
types: begin of ty_scarr, "航班表
carrid like scarr-carrid,
carrname like scarr-carrname,
end of ty_scarr.
data: user_name(20) type c.
data: t_data type table of ty_data,
t_scarr type table of ty_scarr,
wa_data type ty_data,
wa_alvdata type ty_data,
wa_tempdata type ty_data,
wa_scarr type ty_scarr.
data: itab_data like t_data,
temp_data like t_data,
t_alvdata like t_data.
data: t_printdata type table of ty_data,
wa_printdata type ty_data.
data: i_events type slis_t_event,
w_events like line of i_events,
i_list_comments type slis_t_listheader,
w_list_comments like line of i_list_comments.
data fm_name type rs38l_fnam.
define mc_add_fieldcat.
clear l_wa_fieldcat.
l_wa_fieldcat-fieldname = &1.
l_wa_fieldcat-seltext_l = &2.
l_wa_fieldcat-key = &3.
l_wa_fieldcat-outputlen = &4.
l_wa_fieldcat-no_zero = &5.
l_wa_fieldcat-just = &6.
append l_wa_fieldcat to lt_fieldcat.
end-of-definition.
*EXCEL输出表
types: begin of typ_exceldata,
row(4) type c,
column(4) type c,
value(60) type c,
end of typ_exceldata.
data: t_exceldata type table of typ_exceldata with header line.
*定义EXCEL输出模版
constants: c_exceltemplate like wwwdatatab value 'MIZLZX002'.
data: g_starrow type i value 4.
parameters p_cityfr like spfli-cityfrom.
select-options: s_cityto for spfli-cityto.
parameters p_date like sflight-fldate.
zalv_data.
form sub_get_data .
" 取主体数据
if p_cityfr is not initial and p_date is initial and s_cityto is initial.
select spfli~carrid spfli~cityfrom spfli~airpfrom spfli~cityto spfli~airpto spfli~deptime spfli~arrtime sflight~paymentsum sflight~fldate
into corresponding fields of table t_data
from spfli inner join sflight on spfli~carrid = sflight~carrid and spfli~connid = sflight~connid
where spfli~cityfrom = p_cityfr.
endif.
if p_cityfr is initial and p_date is not initial and s_cityto is initial.
select spfli~carrid spfli~cityfrom spfli~airpfrom spfli~cityto spfli~airpto spfli~deptime spfli~arrtime sflight~paymentsum sflight~fldate
into corresponding fields of table t_data
from spfli inner join sflight on spfli~carrid = sflight~carrid and spfli~connid = sflight~connid
where sflight~fldate = p_date.
endif.
if p_cityfr is initial and p_date is initial and s_cityto is not initial.
select spfli~carrid spfli~cityfrom spfli~airpfrom spfli~cityto spfli~airpto spfli~deptime spfli~arrtime sflight~paymentsum sflight~fldate
into corresponding fields of table t_data
from spfli inner join sflight on spfli~carrid = sflight~carrid and spfli~connid = sflight~connid
where spfli~cityto in s_cityto.
endif.
if p_cityfr is not initial and p_date is not initial and s_cityto is initial.
select spfli~carrid spfli~cityfrom spfli~airpfrom spfli~cityto spfli~airpto spfli~deptime spfli~arrtime sflight~paymentsum sflight~fldate
into corresponding fields of table t_data
from spfli inner join sflight on spfli~carrid = sflight~carrid and spfli~connid = sflight~connid
where spfli~cityfrom = p_cityfr and sflight~fldate = p_date.
endif.
if p_cityfr is not initial and p_date is initial and s_cityto is not initial.
select spfli~carrid spfli~cityfrom spfli~airpfrom spfli~cityto spfli~airpto spfli~deptime spfli~arrtime sflight~paymentsum sflight~fldate
into corresponding fields of table t_data
from spfli inner join sflight on spfli~carrid = sflight~carrid and spfli~connid = sflight~connid
where spfli~cityfrom = p_cityfr and spfli~cityto in s_cityto .
endif.
if p_cityfr is initial and p_date is not initial and s_cityto is not initial.
select spfli~carrid spfli~cityfrom spfli~airpfrom spfli~cityto spfli~airpto spfli~deptime spfli~arrtime sflight~paymentsum sflight~fldate
into corresponding fields of table t_data
from spfli inner join sflight on spfli~carrid = sflight~carrid and spfli~connid = sflight~connid
where spfli~cityto in s_cityto and sflight~fldate = p_date.
endif.
if p_cityfr is initial and p_date is initial and s_cityto is initial.
select spfli~carrid spfli~cityfrom spfli~airpfrom spfli~cityto spfli~airpto spfli~deptime spfli~arrtime sflight~paymentsum sflight~fldate
into corresponding fields of table t_data
from spfli inner join sflight on spfli~carrid = sflight~carrid and spfli~connid = sflight~connid.
endif.
if p_cityfr is not initial and p_date is not initial and s_cityto is not initial.
select spfli~carrid spfli~cityfrom spfli~airpfrom spfli~cityto spfli~airpto spfli~deptime spfli~arrtime sflight~paymentsum sflight~fldate
into corresponding fields of table t_data
from spfli inner join sflight on spfli~carrid = sflight~carrid and spfli~connid = sflight~connid
where spfli~cityto in s_cityto and sflight~fldate = p_date and spfli~cityfrom = p_cityfr.
endif.
endform. " SUB_GET_DATA
*&---------------------------------------------------------------------*
*& Form SUB_PROCESS_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form sub_process_data .
"航班名称
itab_data = t_data.
sort itab_data by carrid.
delete adjacent duplicates from itab_data comparing carrid.
select carrid carrname into table t_scarr from scarr for all entries in itab_data
where carrid = itab_data-carrid.
"目标城市
refresh itab_data.
itab_data = t_data.
sort itab_data by cityto.
delete adjacent duplicates from itab_data comparing cityto.
if s_cityto-high is initial.
sort t_data by carrname.
loop at t_data into wa_data.
read table t_scarr into wa_scarr with key carrid = wa_data-carrid.
wa_alvdata-carrid = wa_data-carrid.
wa_alvdata-carrname = wa_scarr-carrname.
wa_alvdata-cityfrom = wa_data-cityfrom.
wa_alvdata-airpfrom = wa_data-airpfrom.
wa_alvdata-cityto = wa_data-cityto. "
wa_alvdata-airpto = wa_data-airpto.
wa_alvdata-deptime = wa_data-deptime. "
wa_alvdata-arrtime = wa_data-arrtime.
wa_alvdata-fldate = wa_data-fldate.
append wa_alvdata to t_alvdata.
clear wa_scarr.
clear wa_data.
clear wa_alvdata.
endloop.
else.
sort t_data by cityto.
data count type i.
loop at itab_data into wa_tempdata.
loop at t_data into wa_data where cityto = wa_tempdata-cityto.
count = count + 1.
read table t_scarr into wa_scarr with key carrid = wa_data-carrid.
wa_alvdata-carrid = wa_data-carrid.
wa_alvdata-carrname = wa_scarr-carrname.
wa_alvdata-cityfrom = wa_data-cityfrom.
wa_alvdata-airpfrom = wa_data-airpfrom.
wa_alvdata-cityto = wa_data-cityto. "
wa_alvdata-airpto = wa_data-airpto.
wa_alvdata-deptime = wa_data-deptime. "
wa_alvdata-arrtime = wa_data-arrtime.
wa_alvdata-fldate = wa_data-fldate.
append wa_alvdata to t_alvdata.
clear wa_scarr.
clear wa_data.
clear wa_alvdata.
endloop.
if count > 0.
wa_alvdata-carrid = ''.
wa_alvdata-carrname = '小计'.
wa_alvdata-cityfrom = ''.
wa_alvdata-airpfrom = ''.
wa_alvdata-cityto = count. "
wa_alvdata-airpto = ''.
wa_alvdata-deptime = ''. "
wa_alvdata-arrtime = ''.
condense wa_alvdata-cityto.
append wa_alvdata to t_alvdata.
clear wa_alvdata.
endif.
clear wa_tempdata.
count = 0.
endloop.
endif.
endform. " SUB_PROCESS_DATA
*&---------------------------------------------------------------------*
*& Form get_user_name
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_CGNAME text
*----------------------------------------------------------------------*
form get_user_name changing p_name.
select single * from usr21 where bname = p_name.
select single * from adrp where persnumber = usr21-persnumber.
check sy-subrc = 0.
concatenate adrp-name_last adrp-name_first into p_name.
condense p_name no-gaps.
endform. " get_user_name
*&---------------------------------------------------------------------*
*& Form sub_pop_f4value
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form pop_f4value .
types: begin of ty_spfli,
cityfrom like spfli-cityfrom,
end of ty_spfli.
data: t_spfli type table of ty_spfli,
wa_spfli type ty_spfli.
select distinct cityfrom from spfli into table t_spfli order by cityfrom.
data: wa_fields like help_value,
t_fields like table of help_value.
types: begin of ty_valuetab,
string(60),
end of ty_valuetab.
data: t_valuetab type table of ty_valuetab,
wa_valuetab type ty_valuetab.
types: begin of ty_help,
xh(2) type c,
cityfrom like spfli-cityfrom,
end of ty_help.
data: t_help type table of ty_help.
clear wa_fields.
wa_fields-tabname = 'ZSPFLI_LZX'.
wa_fields-fieldname = 'XH'.
append wa_fields to t_fields.
clear wa_fields.
wa_fields-tabname = 'ZSPFLI_LZX'.
wa_fields-fieldname = 'CITYFROM'.
wa_fields-selectflag = 'X'.
append wa_fields to t_fields..
data: xh type i value 0.
loop at t_spfli into wa_spfli.
xh = xh + 1.
wa_valuetab-string = xh.
append wa_valuetab to t_valuetab.
wa_valuetab-string = wa_spfli-cityfrom.
append wa_valuetab to t_valuetab.
endloop.
call function 'HELP_VALUES_GET_WITH_TABLE'
exporting
title_in_values_list = '自定义起飞城市帮助'
titel = 'Please choose you select:'
importing
select_value = p_cityfr
tables
fields = t_fields
valuetab = t_valuetab.
if sy-subrc ne 0.
* MESSAGE S000 WITH 'No value selected!'.
message text-e02 type 'S'.
endif.
endform. " pop_f4value
zalv_alv.
form display_data .
data:
l_wa_fieldcat type slis_fieldcat_alv,
l_wa_layout type slis_layout_alv,
lt_fieldcat type table of slis_fieldcat_alv.
"ALV标题
call function 'REUSE_ALV_EVENTS_GET'
exporting
i_list_type = 0
importing
et_events = i_events.
read table i_events
with key name = slis_ev_top_of_page
into w_events.
if sy-subrc = 0.
move 'ALV_TOP_OF_PAGE' to w_events-form.
modify i_events from w_events index sy-tabix.
endif.
read table i_events
with key name = slis_ev_end_of_list
into w_events.
if sy-subrc = 0.
move 'ALV_END_OF_LIST' to w_events-form.
modify i_events from w_events index sy-tabix.
endif.
mc_add_fieldcat: 'CARRNAME' '航班名称' '' '20' '' 'L' ,
'FLDATE' '航班日期' '' '20' '' 'L' ,
'CITYFROM' '起飞城市' '' '20' '' 'L' ,
'AIRPFROM' '起飞机场' '' '20' '' 'L' ,
'CITYTO' '目标城市' '' '20' '' 'L' ,
'AIRPTO' '目的机场' '' '20' '' 'L' ,
'DEPTIME' '启程时间' '' '20' '' 'C' ,
'ARRTIME' '到达时间' '' '20' '' 'C' ,
'PAYMENTSUM' '当前预定总数' '' '20' '' 'R' .
l_wa_layout-colwidth_optimize = 'X'.
l_wa_layout-box_fieldname = 'SEL'. "必须大写
l_wa_layout-edit_mode = 'X'. "
l_wa_layout-zebra = 'X'.
l_wa_layout-f2code = '&ETA'. " 双击事件(明细事件)
* l_wa_layout-detail_popup = 'X'. "双击事件
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
i_callback_program = sy-repid
i_callback_pf_status_set = 'SET_STATUS'
i_callback_user_command = 'USER_COMMAND'
is_layout = l_wa_layout
i_save = 'A'
it_fieldcat = lt_fieldcat
it_events = i_events
i_grid_title = ' 航班信息表子标题'
tables
t_outtab = t_alvdata
exceptions
program_error = 1
others = 2.
if sy-subrc <> 0.
message text-e02 type 'S' display like 'E'.
endif.
endform. "DISPLAY_DATA
*&---------------------------------------------------------------------*
*& Form alv_top_of_page
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form alv_top_of_page.
user_name = sy-uname.
perform get_user_name changing user_name.
clear: i_list_comments[].
w_list_comments-typ = 'H'. "H=Header, S=Selection, A=Action
w_list_comments-key = ''.
concatenate '航班信息表' '' into w_list_comments-info.
append w_list_comments to i_list_comments.
w_list_comments-typ = 'S'. "H=Header, S=Selection, A=Action
w_list_comments-key = ''.
w_list_comments-info = '查 询 人:'.
concatenate w_list_comments-info user_name into w_list_comments-info.
w_list_comments-typ = 'S'. "H=Header, S=Selection, A=Action
w_list_comments-key = ''.
concatenate w_list_comments-info ' 查询日期:' sy-datum+0(4) '/' sy-datum+4(2) '/' sy-datum+6(2) into w_list_comments-info.
append w_list_comments to i_list_comments.
call function 'REUSE_ALV_COMMENTARY_WRITE'
exporting
it_list_commentary = i_list_comments.
endform. "alv_top_of_page
*&--------------------------------------------------------------------*
*& Form alv_end_of_list
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form alv_end_of_list.
data count type i.
describe table t_data lines count.
clear: i_list_comments[].
w_list_comments-typ = 'S'.
w_list_comments-info = count.
condense w_list_comments-info.
concatenate '查询结果:总记录数为 ' w_list_comments-info into w_list_comments-info.
append w_list_comments to i_list_comments.
clear w_list_comments.
call function 'REUSE_ALV_COMMENTARY_WRITE'
exporting
it_list_commentary = i_list_comments
.
endform. "alv_end_of_list
*&---------------------------------------------------------------------*
*& Form SET_STATUS
*&---------------------------------------------------------------------*
* text STATUS按钮
*----------------------------------------------------------------------*
* -->RT_EXTAB text
*----------------------------------------------------------------------*
form set_status using rt_extab type slis_t_extab.
set pf-status 'Z_STATUS_001' excluding rt_extab.
endform. "set_status
*&---------------------------------------------------------------------*
*& Form USER_COMMAND
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->RF_UCOMM text
* -->RS_SELFIELD text
*----------------------------------------------------------------------*
form user_command using rf_ucomm like sy-ucomm
rs_selfield type slis_selfield.
case rf_ucomm.
when 'PRINT'.
read table t_alvdata transporting no fields with key sel = 'X'.
if sy-subrc ne 0.
perform sub_print_data using 'ALL'.
else.
perform sub_print_data using 'SAME' .
endif.
when 'EXCEL'.
check t_alvdata is not initial.
g_starrow = 4.
read table t_alvdata transporting no fields with key sel = 'X'.
if sy-subrc ne 0.
perform sub_export_excel using 'ALL' .
else.
perform sub_export_excel using 'SAME' .
endif.
call function 'ZPETROCHINA_EXCEL_OUTPUT'
exporting
excel_template = c_exceltemplate
macro = 'write_cell'
exit_program = sy-repid
exit_subroutine = 'SUB_ADDITION_EXCEL'
tables
data_source = t_exceldata.
when '&IC1'.
endcase.
endform.
zalv_excel.
form sub_export_excel using p_type.
data: user_name(20) type c,
print_day(10) type c.
user_name = sy-uname.
perform get_user_name changing user_name.
concatenate sy-datum+0(4) '/' sy-datum+4(2) '/' sy-datum+6(2) into print_day.
refresh t_exceldata.
* 抬头
t_exceldata-value = user_name.
t_exceldata-row = 2.
t_exceldata-column = 2.
append t_exceldata.
t_exceldata-value = print_day.
t_exceldata-row = 2.
t_exceldata-column = 4.
append t_exceldata.
clear t_exceldata.
* 主体数据
loop at t_alvdata into wa_alvdata.
if p_type = 'ALL'.
perform build_excel.
elseif p_type = 'SAME'.
if wa_alvdata-sel = 'X'.
perform build_excel.
endif.
endif.
endloop.
endform. " sub_export_excel
*&---------------------------------------------------------------------*
*& Form sub_addition_excel
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->EXCEL text
* -->WBOOK text
* -->SHEET text
* -->CELL text
* -->RANGE text
*----------------------------------------------------------------------*
form sub_addition_excel changing excel wbook sheet cell range.
g_starrow = g_starrow - 1.
perform sub_excel_table using sheet 4 1 g_starrow 9 1 changing excel.
endform. "SUB_ADDITION_EXCEL
*&---------------------------------------------------------------------*
*& Form build_excel
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form build_excel .
* 航班名称
t_exceldata-value = wa_alvdata-carrname.
t_exceldata-row = g_starrow.
t_exceldata-column = 1.
append t_exceldata.
clear t_exceldata.
t_exceldata-value = wa_alvdata-fldate.
t_exceldata-row = g_starrow.
t_exceldata-column = 2.
append t_exceldata.
clear t_exceldata.
* 起飞城市
t_exceldata-value = wa_alvdata-cityfrom.
t_exceldata-row = g_starrow.
t_exceldata-column = 3.
append t_exceldata.
clear t_exceldata.
* 起飞机场
t_exceldata-value = wa_alvdata-airpfrom.
t_exceldata-row = g_starrow.
t_exceldata-column = 4.
append t_exceldata.
clear t_exceldata.
* 目标城市
t_exceldata-value = wa_alvdata-cityto.
t_exceldata-row = g_starrow.
t_exceldata-column = 5.
append t_exceldata.
clear t_exceldata.
* 目的机场
t_exceldata-value = wa_alvdata-airpto.
t_exceldata-row = g_starrow.
t_exceldata-column = 6.
append t_exceldata.
clear t_exceldata.
* 启程时间
t_exceldata-value = wa_alvdata-deptime.
t_exceldata-row = g_starrow.
t_exceldata-column = 7.
append t_exceldata.
clear t_exceldata.
* 到达时间
t_exceldata-value = wa_alvdata-arrtime.
t_exceldata-row = g_starrow.
t_exceldata-column = 8.
append t_exceldata.
clear t_exceldata.
* 当前预定总数
t_exceldata-value = wa_alvdata-paymentsum.
t_exceldata-row = g_starrow.
t_exceldata-column = 9.
append t_exceldata.
clear t_exceldata.
add 1 to g_starrow.
endform.
zalv_print.
form sub_print_data using p_type.
data: linecount type i,
times type i.
clear wa_alvdata.
refresh t_printdata.
loop at t_alvdata into wa_alvdata.
if p_type = 'ALL'.
wa_printdata-carrid = wa_alvdata-carrid.
wa_printdata-carrname = wa_alvdata-carrname.
wa_printdata-cityfrom = wa_alvdata-cityfrom.
wa_printdata-airpfrom = wa_alvdata-airpfrom.
wa_printdata-cityto = wa_alvdata-cityto.
wa_printdata-airpto = wa_alvdata-airpto.
wa_printdata-deptime = wa_alvdata-deptime.
wa_printdata-arrtime = wa_alvdata-arrtime.
wa_printdata-paymentsum = wa_alvdata-paymentsum.
else.
if wa_alvdata-sel = 'X'.
wa_printdata-carrid = wa_alvdata-carrid.
wa_printdata-carrname = wa_alvdata-carrname.
wa_printdata-cityfrom = wa_alvdata-cityfrom.
wa_printdata-airpfrom = wa_alvdata-airpfrom.
wa_printdata-cityto = wa_alvdata-cityto.
wa_printdata-airpto = wa_alvdata-airpto.
wa_printdata-deptime = wa_alvdata-deptime.
wa_printdata-arrtime = wa_alvdata-arrtime.
wa_printdata-paymentsum = wa_alvdata-paymentsum.
endif.
endif.
append wa_printdata to t_printdata.
clear wa_printdata.
endloop.
*以下代码用于测试,打印60个行项目,每页10行。
linecount = lines( t_printdata ).
if linecount < 60.
times = 60 - linecount.
do times times.
append wa_printdata to t_printdata.
enddo.
endif.
export a = t_printdata to memory id 'printlzx'.
data user_name(10) type c.
user_name = sy-uname.
perform get_user_name changing user_name.
call function 'SSF_FUNCTION_MODULE_NAME'
exporting
formname = 'ZSMART_DEMO_LZX_006'
variant = ''
direct_call = ''
importing
fm_name = fm_name
exceptions
no_form = 1
no_function_module = 2
others = 3.
if sy-subrc <> 0.
endif.
call function fm_name
exporting
ptr_items = 'printlzx'
user_name = user_name
exceptions
others = 5.
if sy-subrc <> 0.
endif.
endform.