赋值调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
DATA: l_name LIKE wwwdatatab, l_mime LIKE w3mime OCCURS 10, l_filename TYPE string, l_path TYPE string, l_title TYPE string, l_row TYPE i, l_col TYPE i. l_name-relid = 'MI'. * l_name-objid = 'ZFIR_001'."EXCEL模板的名 l_name-objid = 'ZFIR_00102'."EXCEL模板的名 20211223 l_name-text = sy-title. CALL FUNCTION 'WWWDATA_IMPORT' EXPORTING key = l_name TABLES mime = l_mime EXCEPTIONS wrong_object_type = 1 import_error = 2 OTHERS = 3. IF p_dwn <> ''. l_fullpath = p_dwn. ELSE. l_fullpath = 'D:\资产负债表.XLS'. ENDIF. lpath2 = l_fullpath. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING confirm_overwrite = 'X' filename = l_fullpath filetype = 'BIN' TABLES data_tab = l_mime. *--打开excel模板 PERFORM frm_open_excel USING l_fullpath 0. *--打开excel第一个sheet v_sheet_number = 1. PERFORM frm_open_excel_sheet USING v_sheet_number. *--写excel file的内容 DATA: s_titl TYPE string, s_yea TYPE string, y_mot TYPE string. DATA: lv_bzdw TYPE string. CLEAR s_titl. * PERFORM FRM_FILL_CELL_NEW USING 3 1 DW 10 1. * PERFORM FRM_FILL_CELL_NEW USING 2 4 RQ 10 1. lv_bzdw = |{ dw } { rq } 单位:元 |. PERFORM frm_fill_cell_new USING 2 1 lv_bzdw 10 1. LOOP AT it_row_col_sum. l_row = it_row_col_sum-zrow. l_col = it_row_col_sum-zcol. IF it_row_col_sum-value IS NOT INITIAL. PERFORM frm_fill_cell_new USING l_row l_col it_row_col_sum-value 10 1. ENDIF. ENDLOOP. TYPES:BEGIN OF ty_name , dept TYPE zfi_rep-dept, name TYPE zfi_rep-name, END OF ty_name. DATA:wa_name TYPE ty_name, lt_name TYPE STANDARD TABLE OF ty_name. DATA:gv_name1 TYPE char100, gv_name2 TYPE char100, gv_name3 TYPE char100, gv_name TYPE char100. SELECT dept name INTO TABLE lt_name FROM zfi_rep WHERE tcode = 'ZFI090' AND bukrs = p_rbukrs. LOOP AT lt_name INTO wa_name . IF wa_name-dept = '01'. CONCATENATE '法定代表人:' wa_name-name INTO gv_name1. ELSEIF wa_name-dept = '02'. CONCATENATE '主管会计工作负责人:' wa_name-name INTO gv_name2. ELSEIF wa_name-dept = '03'. CONCATENATE '会计机构负责人:' wa_name-name INTO gv_name3. ENDIF. ENDLOOP. CONCATENATE ' ' ' ' ' ' ' ' ' ' gv_name1 ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' gv_name2 ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' gv_name3 INTO gv_name. * end add * 关闭EXCEL PERFORM frm_save_excel USING l_fullpath. |
ole通用部分代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 |
*&---------------------------------------------------------------------* *& 包含 ZEXCEL_PUBLIC *&---------------------------------------------------------------------* DATA: ROWS TYPE OLE2_OBJECT,"Sheet的行对象 COLUMNS TYPE OLE2_OBJECT. *导出EXCEL所用到变量 DATA: G_EXCEL TYPE OLE2_OBJECT, G_SHEET TYPE OLE2_OBJECT, G_WORKBOOK TYPE OLE2_OBJECT, G_CELL TYPE OLE2_OBJECT, G_RANGE TYPE OLE2_OBJECT, G_FONT TYPE OLE2_OBJECT, G_CELL1 TYPE OLE2_OBJECT, G_CELL2 TYPE OLE2_OBJECT, G_CELLS TYPE OLE2_OBJECT, G_COLUMNS TYPE OLE2_OBJECT, G_SHEET_NUMBER TYPE I, G_SHADING TYPE OLE2_OBJECT, G_BORDER TYPE OLE2_OBJECT. *&---------------------------------------------------------------------* *& Form FRM_OPEN_EXCEL *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_L_FULLPATH text *----------------------------------------------------------------------* FORM FRM_OPEN_EXCEL USING P_FULLPATH P_VISIBLE. CREATE OBJECT G_EXCEL 'Excel.Application'. SET PROPERTY OF G_EXCEL 'VISIBLE' = P_VISIBLE. CALL METHOD OF G_EXCEL 'Workbooks' = G_WORKBOOK. CALL METHOD OF G_WORKBOOK 'Open' = G_WORKBOOK EXPORTING #1 = P_FULLPATH. IF SY-SUBRC <> 0. MESSAGE S009(ZHRFD) WITH 'Open file failed!'. STOP. ENDIF. ENDFORM. " FRM_OPEN_EXCEL *&---------------------------------------------------------------------* *& Form FRM_OPEN_EXCEL_SHEET *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_L_SHEET_NUMBER text *----------------------------------------------------------------------* FORM FRM_OPEN_EXCEL_SHEET USING P_SHEET. CALL METHOD OF G_WORKBOOK 'Sheets' = G_SHEET EXPORTING #1 = P_SHEET. CALL METHOD OF G_SHEET 'Activate'. ENDFORM. " FRM_OPEN_EXCEL_SHEET *&---------------------------------------------------------------------* *& Form FRM_COPY_SHEET_ALL *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM FRM_COPY_SHEET_ALL USING P_FROM_SHEET P_TO_SHEET. CALL METHOD OF G_EXCEL 'Sheets' = G_SHEET EXPORTING #1 = P_FROM_SHEET. CALL METHOD OF G_SHEET 'Cells' = G_CELL. CALL METHOD OF G_CELL 'Copy'. CALL METHOD OF G_EXCEL 'Sheets' = G_SHEET EXPORTING #1 = P_TO_SHEET. CALL METHOD OF G_EXCEL 'Range' = G_RANGE EXPORTING #1 = 'A1'. CALL METHOD OF G_RANGE 'PasteSpecial'. ENDFORM. " FRM_COPY_SHEET_ALL *&---------------------------------------------------------------------* *& Form FRM_SAVE_EXCEL *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_L_FULLPATH text *----------------------------------------------------------------------* FORM FRM_SAVE_EXCEL USING P_FULLPATH. GET PROPERTY OF G_EXCEL 'ActiveWorkbook' = G_WORKBOOK. CALL METHOD OF G_WORKBOOK 'Save'. *SET PROPERTY OF EXCEL 'DisplayAlerts' = 0. CALL METHOD OF G_WORKBOOK 'CLOSE'. CALL METHOD OF G_EXCEL 'QUIT'. FREE G_SHEET. FREE G_WORKBOOK. FREE G_EXCEL. ENDFORM. " FRM_SAVE_EXCEL *&---------------------------------------------------------------------* *& Form FRM_CREATE_SHEET *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM FRM_CREATE_SHEET . CALL METHOD OF G_EXCEL 'Sheets' = G_SHEET. CALL METHOD OF G_SHEET 'ADD'. ENDFORM. " FRM_CREATE_SHEET *&---------------------------------------------------------------------* *& Form FRM_SET_SHEET_NAME *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_L_SHEET_NAME text *----------------------------------------------------------------------* FORM FRM_SET_SHEET_NAME USING P_SHEET_NAME. SET PROPERTY OF G_SHEET 'Name' = P_SHEET_NAME. ENDFORM. " FRM_SET_SHEET_NAME *&---------------------------------------------------------------------* *& Form FRM_FILL_CELL *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_L_ROW text * -->P_L_COL text * -->P_L_XH text *----------------------------------------------------------------------* FORM FRM_FILL_CELL USING P_ROW P_COL P_VALUE. DATA: LO_FONT TYPE OLE2_OBJECT. CALL METHOD OF G_EXCEL 'CELLS' = G_CELL EXPORTING #1 = P_ROW #2 = P_COL. SET PROPERTY OF G_CELL 'VALUE' = P_VALUE. GET PROPERTY OF G_CELL 'font' = G_FONT. SET PROPERTY OF G_FONT 'size' = 9. SET PROPERTY OF G_FONT 'Name' = '宋体'. * SET PROPERTY OF CELL 'HorizontalAlignment' = 3. " 居中 ENDFORM. " FRM_FILL_CELL *&---------------------------------------------------------------------* *& Form FRM_FILL_CELL *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_L_ROW text * -->P_L_COL text * -->P_L_XH text *----------------------------------------------------------------------* FORM FRM_FILL_CELL_NEW USING P_ROW P_COL P_VALUE P_SIZE P_ALIGNMENT. DATA: LO_FONT TYPE OLE2_OBJECT. CALL METHOD OF G_EXCEL 'CELLS' = G_CELL EXPORTING #1 = P_ROW #2 = P_COL. SET PROPERTY OF G_CELL 'VALUE' = P_VALUE. GET PROPERTY OF G_CELL 'font' = G_FONT. SET PROPERTY OF G_FONT 'size' = P_SIZE. SET PROPERTY OF G_FONT 'Name' = '宋体'. SET PROPERTY OF G_CELL 'HorizontalAlignment' = P_ALIGNMENT. " 1=靠左 4=靠右 3=居中 SET PROPERTY OF G_CELL 'LineStyle' = 'xlContinuous'. * SET PROPERTY OF g_cell 'ColumnWidth' = 17. ENDFORM. " FRM_FILL_CELL *&---------------------------------------------------------------------* *& Form FRM_MERGE_RANGE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_L_XH_MERGE_X1 text * -->P_L_XH_MERGE_Y1 text * -->P_L_XH_MERGE_X2 text * -->P_L_XH_MERGE_Y2 text *----------------------------------------------------------------------* FORM FRM_MERGE_RANGE USING P_X1 P_Y1 P_X2 P_Y2. CALL METHOD OF G_EXCEL 'Cells' = G_CELL1 EXPORTING #1 = P_X1 #2 = P_Y1. CALL METHOD OF G_EXCEL 'Cells' = G_CELL2 EXPORTING #1 = P_X2 #2 = P_Y2. CALL METHOD OF G_EXCEL 'Range' = G_CELLS EXPORTING #1 = G_CELL1 #2 = G_CELL2. CALL METHOD OF G_CELLS 'merge'. ENDFORM. " FRM_MERGE_RANGE *&---------------------------------------------------------------------* *& Form FRM_DRAWLINE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_X1 text * -->P_Y1 text * -->P_X2 text * -->P_Y2 text *----------------------------------------------------------------------* FORM FRM_DRAWLINE USING P_X1 P_Y1 P_X2 P_Y2. CALL METHOD OF G_EXCEL 'Cells' = G_CELL1 EXPORTING #1 = P_X1 #2 = P_Y1. CALL METHOD OF G_EXCEL 'Cells' = G_CELL2 EXPORTING #1 = P_X2 #2 = P_Y2. CALL METHOD OF G_EXCEL 'Range' = G_CELLS EXPORTING #1 = G_CELL1 #2 = G_CELL2. * CALL METHOD OF cells 'Select'. GET PROPERTY OF G_CELLS 'borders' = G_RANGE. SET PROPERTY OF G_RANGE 'weight' = '2'. SET PROPERTY OF G_RANGE 'linestyle' = '1'. ENDFORM. " FRM_DRAWLINE *&---------------------------------------------------------------------* *& Form FRM_WRITE_TITLE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_2 text * -->P_1 text * -->P_L_STR text *----------------------------------------------------------------------* FORM FRM_WRITE_TITLE USING P_ROW P_COL P_VALUE. DATA: LO_FONT TYPE OLE2_OBJECT. CALL METHOD OF G_EXCEL 'CELLS' = G_CELL EXPORTING #1 = P_ROW #2 = P_COL. SET PROPERTY OF G_CELL 'VALUE' = P_VALUE. GET PROPERTY OF G_CELL 'font' = G_FONT. SET PROPERTY OF G_FONT 'size' = 12. SET PROPERTY OF G_FONT 'Name' = '宋体'. SET PROPERTY OF G_FONT 'bold' = 1. SET PROPERTY OF G_CELL 'HorizontalAlignment' = 3. " 居中 ENDFORM. " FRM_WRITE_TITLE *&---------------------------------------------------------------------* *& Form FRM_DEL_COL *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_COL text *----------------------------------------------------------------------* FORM FRM_DEL_COL USING P_COLNUM. CALL METHOD OF G_EXCEL 'COLUMNS' = G_COLUMNS EXPORTING #1 = P_COLNUM. CALL METHOD OF G_COLUMNS 'DELETE'. ENDFORM. " FRM_DEL_COL *&---------------------------------------------------------------------* *& Form FRM_CLEAR_DATA *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_L_YZ_MERGE_X1 text * -->P_L_YZ_MERGE_Y1 text * -->P_L_YZ_MERGE_X2 text * -->P_L_YZ_MERGE_Y2 text *----------------------------------------------------------------------* FORM FRM_CLEAR_DATA USING P_X1 P_Y1 P_X2 P_Y2. PERFORM FRM_SELECT_RANGE USING P_X1 P_Y1 P_X2 P_Y2. CALL METHOD OF G_RANGE 'ClearContents'. ENDFORM. " FRM_CLEAR_DATA *&---------------------------------------------------------------------* *& Form FRM_SELECT_RANGE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_P_X1 text * -->P_P_Y1 text * -->P_P_X2 text * -->P_P_Y2 text *----------------------------------------------------------------------* FORM FRM_SELECT_RANGE USING P_X1 P_Y1 P_X2 P_Y2. CALL METHOD OF G_EXCEL 'CELLS' = G_CELL1 EXPORTING #1 = P_X1 #2 = P_Y1. CALL METHOD OF G_EXCEL 'CELLS' = G_CELL2 EXPORTING #1 = P_X2 #2 = P_Y2. CALL METHOD OF G_EXCEL 'RANGE' = G_RANGE EXPORTING #1 = G_CELL1 #2 = G_CELL2. CALL METHOD OF G_RANGE 'SELECT'. ENDFORM. " FRM_SELECT_RANGE *&---------------------------------------------------------------------* *& Form FRM_PREVIEW_EXCEL *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM FRM_PREVIEW_EXCEL . SET PROPERTY OF G_EXCEL 'VISIBLE' = 1. CALL METHOD OF G_SHEET 'PrintPreview'. ENDFORM. " FRM_PREVIEW_EXCEL *&---------------------------------------------------------------------* *& Form insert_colmun *&---------------------------------------------------------------------* * 在EXCEL模板中插入行 *----------------------------------------------------------------------* * -->row 行位置 *----------------------------------------------------------------------* FORM INSERT_ROW USING ROW . CALL METHOD OF G_EXCEL 'ROWS' = ROWS EXPORTING #1 = ROW. CALL METHOD OF ROWS 'INSERT'. FREE OBJECT ROWS. ENDFORM. "insert_row *&---------------------------------------------------------------------* *& Form delete_row *&---------------------------------------------------------------------* * 在EXCEL模板中删除行 *----------------------------------------------------------------------* * -->row 行位置 *----------------------------------------------------------------------* FORM DELETE_ROW USING ROW . CALL METHOD OF G_EXCEL 'ROWS' = ROWS EXPORTING #1 = ROW. CALL METHOD OF ROWS 'DELETE'. FREE OBJECT ROWS. ENDFORM. "delete_row *&---------------------------------------------------------------------* *& Form auto_fit *&---------------------------------------------------------------------* * 在EXCEL模板中自适应高度 *----------------------------------------------------------------------* * *----------------------------------------------------------------------* FORM AUTO_FIT USING ROW . DATA: L_END TYPE STRING, L_STR TYPE STRING. L_STR = ROW. CONCATENATE L_STR ':' L_STR INTO L_END . CONDENSE L_END NO-GAPS. CALL METHOD OF G_EXCEL 'ROWS' = ROWS EXPORTING #1 = L_END. CALL METHOD OF ROWS 'SELECT'. CALL METHOD OF ROWS 'AutoFit'. ENDFORM. "AUTO_FIT *&---------------------------------------------------------------------* *& Form COLUMNWIDTH *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_1091 text *----------------------------------------------------------------------* FORM COLUMNWIDTH USING COLNUM . CALL METHOD OF G_EXCEL 'COLUMNS' = COLUMNS EXPORTING #1 = COLNUM. CALL METHOD OF COLUMNS 'AutoFit'. ENDFORM. "columnwidth *&---------------------------------------------------------------------* *& Form FRM_SET_SHEET_STLYE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM FRM_SET_SHEET_STLYE USING P_START_ROW P_START_COL P_END_ROW P_END_COL. * CHANGE RANGE VALUES - include all lines (1,2 and 3) FREE G_RANGE. CALL METHOD OF G_EXCEL 'Cells' = G_CELL1 "start cell EXPORTING #1 = P_START_ROW "down #2 = P_START_COL. "across CALL METHOD OF G_EXCEL 'Cells' = G_CELL2 "end cell EXPORTING #1 = P_END_ROW "down #2 = P_END_COL. "across CALL METHOD OF G_EXCEL 'Range' = G_RANGE EXPORTING #1 = G_CELL1 #2 = G_CELL2. * SET BORDER PROPERTIES of range CALL METHOD OF G_RANGE 'BORDERS' = G_BORDER EXPORTING #1 = '1'. "left SET PROPERTY OF G_BORDER 'LineStyle' = '1'. "line style solid, dashed... SET PROPERTY OF G_BORDER 'WEIGHT' = 2. "max = 4 FREE OBJECT G_BORDER. CALL METHOD OF G_RANGE 'BORDERS' = G_BORDER EXPORTING #1 = '2'. "right SET PROPERTY OF G_BORDER 'LineStyle' = '1'. SET PROPERTY OF G_BORDER 'WEIGHT' = 2. "max = 4 FREE OBJECT G_BORDER. CALL METHOD OF G_RANGE 'BORDERS' = G_BORDER EXPORTING #1 = '3'. "top SET PROPERTY OF G_BORDER 'LineStyle' = '1'. SET PROPERTY OF G_BORDER 'WEIGHT' = 2. "max = 4 FREE OBJECT G_BORDER. CALL METHOD OF G_RANGE 'BORDERS' = G_BORDER EXPORTING #1 = '4'. "bottom SET PROPERTY OF G_BORDER 'LineStyle' = '1'. SET PROPERTY OF G_BORDER 'WEIGHT' = 2. "max = 4 FREE OBJECT G_BORDER. * Overwites all cell values in range to equal 'test' * SET PROPERTY OF range 'VALUE' = 'test'. *********************************************** * Set Columns to auto fit to width of text * *********************************************** CALL METHOD OF G_EXCEL 'Columns' = G_COLUMNS. CALL METHOD OF G_COLUMNS 'Autofit'. FREE OBJECT G_COLUMNS. ENDFORM. " FRM_SET_SHEET_STLYE |