动A 动E
*&---------------------------------------------------------------------* *& Report ZCESHI_26 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT zceshi_03. TABLES: afpo,aufk,plaf,makt,t001w. *alv 定义 TYPE-POOLS: slis. DATA: afield TYPE slis_fieldcat_alv, fieldcat TYPE slis_t_fieldcat_alv, gs_layout TYPE slis_layout_alv, g_repid TYPE sy-repid, i_layout TYPE slis_layout_alv, gridlsl TYPE lvc_s_glay. *downloadexcel 定义 TYPE-POOLS ole2. DATA: v_excel TYPE ole2_object, v_book TYPE ole2_object, v_cell TYPE ole2_object, v_range TYPE ole2_object, v_font TYPE ole2_object, v_color TYPE ole2_object, v_column TYPE ole2_object, v_border TYPE ole2_object. DATA:count1 TYPE i VALUE 0. DATA:count2 TYPE i VALUE 0. DATA:count3 TYPE i VALUE 0. DATA:count4 TYPE i VALUE 0. DATA:count5 TYPE i VALUE 0. DATA:count6 TYPE i VALUE 0. DATA:count7 TYPE i VALUE 0. DATA:count8 TYPE i VALUE 0. DATA:count9 TYPE i VALUE 0. DATA:count10 TYPE i VALUE 0. DATA:count11 TYPE i VALUE 0. DATA:count12 TYPE i VALUE 0. *内表定义区 DATA: BEGIN OF itab_d_ty, matnr LIKE afpo-matnr, maktx LIKE makt-maktx, sumnb LIKE afpo-psmng, meins LIKE afpo-meins, month(6) TYPE c, END OF itab_d_ty. DATA: BEGIN OF wa_month, month(6) TYPE c, END OF wa_month. DATA: BEGIN OF itab_alv_ty, matnr LIKE afpo-matnr, "生产订单物料编码 maktx LIKE makt-maktx, "生产物料名称 3 LIKE afpo-psmng, 4 LIKE afpo-psmng, 5 LIKE afpo-psmng, 6 LIKE afpo-psmng, 7 LIKE afpo-psmng, 8 LIKE afpo-psmng, 9 LIKE afpo-psmng, 10 LIKE afpo-psmng, 11 LIKE afpo-psmng, 12 LIKE afpo-psmng, 13 LIKE afpo-psmng, 14 LIKE afpo-psmng, sumnb LIKE afpo-psmng, meins LIKE afpo-meins, flag(1), END OF itab_alv_ty. DATA: itab_d LIKE itab_d_ty OCCURS 0 WITH HEADER LINE. DATA: it_month LIKE wa_month OCCURS 0 WITH HEADER LINE. DATA: itab_alv LIKE itab_alv_ty OCCURS 0 WITH HEADER LINE. DATA: str_name2 TYPE string. PARAMETERS: p_pwerk LIKE afpo-pwerk OBLIGATORY, p_date LIKE sy-datum OBLIGATORY. AT SELECTION-SCREEN ON p_pwerk. PERFORM: check_pwerk. START-OF-SELECTION. PERFORM get_data. PERFORM creat_allmonth. PERFORM alv_display. *&---------------------------------------------------------------------* *& Form CHECK_PWERK *&---------------------------------------------------------------------* * 字段检查,检查工厂是否存在 *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM check_pwerk . DATA: mesg_01 TYPE string. SELECT SINGLE * FROM afpo WHERE pwerk = p_pwerk. IF sy-subrc = 4. CONCATENATE p_pwerk '工厂不存在!' INTO mesg_01. MESSAGE mesg_01 TYPE 'W'. CLEAR sy-subrc. ENDIF. ENDFORM. " CHECK_PWERK *&---------------------------------------------------------------------* *& Form GET_DATA *&---------------------------------------------------------------------* * 主数据取数 *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM get_data . DATA: BEGIN OF itab_01_ty, pwerk LIKE afpo-pwerk, "工厂 matnr LIKE afpo-matnr, "生产订单物料编码 maktx LIKE makt-maktx, "生产物料名称 psmng LIKE afpo-psmng, "生产订单数量 dgltp LIKE afpo-dgltp, "生产订单基本结束日期 meins LIKE afpo-meins, month(6) TYPE c, END OF itab_01_ty. DATA: BEGIN OF itab_02_ty, plwrk LIKE plaf-plwrk, matnr LIKE plaf-matnr, "计划订单物料编码 maktx LIKE makt-maktx, "计划物料名称 gsmng LIKE plaf-gsmng, "计划订单数量 pedtr LIKE plaf-pedtr, "计划订单基本结束日期 meins LIKE plaf-meins, month(6) TYPE c, END OF itab_02_ty. DATA: BEGIN OF itab_tw_ty, name2 LIKE t001w-name2, END OF itab_tw_ty. DATA: itab_01 LIKE itab_01_ty OCCURS 0 WITH HEADER LINE. DATA: itab_02 LIKE itab_02_ty OCCURS 0 WITH HEADER LINE. DATA: p_date1 TYPE dats. DATA: string_1(6) TYPE c. DATA: rows TYPE i. CLEAR: itab_01[],itab_02[]. * 获取12月之后日期 PERFORM get_date USING p_date CHANGING p_date1. CONCATENATE p_date1(6) '01' INTO p_date1."取一年之后当月的第一天 * 取生产订单的计划数量和物料号: SELECT afpo~pwerk afpo~matnr makt~maktx afpo~psmng afpo~dgltp afpo~meins INTO CORRESPONDING FIELDS OF TABLE itab_01 FROM afpo INNER JOIN aufk ON aufk~aufnr = afpo~aufnr AND aufk~loekz NE 'X' INNER JOIN makt ON makt~matnr = afpo~matnr AND makt~spras = 1 WHERE afpo~pwerk = p_pwerk AND aufk~auart IN ('JJ01', 'JJ05', 'JL01', 'JL05', 'KC01', 'KC05', 'NM01', 'NM02', 'NM03', 'NX01', 'NX02', 'NX03', 'WY01', 'WY02', 'WE01', 'WE02', 'WS01', 'WS02') AND afpo~dgltp >= p_date AND afpo~dgltp < p_date1 ORDER BY afpo~dgltp. * 选出符合条件的计划订单 SELECT plaf~plwrk plaf~matnr makt~maktx plaf~gsmng plaf~pedtr plaf~meins INTO CORRESPONDING FIELDS OF TABLE itab_02 FROM plaf INNER JOIN makt ON makt~matnr = plaf~matnr AND makt~spras = 1 WHERE plaf~plwrk = p_pwerk AND plaf~pedtr >= p_date AND plaf~pedtr < p_date1 AND plaf~paart = 'LA' AND plaf~matnr NOT LIKE '000000000080%' . IF itab_01[] IS INITIAL AND itab_02[] IS INITIAL. MESSAGE '未包含任何生产订单和计划单!' TYPE 'I'. STOP. RETURN. ENDIF. *获取工厂描述 SELECT name2 INTO CORRESPONDING FIELDS OF itab_tw_ty FROM t001w WHERE werks = p_pwerk AND spras = 1. str_name2 = itab_tw_ty-name2. ENDSELECT. *提炼itab_01和itab_02的数据 LOOP AT itab_01 INTO itab_01_ty. MOVE itab_01_ty-matnr TO itab_d_ty-matnr. MOVE itab_01_ty-maktx TO itab_d_ty-maktx. MOVE itab_01_ty-psmng TO itab_d_ty-sumnb. MOVE itab_01_ty-dgltp(6) TO string_1. MOVE string_1 TO itab_d_ty-month. MOVE itab_01_ty-meins TO itab_d_ty-meins. COLLECT itab_d_ty INTO itab_d. ENDLOOP. CLEAR itab_01[]. LOOP AT itab_02 INTO itab_02_ty. MOVE itab_02_ty-matnr TO itab_d_ty-matnr. MOVE itab_02_ty-maktx TO itab_d_ty-maktx. MOVE itab_02_ty-gsmng TO itab_d_ty-sumnb. MOVE itab_02_ty-pedtr(6) TO string_1. MOVE string_1 TO itab_d_ty-month. MOVE itab_02_ty-meins TO itab_d_ty-meins. COLLECT itab_d_ty INTO itab_d. ENDLOOP. CLEAR itab_02[]. SORT itab_d BY month matnr. ENDFORM. " GET_DATA *&---------------------------------------------------------------------* *& Form GET_DATE *&---------------------------------------------------------------------* * 计算出当前年月之后一年的年月 *----------------------------------------------------------------------* * -->P_P_DATE text * <--P_P_DATE1 text *----------------------------------------------------------------------* FORM get_date USING l_date CHANGING l_date1. CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL' EXPORTING date = l_date days = 0 "天数加N或者减N 不加也不减 写0 months = 12 "月份加N或者减N 不加也不减 写0 signum = '+' "可以是+或者- years = 0"年份加N或者减N 不加也不减 写0 IMPORTING calc_date = l_date1."得到加减之后的日期 ENDFORM. " GET_DATE *&---------------------------------------------------------------------* *& Form CREAT_ALLMONTH *&---------------------------------------------------------------------* * 获取满足条件的数据的所有不相同的年月份 *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM creat_allmonth . DATA: c1(6) TYPE c, y2(4) TYPE c, m3(2) TYPE c. MOVE p_date(6) TO c1. MOVE p_date(4) TO y2. MOVE p_date+4(2) TO m3. APPEND c1 TO it_month. DO 11 TIMES. m3 = m3 + 1. IF m3 < 10. CONCATENATE y2 '0' m3 INTO c1. APPEND c1 TO it_month. ELSEIF m3 > 9 AND m3 < 13. CONCATENATE y2 m3 INTO c1. APPEND c1 TO it_month. ELSEIF m3 > 12. m3 = 1. y2 = y2 + 1. CONCATENATE y2 '0' m3 INTO c1. APPEND c1 TO it_month. ENDIF. ENDDO. ENDFORM. " CREAT_ALLMONTH *&---------------------------------------------------------------------* *& Form ALV_DISPLAY *&---------------------------------------------------------------------* * 调用ALV *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM alv_display . PERFORM get_alv_data. PERFORM layout_build. PERFORM fieldcat_init USING fieldcat[]. PERFORM display_alv. ENDFORM. " ALV_DISPLAY *&---------------------------------------------------------------------* *& Form GET_ALV_DATA *&---------------------------------------------------------------------* * 为ALV取得数据 *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM get_alv_data . FIELD-SYMBOLS: <dyn_field> TYPE any. DATA: col(4) TYPE c. DEFINE cutmonth. loop at it_month into wa_month. if wa_month-month = &1. col = sy-tabix + 2. assign component col of structure itab_alv_ty to <dyn_field>. <dyn_field> = &2. clear col. endif. endloop. END-OF-DEFINITION. LOOP AT itab_d INTO itab_d_ty. ASSIGN COMPONENT 1 OF STRUCTURE itab_alv_ty TO <dyn_field>. PERFORM cut_zero USING itab_d_ty-matnr CHANGING itab_d_ty-matnr. <dyn_field> = itab_d_ty-matnr. ASSIGN COMPONENT 2 OF STRUCTURE itab_alv_ty TO <dyn_field>. <dyn_field> = itab_d_ty-maktx. cutmonth itab_d_ty-month itab_d_ty-sumnb. ASSIGN COMPONENT 16 OF STRUCTURE itab_alv_ty TO <dyn_field>. <dyn_field> = itab_d_ty-meins. COLLECT itab_alv_ty INTO itab_alv. CLEAR itab_alv_ty. ENDLOOP. LOOP AT itab_alv INTO itab_alv_ty. itab_alv_ty-sumnb = itab_alv_ty-3 + itab_alv_ty-4 + itab_alv_ty-5 + itab_alv_ty-6 + itab_alv_ty-7 + itab_alv_ty-8 + itab_alv_ty-9 + itab_alv_ty-10 + itab_alv_ty-11 + itab_alv_ty-12 + itab_alv_ty-13 + itab_alv_ty-14. MODIFY itab_alv FROM itab_alv_ty. ENDLOOP. LOOP AT itab_alv. IF itab_alv-3 IS NOT INITIAL. count1 = count1 + 1. ENDIF. IF itab_alv-4 IS NOT INITIAL. count2 = count2 + 1. ENDIF. IF itab_alv-5 IS NOT INITIAL. count3 = count3 + 1. ENDIF. IF itab_alv-6 IS NOT INITIAL. count4 = count4 + 1. ENDIF. IF itab_alv-7 IS NOT INITIAL. count5 = count5 + 1. ENDIF. IF itab_alv-8 IS NOT INITIAL. count6 = count6 + 1. ENDIF. IF itab_alv-9 IS NOT INITIAL. count7 = count7 + 1. ENDIF. IF itab_alv-10 IS NOT INITIAL. count8 = count8 + 1. ENDIF. IF itab_alv-11 IS NOT INITIAL. count9 = count9 + 1. ENDIF. IF itab_alv-12 IS NOT INITIAL. count10 = count10 + 1. ENDIF. IF itab_alv-13 IS NOT INITIAL. count11 = count11 + 1. ENDIF. IF itab_alv-14 IS NOT INITIAL. count12 = count12 + 1. ENDIF. ENDLOOP. ENDFORM. " GET_ALV_DATA *&---------------------------------------------------------------------* *& Form LAYOUT_BUILD *&---------------------------------------------------------------------* * 设置ALV的显示格式 *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM layout_build . i_layout-box_fieldname = 'FLAG'. i_layout-colwidth_optimize = 'X'. "优化列宽选项是否设置 i_layout-zebra = 'X'. i_layout-detail_initial_lines = 'X'. i_layout-no_vline = 'X'." layout_build ENDFORM. " LAYOUT_BUILD *&---------------------------------------------------------------------* *& Form FIELDCAT_INIT *&---------------------------------------------------------------------* * 设置ALV的列项目 *----------------------------------------------------------------------* * -->P_FIELDCAT[] text *----------------------------------------------------------------------* FORM fieldcat_init USING fieldcat TYPE slis_t_fieldcat_alv. DATA: string(10) TYPE c. DATA: tabix(6) TYPE c. DATA: c1(4) TYPE c. DATA: c2(2) TYPE c. DATA: c3(2) TYPE c. DATA: afieldcat TYPE slis_fieldcat_alv, pos TYPE i. CLEAR pos. DEFINE d_get_field. pos = pos + 1. clear afieldcat. afieldcat-col_pos = pos. afieldcat-fieldname = &1. afieldcat-seltext_l = &2. afieldcat-key = &3. afieldcat-no_zero = &4. afieldcat-decimals_out = '0'. append afieldcat to fieldcat. END-OF-DEFINITION. d_get_field 'MATNR' '物料编号' 'X' ''. d_get_field 'MAKTX' '物料描述' 'X' ''. LOOP AT it_month INTO wa_month. MOVE wa_month-month(4) TO c1. MOVE wa_month-month+4(2) TO c2. CONCATENATE c1 '年' c2 '月' INTO string. tabix = sy-tabix + 2. CONDENSE tabix NO-GAPS. CASE tabix. WHEN '3'. IF count1 NE 0. d_get_field tabix string '' 'X'. ENDIF. WHEN '4'. IF count2 NE 0. d_get_field tabix string '' 'X'. ENDIF. WHEN '5'. IF count3 NE 0. d_get_field tabix string '' 'X'. ENDIF. WHEN '6'. IF count4 NE 0. d_get_field tabix string '' 'X'. ENDIF. WHEN '7'. IF count5 NE 0. d_get_field tabix string '' 'X'. ENDIF. WHEN '8'. IF count6 NE 0. d_get_field tabix string '' 'X'. ENDIF. WHEN '9'. IF count7 NE 0. d_get_field tabix string '' 'X'. ENDIF. WHEN '10'. IF count8 NE 0. d_get_field tabix string '' 'X'. ENDIF. WHEN '11'. IF count9 NE 0. d_get_field tabix string '' 'X'. ENDIF. WHEN '12'. IF count10 NE 0. d_get_field tabix string '' 'X'. ENDIF. WHEN '13'. IF count11 NE 0. d_get_field tabix string '' 'X'. ENDIF. WHEN '14'. IF count12 NE 0. d_get_field tabix string '' 'X'. ENDIF. WHEN OTHERS. ENDCASE. * d_get_field tabix string '' 'X'. CLEAR: c1,c2,tabix,string. ENDLOOP. d_get_field 'SUMNB' '总计' '' ''. d_get_field 'MEINS' '单位' '' ''. ENDFORM. " FIELDCAT_INIT *&---------------------------------------------------------------------* *& Form DISPLAY_ALV *&---------------------------------------------------------------------* * 显示ALV *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM display_alv . g_repid = sy-repid. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING * i_grid_settings = gridlsl * i_structure_name = '' i_callback_program = g_repid i_callback_pf_status_set = 'USER_STATUS' i_callback_user_command = 'USER_COMMAND' it_fieldcat = fieldcat is_layout = i_layout i_callback_top_of_page = slis_ev_top_of_page i_save = 'A' * it_sort = it_sort * it_events = er_events[] TABLES t_outtab = itab_alv. CLEAR itab_alv[]. ENDFORM. " DISPLAY_ALV *&---------------------------------------------------------------------* *& Form user_status *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM user_status USING extab TYPE slis_t_extab. SET TITLEBAR 'TITLE_ALV'. SET PF-STATUS 'STSTUS_ALV'. ENDFORM. "USER_STATUS *&---------------------------------------------------------------------* *& Form user_command *&---------------------------------------------------------------------* * 抓取ALV的功能码 *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM user_command USING r_ucomm LIKE sy-ucomm rs_selfield TYPE slis_selfield. rs_selfield-refresh = 'X'. r_ucomm = sy-ucomm. CLEAR sy-ucomm. CASE r_ucomm. WHEN 'DLOD'. PERFORM download_excel. WHEN OTHERS. ENDCASE. ENDFORM. "user_command *&---------------------------------------------------------------------* *& Form top_of_page *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM top_of_page. DATA: lit_header TYPE slis_t_listheader, lhe_header TYPE slis_listheader. * wk_rate TYPE string. CLEAR lhe_header. lhe_header-typ = 'H'. lhe_header-info = str_name2. APPEND lhe_header TO lit_header. CLEAR lhe_header. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING it_list_commentary = lit_header. ENDFORM. "top_of_page *&---------------------------------------------------------------------* *& Form DOWNLOAD_EXCEL *&---------------------------------------------------------------------* * 导出到Excel *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM download_excel . DATA column TYPE i. DATA: str_title TYPE string. DATA: str_month TYPE string. *对单元格操作的宏 DEFINE fill_cell. call method of v_excel 'CELLS' = v_cell exporting #1 = &1 #2 = &2. set property of v_cell 'value' = &3. call method of v_cell 'FONT' = v_font. set property of v_font 'Bold' = &4. set property of v_font 'size' = &5. END-OF-DEFINITION. *为特定区域加边框的宏 DEFINE addborder. call method of v_excel 'Range' = v_range exporting #1 = &1 #2 = &2. get property of v_range 'Borders' = v_border. set property of v_border 'LineStyle' = 1. set property of v_border 'WEIGHT' = 2. free object v_border. END-OF-DEFINITION. *产生对象 CREATE OBJECT v_excel 'Excel.Application'. CALL METHOD OF v_excel 'Workbooks' = v_book. CALL METHOD OF v_book 'ADD'. *生产大标题,居中,加粗 CONCATENATE str_name2 '整机投入产品明细表' INTO str_title. fill_cell 1 1 str_title 1 18. SET PROPERTY OF v_cell 'HorizontalAlignment' = -4108. *合并大标题所在单元格 CALL METHOD OF v_excel 'Range' = v_range EXPORTING #1 = 'A1' #2 = 'P1'. CALL METHOD OF v_range 'select'. SET PROPERTY OF v_range 'MergeCells' = 1. *添加年月份标题 fill_cell 2 3 '年份月份' 1 10. SET PROPERTY OF v_cell 'HorizontalAlignment' = -4108. *合并年月份标题单元格,居中 CALL METHOD OF v_excel 'Range' = v_range EXPORTING #1 = 'C2' #2 = 'P2'. CALL METHOD OF v_range 'select'. SET PROPERTY OF v_range 'MergeCells' = 1. *添加主表表头信息 fill_cell 2 1 '物料编码' 1 10. SET PROPERTY OF v_cell 'HorizontalAlignment' = -4108. fill_cell 2 2 '物料描述' 1 10. SET PROPERTY OF v_cell 'HorizontalAlignment' = -4108. CALL METHOD OF v_excel 'Range' = v_range EXPORTING #1 = 'A2' #2 = 'A3'. CALL METHOD OF v_range 'select'. SET PROPERTY OF v_range 'MergeCells' = 1. CALL METHOD OF v_excel 'Range' = v_range EXPORTING #1 = 'B2' #2 = 'B3'. CALL METHOD OF v_range 'select'. SET PROPERTY OF v_range 'MergeCells' = 1. LOOP AT it_month INTO wa_month. column = sy-tabix + 2. CONCATENATE wa_month-month(4) '年' wa_month-month+4(2) '月' INTO str_month. fill_cell 3 column str_month 1 10. ENDLOOP. fill_cell 3 15 '总计' 1 10. fill_cell 3 16 '单位' 1 10. *为数据区赋值 FIELD-SYMBOLS: <field> TYPE any. DATA lv_txt(50) TYPE c. DATA: row_i TYPE i, col_i TYPE i. LOOP AT itab_alv. row_i = sy-tabix + 3. DO 16 TIMES. col_i = sy-index. ASSIGN COMPONENT sy-index OF STRUCTURE itab_alv TO <field>. lv_txt = <field>. IF col_i > 2 AND col_i < 16 AND lv_txt = 0. lv_txt = ''. ENDIF. fill_cell row_i col_i lv_txt 0 10. * IF col_i = 15. * CALL METHOD of v_cell 'INTERIOR' = v_color. * SET PROPERTY OF v_color 'ColorIndex' = 8. * ENDIF. ENDDO. ENDLOOP. *为除了大标题外的所有加边框 DATA: intstr TYPE string. intstr = row_i. CONCATENATE 'P' intstr INTO intstr. addborder 'A2' 'B3'. addborder 'C2' 'P3'. addborder 'A4' intstr. *设置列的长度为自适应 CALL METHOD OF v_excel 'Columns' = v_column. CALL METHOD OF v_column 'Autofit'. *设置Excel可见 SET PROPERTY OF v_excel 'Visible' = 1. ENDFORM. " DOWNLOAD_EXCEL *&---------------------------------------------------------------------* *& Form CUT_ZERO *&---------------------------------------------------------------------* * 去掉前导零 *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM cut_zero USING input_data CHANGING output_data. CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = input_data IMPORTING output = output_data. ENDFORM. " CUT_ZERO