ABAP-ALV合并单元格操作
1 *SE24基于源代码创建类 2 CLASS zcl_gui_alv_grid_merge DEFINITION 3 PUBLIC 4 INHERITING FROM cl_gui_alv_grid 5 CREATE PUBLIC . 6 7 PUBLIC SECTION. 8 METHODS z_set_merge_horiz 9 IMPORTING 10 row TYPE i 11 CHANGING 12 tab_col_merge TYPE lvc_t_co01 . 13 METHODS z_set_merge_vert 14 IMPORTING 15 row TYPE i 16 CHANGING 17 tab_col_merge TYPE lvc_t_co01 . 18 METHODS z_display . 19 METHODS z_set_cell_style 20 IMPORTING 21 row TYPE i OPTIONAL 22 col TYPE i OPTIONAL 23 style TYPE lvc_style 24 style2 TYPE lvc_style OPTIONAL . 25 METHODS z_set_fixed_col_row 26 IMPORTING 27 col TYPE i 28 row TYPE i . 29 METHODS z_init_cell_styles . 30 PROTECTED SECTION. 31 PRIVATE SECTION. 32 ENDCLASS. 33 34 CLASS ZCL_GUI_ALV_GRID_MERGE IMPLEMENTATION. 35 36 METHOD z_display. 37 38 DATA lv_stable TYPE lvc_s_stbl. 39 DATA lv_soft TYPE c. 40 41 CALL METHOD me->set_data_table 42 CHANGING 43 data_table = mt_data[]. 44 45 CALL METHOD set_auto_redraw 46 EXPORTING 47 enable = 1. 48 49 ENDMETHOD. 50 51 METHOD z_init_cell_styles. 52 53 FIELD-SYMBOLS <fs_data> TYPE lvc_s_data. 54 55 LOOP AT mt_data ASSIGNING <fs_data>. 56 <fs_data>-style = 0. 57 ENDLOOP. 58 59 ENDMETHOD. 60 61 METHOD z_set_cell_style. 62 63 FIELD-SYMBOLS <fs_data> TYPE lvc_s_data. 64 IF row IS INITIAL. 65 IF col IS INITIAL. 66 67 EXIT. 68 ELSE. 69 70 LOOP AT mt_data ASSIGNING <fs_data> 71 WHERE col_pos = col. 72 <fs_data>-style = <fs_data>-style + style. 73 <fs_data>-style2 = <fs_data>-style2 + style2. 74 ENDLOOP. 75 ENDIF. 76 ELSE. 77 IF col IS INITIAL. 78 79 LOOP AT mt_data ASSIGNING <fs_data> 80 WHERE row_pos = row. 81 <fs_data>-style = <fs_data>-style + style. 82 <fs_data>-style2 = <fs_data>-style2 + style2. 83 ENDLOOP. 84 ELSE. 85 READ TABLE mt_data ASSIGNING <fs_data> 86 WITH KEY row_pos = row 87 col_pos = col. 88 IF sy-subrc EQ 0. 89 <fs_data>-style = <fs_data>-style + style. 90 <fs_data>-style2 = <fs_data>-style2 + style2. 91 ELSE. 92 EXIT. 93 ENDIF. 94 ENDIF. 95 ENDIF. 96 97 ENDMETHOD. 98 99 METHOD z_set_fixed_col_row. 100 101 me->set_fixed_cols( col ). 102 me->set_fixed_rows( row ). 103 104 ENDMETHOD. 105 106 METHOD z_set_merge_horiz. 107 108 FIELD-SYMBOLS <fs_cols> TYPE lvc_s_co01. 109 FIELD-SYMBOLS <fs_data> TYPE lvc_s_data. 110 DATA outputlen TYPE i. 111 112 SORT tab_col_merge. 113 LOOP AT tab_col_merge ASSIGNING <fs_cols>. 114 115 IF <fs_cols>-col_id LE 0. CONTINUE. ENDIF. 116 IF <fs_cols>-outputlen LE <fs_cols>-col_id. CONTINUE. ENDIF. 117 outputlen = <fs_cols>-outputlen - <fs_cols>-col_id. 118 LOOP AT mt_data ASSIGNING <fs_data> 119 WHERE row_pos = row AND 120 ( col_pos BETWEEN <fs_cols>-col_id AND 121 <fs_cols>-outputlen ). 122 123 IF <fs_data>-col_pos = <fs_cols>-col_id. 124 <fs_data>-mergehoriz = outputlen. 125 126 ELSE. 127 CLEAR <fs_data>-mergehoriz. 128 CLEAR <fs_data>-value. 129 ENDIF. 130 ENDLOOP. 131 132 ENDLOOP. 133 134 ENDMETHOD. 135 136 METHOD z_set_merge_vert. 137 138 FIELD-SYMBOLS <fs_cols> TYPE lvc_s_co01. 139 FIELD-SYMBOLS <fs_data> TYPE lvc_s_data. 140 DATA outputlen TYPE i. 141 142 SORT tab_col_merge. 143 144 LOOP AT tab_col_merge ASSIGNING <fs_cols>. 145 146 IF <fs_cols>-col_id LE 0. CONTINUE. ENDIF. 147 IF <fs_cols>-outputlen LE <fs_cols>-col_id. CONTINUE. ENDIF. 148 outputlen = <fs_cols>-outputlen - <fs_cols>-col_id. 149 LOOP AT mt_data ASSIGNING <fs_data> 150 WHERE row_pos = row AND 151 ( col_pos BETWEEN <fs_cols>-col_id AND 152 <fs_cols>-outputlen ). 153 154 IF <fs_data>-col_pos = <fs_cols>-col_id. 155 <fs_data>-mergevert = outputlen. 156 157 ELSE. 158 CLEAR <fs_data>-mergevert. 159 CLEAR <fs_data>-value. 160 ENDIF. 161 ENDLOOP. 162 163 ENDLOOP. 164 165 ENDMETHOD. 166 ENDCLASS.
1 *SE38直接复制粘贴 2 REPORT y4_b4_test16. 3 INCLUDE <cl_alv_control>. 4 INCLUDE <icon>. 5 DATA retc TYPE sy-subrc. . 6 DATA ok_code TYPE sy-ucomm. 7 DATA it_grp TYPE lvc_t_sgrp. 8 DATA it_fil TYPE lvc_t_filt. 9 DATA wa_fil TYPE lvc_s_filt. 10 11 * **** ALV_GRID **************************************************** 12 TYPES: BEGIN OF t_check_styles, 13 field01(20), 14 field02(20), 15 field03(20), 16 field04(20), 17 field05(20), 18 field06(20), 19 field07(20), 20 field08(20), 21 field09(20), 22 field10(20), 23 field11(20), 24 field12(20). 25 TYPES END OF t_check_styles. 26 27 DATA it_styles TYPE t_check_styles OCCURS 0. 28 DATA wa_styles TYPE t_check_styles. 29 FIELD-SYMBOLS <fs_styles> TYPE t_check_styles. 30 DATA : lt_fieldcatalog TYPE lvc_t_fcat. 31 DATA : ls_fieldcatalog TYPE lvc_t_fcat. 32 DATA : wa_cat TYPE lvc_s_fcat. 33 DATA : fieldname(40). 34 DATA : fieldnr(2) TYPE n. 35 36 FIELD-SYMBOLS <fs_cat> TYPE lvc_s_fcat. 37 38 DATA lt_iinfo TYPE lvc_t_info. 39 DATA wa_iinfo TYPE lvc_s_info. 40 DATA lt_idata TYPE lvc_t_data. 41 DATA wa_idata TYPE lvc_s_data. 42 43 DATA it_col_merge TYPE lvc_t_co01. 44 DATA wa_col_merge TYPE lvc_s_co01. 45 DATA: g_container TYPE scrfname VALUE 'CU_CON'. 46 DATA: g_custom_container TYPE REF TO cl_gui_custom_container. 47 DATA g_alv_grid TYPE REF TO zcl_gui_alv_grid_merge. 48 CLASS cl_gui_cfw DEFINITION LOAD. 49 50 DATA: x_save, "for Parameter I_SAVE 51 gs_variant TYPE disvariant. "for parameter IS_VARIANT 52 DATA gs_layout TYPE lvc_s_layo. " Layout 53 DATA wa_style TYPE lvc_s_styl. 54 55 START-OF-SELECTION. 56 CALL SCREEN 0200. 57 58 END-OF-SELECTION. 59 60 MODULE status_0200 OUTPUT. 61 62 ** Status und Titel setzen 63 SET PF-STATUS '0200'. 64 SET TITLEBAR '001'. 65 66 ** Objekte instanzieren und zuordnen: Grid 67 IF g_custom_container IS INITIAL. 68 69 CREATE OBJECT g_custom_container 70 EXPORTING 71 container_name = g_container. 72 73 CREATE OBJECT g_alv_grid 74 EXPORTING 75 i_parent = g_custom_container. 76 77 gs_layout-stylefname = 'CELL'. 78 gs_layout-no_headers = 'X'. 79 gs_layout-cwidth_opt = ' '. 80 gs_layout-no_toolbar = 'X'. 81 82 ** Feldkatalog erzeugen 83 REFRESH lt_fieldcatalog. 84 85 CALL FUNCTION 'LVC_FIELDCATALOG_MERGE' 86 EXPORTING 87 i_internal_tabname = 'IT_STYLES' 88 CHANGING 89 ct_fieldcat = lt_fieldcatalog 90 EXCEPTIONS 91 inconsistent_interface = 1 92 program_error = 2 93 OTHERS = 3. 94 95 REFRESH lt_fieldcatalog. 96 REFRESH lt_iinfo. 97 98 DO 12 TIMES. 99 CLEAR wa_cat. 100 fieldnr = sy-index. 101 wa_cat-col_pos = sy-index. 102 CONCATENATE 'FIELD' fieldnr INTO fieldname. 103 wa_cat-fieldname = fieldname. 104 wa_cat-tabname = '1'. 105 wa_cat-datatype = 'CHAR'. 106 wa_cat-inttype = 'C'. 107 wa_cat-intlen = 20. 108 IF sy-index > 1. 109 wa_cat-outputlen = 6. 110 ELSE. 111 wa_cat-outputlen = 20. 112 ENDIF. 113 wa_cat-reptext = fieldname. 114 wa_cat-scrtext_l = fieldname. 115 wa_cat-scrtext_m = fieldname. 116 wa_cat-scrtext_s = fieldname. 117 wa_cat-scrtext_l = fieldname. 118 APPEND wa_cat TO lt_fieldcatalog. 119 ENDDO. 120 121 * 1 Zeile 122 CLEAR wa_styles. 123 wa_styles-field01 = 'TRICKTRESOR'. 124 wa_styles-field03 = 'F'. 125 wa_styles-field04 = 'P'. 126 wa_styles-field09 = 'M'. 127 wa_styles-field10 = 'K'. 128 APPEND wa_styles TO it_styles. 129 * 2 Zeile 130 CLEAR wa_styles. 131 wa_styles-field03 = 'HQ'. 132 wa_styles-field04 = 'HC'. 133 wa_styles-field08 = 'HW'. 134 wa_styles-field09 = 'HC'. 135 wa_styles-field10 = 'HC'. 136 wa_styles-field12 = 'HW'. 137 APPEND wa_styles TO it_styles. 138 * 3-Zeile 139 CLEAR wa_styles. 140 wa_styles-field01 = 'Bezeichnung'. 141 wa_styles-field02 = 'Radius'. 142 wa_styles-field03 = 'WPX 12'. 143 wa_styles-field04 = 'WAP 25'. 144 wa_styles-field05 = 'WAP 35'. 145 wa_styles-field06 = 'WTP 35'. 146 wa_styles-field07 = 'WXP 45'. 147 wa_styles-field08 = 'WPM'. 148 wa_styles-field09 = 'WXM 35'. 149 wa_styles-field10 = 'WAK 15'. 150 wa_styles-field11 = 'WAK 25'. 151 wa_styles-field12 = 'WKM'. 152 APPEND wa_styles TO it_styles. 153 154 * 4..Zeile 155 CLEAR wa_styles. 156 wa_styles-field01 = 'SPMW 060304 T - A 27'. 157 wa_styles-field02 = '0.54'. 158 wa_styles-field03 = icon_led_green. 159 wa_styles-field04 = icon_led_yellow. 160 wa_styles-field05 = icon_led_red. 161 wa_styles-field08 = icon_led_yellow. 162 APPEND wa_styles TO it_styles. 163 164 CLEAR wa_styles. 165 wa_styles-field01 = 'SPMW 060304 - A 57'. 166 wa_styles-field02 = '0.43'. 167 wa_styles-field03 = icon_led_yellow. 168 wa_styles-field05 = icon_led_red. 169 wa_styles-field08 = icon_led_yellow. 170 wa_styles-field10 = icon_led_yellow. 171 wa_styles-field11 = icon_led_red. 172 wa_styles-field12 = icon_led_yellow. 173 APPEND wa_styles TO it_styles. 174 175 CLEAR wa_styles. 176 wa_styles-field01 = 'SPMW 060304 - D 51'. 177 wa_styles-field02 = '0.76'. 178 wa_styles-field04 = icon_led_yellow. 179 wa_styles-field05 = icon_led_red. 180 wa_styles-field06 = icon_led_red. 181 wa_styles-field07 = icon_led_red. 182 APPEND wa_styles TO it_styles. 183 184 CLEAR wa_styles. 185 wa_styles-field01 = 'SPMW 060304 - F 55'. 186 wa_styles-field02 = '0.44'. 187 wa_styles-field03 = icon_led_red. 188 wa_styles-field05 = icon_led_green. 189 wa_styles-field06 = icon_led_yellow. 190 wa_styles-field07 = icon_led_red. 191 wa_styles-field09 = icon_led_yellow. 192 wa_styles-field10 = icon_led_green. 193 wa_styles-field11 = icon_led_yellow. 194 wa_styles-field12 = icon_led_yellow. 195 APPEND wa_styles TO it_styles. 196 197 CALL METHOD g_alv_grid->set_table_for_first_display 198 EXPORTING 199 is_variant = gs_variant 200 i_save = x_save 201 is_layout = gs_layout 202 CHANGING 203 it_fieldcatalog = lt_fieldcatalog 204 it_outtab = it_styles. 205 206 REFRESH it_col_merge. 207 208 *** DEMO vertikal verbinden 209 wa_col_merge-col_id = 1. 210 wa_col_merge-outputlen = 2. 211 APPEND wa_col_merge TO it_col_merge. 212 213 "打竖合并 214 CALL METHOD g_alv_grid->z_set_merge_vert 215 EXPORTING 216 row = 1 "第一行 217 CHANGING 218 tab_col_merge = it_col_merge. 219 "设置格式 220 wa_style-style = alv_style_font_bold"粗体 221 + alv_style_align_center_center"居中 222 + alv_style_color_key."颜色 223 224 CALL METHOD g_alv_grid->z_set_cell_style 225 EXPORTING 226 row = 1 227 col = 1 228 style = wa_style-style. 229 230 *** VERTIKAL verbinden 231 CLEAR it_col_merge. 232 233 wa_col_merge-col_id = 4. 234 wa_col_merge-outputlen = 8. 235 APPEND wa_col_merge TO it_col_merge. 236 237 wa_col_merge-col_id = 10. 238 wa_col_merge-outputlen = 12. 239 APPEND wa_col_merge TO it_col_merge. 240 241 "打横合并 242 CALL METHOD g_alv_grid->z_set_merge_horiz 243 EXPORTING 244 row = 1 245 CHANGING 246 tab_col_merge = it_col_merge. 247 248 wa_style-style = alv_style_font_bold. 249 250 CALL METHOD g_alv_grid->z_set_cell_style 251 EXPORTING 252 row = 1 253 col = 3 254 style = wa_style-style. 255 256 CALL METHOD g_alv_grid->z_set_cell_style 257 EXPORTING 258 row = 1 259 col = 4 260 style = wa_style-style. 261 262 CALL METHOD g_alv_grid->z_set_cell_style 263 EXPORTING 264 row = 1 265 col = 9 266 style = wa_style-style. 267 268 CALL METHOD g_alv_grid->z_set_cell_style 269 EXPORTING 270 row = 1 271 col = 10 272 style = wa_style-style. 273 274 REFRESH it_col_merge. 275 276 wa_col_merge-col_id = 4. 277 wa_col_merge-outputlen = 7. 278 APPEND wa_col_merge TO it_col_merge. 279 280 CALL METHOD g_alv_grid->z_set_merge_horiz 281 EXPORTING 282 row = 2 283 CHANGING 284 tab_col_merge = it_col_merge. 285 286 wa_style-style = alv_style_color_group + 287 alv_style_align_center_center. 288 289 CALL METHOD g_alv_grid->z_set_cell_style 290 EXPORTING 291 col = 3 292 style = wa_style-style. 293 294 wa_style-style = alv_style_color_heading + 295 alv_style_align_center_center. 296 297 CALL METHOD g_alv_grid->z_set_cell_style 298 EXPORTING 299 col = 4 300 style = wa_style-style. 301 302 CALL METHOD g_alv_grid->z_set_cell_style 303 EXPORTING 304 col = 5 305 style = wa_style-style. 306 307 CALL METHOD g_alv_grid->z_set_cell_style 308 EXPORTING 309 col = 6 310 style = wa_style-style. 311 312 CALL METHOD g_alv_grid->z_set_cell_style 313 EXPORTING 314 col = 7 315 style = wa_style-style. 316 CALL METHOD g_alv_grid->z_set_cell_style 317 EXPORTING 318 col = 8 319 style = wa_style-style. 320 321 wa_style-style = alv_style_color_total + 322 alv_style_align_center_center. 323 324 CALL METHOD g_alv_grid->z_set_cell_style 325 EXPORTING 326 col = 9 327 style = wa_style-style. 328 329 wa_style-style = alv_style_color_negative + 330 alv_style_align_center_center. 331 332 CALL METHOD g_alv_grid->z_set_cell_style 333 EXPORTING 334 col = 10 335 style = wa_style-style. 336 337 CALL METHOD g_alv_grid->z_set_cell_style 338 EXPORTING 339 col = 11 340 style = wa_style-style. 341 342 CALL METHOD g_alv_grid->z_set_cell_style 343 EXPORTING 344 col = 12 345 style = wa_style-style. 346 347 CALL METHOD g_alv_grid->z_set_cell_style 348 EXPORTING 349 col = 13 350 style = wa_style-style. 351 352 wa_style-style = alv_style_color_positive + 353 alv_style_align_center_center. 354 355 CALL METHOD g_alv_grid->z_set_cell_style 356 EXPORTING 357 col = 14 358 style = wa_style-style. 359 360 CALL METHOD g_alv_grid->z_set_cell_style 361 EXPORTING 362 col = 15 363 style = wa_style-style. 364 365 wa_style-style = alv_style_color_int_background + 366 alv_style_align_center_center. 367 368 CALL METHOD g_alv_grid->z_set_cell_style 369 EXPORTING 370 col = 16 371 style = wa_style-style. 372 373 wa_style-style = alv_style_color_positive + 374 alv_style_align_center_center + 375 alv_style_font_italic. 376 377 CALL METHOD g_alv_grid->z_set_cell_style 378 EXPORTING 379 row = 4 380 col = 2 381 style = wa_style-style. 382 383 g_alv_grid->z_set_fixed_col_row( 384 EXPORTING col = 3 385 row = 3 ). 386 387 g_alv_grid->z_display( ). 388 389 ENDIF. 390 ENDMODULE. "status_0100 OUTPUT 391 392 MODULE user_command_0200 INPUT. 393 394 cl_gui_cfw=>dispatch( ). 395 396 CASE ok_code. 397 WHEN '&F03' OR '&F12' OR '&F15'. 398 SET SCREEN 0. LEAVE SCREEN. 399 ENDCASE. 400 401 ENDMODULE. " USER_COMMAND_0100 INPUT
运行后的样子